gpt4 book ai didi

java 异常 java.util.InputMismatchException

转载 作者:行者123 更新时间:2023-12-01 14:08:49 24 4
gpt4 key购买 nike

我基本上只在用户输入零时才尝试结束程序,否则继续循环输入进行处理。

我有一个关于发生了什么的问题,每当我尝试编译它时,它总是给我一个像这样的错误:

Expected Output Your Output 1 16 1 Exception in thread "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());
}
}

最佳答案

Docs

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

查看你的堆栈跟踪:

java.util.Scanner.nextInt(Scanner.java:2160) 5 Ann 2201.94 5 at

这意味着您输入的任何内容都不是整数。

p = sc.nextInt(); 

关于java 异常 java.util.InputMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18693884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com