【发布时间】:2016-02-11 14:21:05
【问题描述】:
我很难在 Java 中使用 Scanner。
我尽力找出这段代码的错误,但我找不到。
我运行它然后程序允许我选择一个“代码”以及“单词”和“类别”(如果代码是 d)。当我尝试输入另一个代码时,程序进入一个无限循环。请大家指出我的错误可能在哪里?
import java.util.*;
public class GrammarChecker {
public static void main(String [ ] args)
{
Dicionary dicionary = new Dicionary();
Grammar grammar = new Grammar();
Item a;
String word, category, specification;
int count = 0;
char code;
Scanner entry = new Scanner(System.in);
System.out.println("What is the code?");
code = entry.nextLine().charAt(0);
switch(code)
{
case 'd':
System.out.println ("How many words your dicionary will have");
count = entry.nextInt();
entry.nextLine();
for(int i = 0; i < count; count--)
{
System.out.println ("What is the word?");
word = entry.nextLine();
System.out.println ("What is the category?");
category = entry.nextLine();
a = new Item (word, category);
dicionary.listWords(a);
}
break;
case 'g':
System.out.println ("How many lines are you going to use to specify your grammar?");
count = entry.nextInt();
entry.nextLine();
for(int j = 0; j < count; count--)
{
System.out.println ("What is the specification?");
specification = entry.nextLine();
grammar.listStructure(specification);
}
break;
case 'c':
System.out.println ("I only accept d ou g");
break;
case 'f':
System.out.println ("I only accept d ou g");
break;
}
System.out.println("Would you like to enter another code?");
code = entry.nextLine().charAt(0);
entry.nextLine();
}
}
【问题讨论】:
-
是字典吗?还是错字? :)
-
这是一个巴西的课堂作业,所以它是葡萄牙语的。我只是从葡萄牙语中直接翻译了这些词。
标签: java class oop methods bluej