我基本上只是在用户输入零时才尝试结束程序,否则保持循环输入以进行处理。
我对发生的事情有疑问,每当我尝试编译它时,它会一直给我一个错误:
预期输出您的输出1 16 1线程中的异常 “main”java.util.InputMismatchException 2 Tom 69.28 2 at java.util.Scanner.throwFor(Scanner.java:909)3 Mickey 108.42 3 at java.util.Scanner.next(Scanner.java:1530)4 Juliet 2488.71 4 at java.util.Scanner.nextInt(Scanner.java:2160)5 Ann 2201.94 5 at java.util.Scanner.nextInt(Scanner.java:2119)6 Christy 894.14 6 at Bank.main(Bank.java:103)
public static void main(String[] args) {
// declare the necessary variables
double x;
int p;
double balance;
String name;
List<Person> list = new ArrayList<Person>();
// declare a Scanner object to read input
Scanner sc = new Scanner(System.in);
// read input and process them accordingly
x = sc.nextDouble();
p = sc.nextInt();
// while (sc.hasNext() == true)
String action;
// {
do {
action = sc.next();
//ArrayList list = new ArrayList();
if (action.equals("Create"))
{
do something
}
if (action.equals("Withdraw"))
{
{
do something
}
}
}
if (action.equals("Deposit"))
{
// output the result
}
if (action.equals("0"))
{
System.out.println(list.size());
for(Person d : list){
double bal = d.getBalance();
System.out.println(d.getName());
} }
}
while ( /*action != null &&*/ !action.isEmpty());
}
}
答案 0 :(得分:1)
由扫描程序抛出,表示检索到的令牌没有 匹配预期类型的模式,或者令牌不在 预期类型的范围。
查看你的stacktrace:
java.util.Scanner.nextInt(Scanner.java:2160)5 Ann 2201.94 5 at
这意味着你输入的不是整数。
p = sc.nextInt();