![](/img/trans.png)
[英]Exception in thread “main” java.util.InputMismatchException:
[英]Two Dimensional Arrays, Exception in thread “main” java.util.InputMismatchException
我有一个小问题。 我需要在一行中读取两个单词的put name和lastname。
public void Promedios5(){
Scanner Marbis=new Scanner(System.in);
String[] x=new String[5];
double[][] a=new double[5][4];
double[] b=new double [5],c=new double[5];
System.out.println("Este programa genera los promedios de las notas de cuatro unidades\n"
+ "se le solicitarán a usted, el nombre y las cuatro notas");
System.out.println("Podría ingresarlas ahora por favor:");
for(int y=0;y<=4;y++){
System.out.println("Ingrese el nombre:");
x[y]=Marbis.nextLine();
for(int z=0;z<=3;z++){
a[y][z]=Marbis.nextDouble();
}
b[y]=a[y][0]+a[y][1]+a[y][2]+a[y][3];
c[y]=b[y]/4;
}
System.out.println("Ahora usted verá los promedios de las personas:");
System.out.println("Nombre:\t\t\tPromedio");
for(int m=0;m<=4;m++)
System.out.printf("%s:\t\t%.2f\n",x[m],c[m]);
}
这里我有错误: x[y]=Marbis.nextLine();
我知道我在一行中用了两个或更多的单词,但是在第二次机会中它标记了我的错误,就像这样(这是结果,我认为我可以在nextLine中使用数组):
MArio Albert
100
100.00
78.00
100.00
Ingrese el nombre:
John Antoinie
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at vectormarbis1.MarbisVectors2.Promedios5(MarbisVectors2.java:125)
at vectormarbis1.VectorMarbis1.main(VectorMarbis1.java:28)
C:\Users\ManoloAurelio\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 39 seconds)
您只能用Java flush
输出。
什么时候丢弃其余的线? 要解决您的问题,您可以致电
input.nextLine();
您需要在nextDouble()
之后执行此操作,因为您希望从下一行读取。
我希望下面给出的代码可以帮助您解决问题。
public void Promedios5(){
Scanner Marbis=new Scanner(System.in);
String[] x=new String[5];
double[][] a=new double[5][4];
double[] b=new double [5],c=new double[5];
System.out.println("Este programa genera los promedios de las notas de cuatro unidades\n"
+ "se le solicitarán a usted, el nombre y las cuatro notas");
System.out.println("Podría ingresarlas ahora por favor:");
for(int y=0;y<=4;y++){
System.out.println("Ingrese el nombre:");
x[y]=Marbis.nextLine();
for(int z=0;z<=3;z++){
a[y][z]=Marbis.nextDouble();
}
Marbis.nextLine(); //Just add this line here
b[y]=a[y][0]+a[y][1]+a[y][2]+a[y][3];
c[y]=b[y]/4;
}
System.out.println("Ahora usted verá los promedios de las personas:");
System.out.println("Nombre:\t\t\tPromedio");
for(int m=0;m<=4;m++)
System.out.printf("%s:\t\t%.2f\n",x[m],c[m]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.