【发布时间】:2017-09-23 19:58:49
【问题描述】:
我正在尝试在 ecplise 中创建一个简单的数组表。 3 列 3 行。第 1 和第 2 列是字符串,第 3 列是双精度数。如果他犯了错误,我还希望用户可以选择重写当前行。
很简单吧?我收到一个我不明白的错误。有人可以向我解释一下吗?这是代码:
import java.util.Scanner;
public class exercise923
{
public static void main(String args[])
{
int counter = 0, check; //these are numbers used for the loops
String column1[] = new String[3]; //these are data for the table
String column2[] = new String[3];
double column3[] = new double[3];
Scanner Scan = new Scanner(System.in);
//this do while loop allows the user to enter data for the table
do {
System.out.println("type values for columns 1, 2, and 3 of line: " + counter);
column1[counter] = Scan.nextLine();
column2[counter] = Scan.nextLine();
column3[counter] = Scan.nextDouble();
//after typing out the data for this line, user can type 1 to go to the next line, or 2 to rewrite this line
System.out.println("type 1 to continue, 2 to rewrite");
check = Scan.nextInt();
if (check==1) {
counter++;
}
} while (counter<3);
//once user is finished typing out all the data for the 3 lines
//of 3 columns, the table gets printed out.
counter=0;
System.out.println("your finished table:");
do {
System.out.print(column1[counter] + "\t");
System.out.print(column2[counter] + "\t");
System.out.println(column3[counter]);
counter++;
} while (counter<3);
} // main
} // class
当我运行它时,我得到:
type values for columns 1, 2, and 3 of line: 0
jack
bob
3
type 1 to continue, 2 to rewrite
2
type values for columns 1, 2, and 3 of line: 0
dale
sally
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at exercise923.main(exercise923.java:21)
【问题讨论】:
-
这正是发生在你身上的事情:stackoverflow.com/a/5032408/3286487
-
我不明白,对不起
标签: java arrays string if-statement do-while