当前位置: 代码迷 >> J2SE >> 新手求解异常
  详细解决方案

新手求解异常

热度:58   发布时间:2016-04-24 12:29:54.0
新手求解错误
import java.util.Scanner;


public class cs {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
String s[]=new String[10];
double d[]=new double[5];
for(int j=0;j<3;j++)
{
System.out.print("输入购买的物品:");
s[j]=in.nextLine();
System.out.print("输入物品的单价:");
d[j]=in.nextDouble();
}
}

}

------解决方案--------------------
换成下面这样正常了
Java code
import java.util.Scanner;public class Cs {    public static void main(String[] args) {// TODO Auto-generated method stub        String s[]=new String[10];        double d[]=new double[5];        for(int j=0;j<3;j++)        {            Scanner in=new Scanner(System.in);            System.out.print("输入购买的物品:");            s[j]=in.nextLine();            System.out.print("输入物品的单价:");            d[j]=in.nextDouble();        }    }}
------解决方案--------------------
你这写就没有问题!

import java.util.Scanner;
public class cs {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
String s[] = new String[10];
double d[] = new double[5];
for (int j = 0; j < 3; j++) {
System.out.println("输入购买的物品:");
s[j] = in.nextLine();
System.out.println("输入物品的单价:");
d[j] = in.nextDouble();
}
}

}

------解决方案--------------------
Java code
import java.util.Scanner;public class cs {      public static void main(String args[]) {                        for ( int i=0 ; i<3 ; i++ )         {              Scanner in = new Scanner(System.in);              String s [] = new String [10];              double d [] = new double [5];              System.out.println("输入购买的物品:");              s[i] = in.nextLine();              System.out.println("输入物品的单价:");              d[i] = in.nextDouble();         }}}
  相关解决方案