为什么这个是错误的呢?

Game-Over 2014-11-11 02:30:54
求求各位大神帮忙看看,说说,改改,到底哪里错误了。 谢谢啦


import java.util.*;  
import java.util.Scanner;
import java.util.Stack;
import java.io.File;

public class test

{
private static int i;
private static final char LEFTPARENT = '(';
private static final char RIGHTPARENT = ')';
private static final char LEFTBRACE = '{';
private static final char RIGHTBRACE = '}';
private static final char LEFTBRACKET = '[';
private static final char RIGHTBRACKET = ']';

public static boolean isBalanced(String s)
{
Stack<E> stack = new Stack<E>();
for (i = 0; i < s.length(); i++)
{

if (s.charAt(i) == LEFTPARENT) stack.push(LEFTPARENT);

else if (s.charAt(i) == LEFTBRACE) stack.push(LEFTBRACE);

else if (s.charAt(i) == LEFTBRACKET) stack.push(LEFTBRACKET);

else if (s.charAt(i) == RIGHTPARENT)
{
if (stack.isEmpty())
return false;
if (stack.pop() != LEFTPARENT)
return false;
}
else if (s.charAt(i) == RIGHTBRACE)
{
if (stack.isEmpty())
return false;
if (stack.pop() != LEFTBRACE)
return false;
}
else if (s.charAt(i) == RIGHTBRACKET)
{
if (stack.isEmpty())
return false;
if (stack.pop() != LEFTBRACKET)
return false;
}
}
return stack.isEmpty();
}
public static void main(String args[])
{
Scanner reader = new Scanner(new File("data.txt"));
while (reader.hasNext()){
int s = reader.nextInt();
System.out.println(i);
}

{
if (isBalanced(s))
System.out.println("The grouping symbols in"+" \""+ s +"\" "+"match");
else
System.out.println("The grouping symbols in"+" \""+ s +"\" "+ "do not match");
System.out.println();
}
}
}
...全文
409 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你多次提到Stack<E>这个是吧,你去看看java泛型就知道这个e是什么意思了,这个E是要你去设置的数据类型。。。
Game-Over 2014-11-12
  • 打赏
  • 举报
回复
引用 4 楼 zhuxianxin0118 的回复:
楼主 我修改的代码如下 s 你定义在while循环里面了 应该拿到while循环外面定义 还有s是int 类型的 应该用其包装类 转换为String类型 才能传参 给方法 isBalanced(String s) 关于泛型的问题 由于你声明的静态常量为Char类型 而后又将char类型的添加到到stack中 其泛型很明确是 Character 类型 public class test { private static int i; private static char LEFTPARENT = '('; private static char RIGHTPARENT = ')'; private static char LEFTBRACE = '{'; private static char RIGHTBRACE = '}'; private static char LEFTBRACKET = '['; private static char RIGHTBRACKET = ']'; public static boolean isBalanced(String s) { Stack<Character> stack = new Stack<Character>(); for (i = 0; i < s.length(); i++) { if (s.charAt(i) == LEFTPARENT) stack.push(LEFTPARENT); else if (s.charAt(i) == LEFTBRACE) stack.push(LEFTBRACE); else if (s.charAt(i) == LEFTBRACKET) stack.push(LEFTBRACKET); else if (s.charAt(i) == RIGHTPARENT) { if (stack.isEmpty()) return false; if (stack.pop() != LEFTPARENT) return false; } else if (s.charAt(i) == RIGHTBRACE) { if (stack.isEmpty()) return false; if (stack.pop() != LEFTBRACE) return false; } else if (s.charAt(i) == RIGHTBRACKET) { if (stack.isEmpty()) return false; if (stack.pop() != LEFTBRACKET) return false; } } return stack.isEmpty(); } public static void main(String args[]) { Scanner reader = null; try { reader = new Scanner(new File("data.txt")); } catch (Exception e) { e.printStackTrace(); } Integer s = 0; while (reader.hasNext()){ s = reader.nextInt(); System.out.println(i); } ; { if (isBalanced(s.toString())) System.out.println("The grouping symbols in"+" \""+ s +"\" "+"match"); else System.out.println("The grouping symbols in"+" \""+ s +"\" "+ "do not match"); System.out.println(); } } }
谢谢回复,不过他的要求是用Stack<E>但是,我不知道怎么建立。
南猿北辙 2014-11-12
  • 打赏
  • 举报
回复
楼主 我修改的代码如下 s 你定义在while循环里面了 应该拿到while循环外面定义 还有s是int 类型的 应该用其包装类 转换为String类型 才能传参 给方法 isBalanced(String s) 关于泛型的问题 由于你声明的静态常量为Char类型 而后又将char类型的添加到到stack中 其泛型很明确是 Character 类型 public class test { private static int i; private static char LEFTPARENT = '('; private static char RIGHTPARENT = ')'; private static char LEFTBRACE = '{'; private static char RIGHTBRACE = '}'; private static char LEFTBRACKET = '['; private static char RIGHTBRACKET = ']'; public static boolean isBalanced(String s) { Stack<Character> stack = new Stack<Character>(); for (i = 0; i < s.length(); i++) { if (s.charAt(i) == LEFTPARENT) stack.push(LEFTPARENT); else if (s.charAt(i) == LEFTBRACE) stack.push(LEFTBRACE); else if (s.charAt(i) == LEFTBRACKET) stack.push(LEFTBRACKET); else if (s.charAt(i) == RIGHTPARENT) { if (stack.isEmpty()) return false; if (stack.pop() != LEFTPARENT) return false; } else if (s.charAt(i) == RIGHTBRACE) { if (stack.isEmpty()) return false; if (stack.pop() != LEFTBRACE) return false; } else if (s.charAt(i) == RIGHTBRACKET) { if (stack.isEmpty()) return false; if (stack.pop() != LEFTBRACKET) return false; } } return stack.isEmpty(); } public static void main(String args[]) { Scanner reader = null; try { reader = new Scanner(new File("data.txt")); } catch (Exception e) { e.printStackTrace(); } Integer s = 0; while (reader.hasNext()){ s = reader.nextInt(); System.out.println(i); } ; { if (isBalanced(s.toString())) System.out.println("The grouping symbols in"+" \""+ s +"\" "+"match"); else System.out.println("The grouping symbols in"+" \""+ s +"\" "+ "do not match"); System.out.println(); } } }
wyc_ 2014-11-12
  • 打赏
  • 举报
回复
引用 3 楼 u012945394 的回复:
[quote=引用 2 楼 cumtwyc 的回复:] Stack<E>中的E是类型参数,你这里需要的是Character ,这样Stack<Character> stack = new Stack<Character>(); main函数中 if (isBalanced(s)) ,s都没定义啊
可是他的要求是要用stack<E>啊。 s不都定义完了吗?int s = reader.nextInt();[/quote] E代表一种数据类型,你可以自己实现一个类,也可以使用Java现有的数据类型,这里明显用Character。s定义了,但是它只在while循环内可见
Cold_5 2014-11-12
  • 打赏
  • 举报
回复
学习学习 我是菜鸟
liumoujie3 2014-11-12
  • 打赏
  • 举报
回复
package collectiontest; public class MyStack<E> { private Object[] es = null; private final int len = 2; public MyStack() { es = new Object[len]; } public boolean isEmpty() { for (Object e : es) { if (e != null) { return false; } } return true; } private void fill(Object[] os, Object o) { for (Object obj : os) { obj = o; } } private void beforePop() { for (Object e : es) { if (e == null) { return; } } Object[] back = es; es = new Object[es.length + len]; fill(es, null); for (int i = 0; i < back.length; i++) { es[i] = back[i]; } } @SuppressWarnings("unchecked") public synchronized E peek() { if (isEmpty()) { throw new RuntimeException("stack is emptyp"); } return (E) es[0]; } public synchronized void push(E e) { beforePop(); for (int i = es.length - 1; i >= 0; i--) { if (es[i] != null) { es[i + 1] = es[i]; } } es[0] = e; } @SuppressWarnings("unchecked") public synchronized E pop() { if (isEmpty()) { throw new RuntimeException("stack is emptyp"); } E ret = (E) es[0]; remove(); return ret; } private void remove() { for (int i = 0; i < es.length - 1; i++) { es[i] = es[i + 1]; } es[es.length - 1] = null; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub MyStack<String> t = new MyStack<String>(); // t.push("abc"); t.push("def"); t.push("xyz"); t.push("uvw"); // t.push("hij"); System.out.println(t.pop()); System.out.println(t.peek()); } }
liumoujie3 2014-11-12
  • 打赏
  • 举报
回复
题目的意思,应该是让你自己写一个 Stack<E> 吧?
Game-Over 2014-11-11
  • 打赏
  • 举报
回复
引用 2 楼 cumtwyc 的回复:
Stack<E>中的E是类型参数,你这里需要的是Character ,这样Stack<Character> stack = new Stack<Character>(); main函数中 if (isBalanced(s)) ,s都没定义啊
可是他的要求是要用stack<E>啊。 s不都定义完了吗?int s = reader.nextInt();
wyc_ 2014-11-11
  • 打赏
  • 举报
回复
Stack<E>中的E是类型参数,你这里需要的是Character ,这样Stack<Character> stack = new Stack<Character>(); main函数中 if (isBalanced(s)) ,s都没定义啊
Game-Over 2014-11-11
  • 打赏
  • 举报
回复
Requirements 1. You must implement and use a Stack<E> class to solve this problem. Your class needs to implement the user-defined operations: push, pop, and peek. 2. Your Stack<E> class must be based on dynamic-linked lists as discussed in class (see Chapter 26). 3. Do not use fixed-length arrays, JDK Stack to implement your Stack class. 它的要求是上面这些。可是我不知道peek到底怎么用,还有stack<E>为什么显示的我是错误啊?

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧