2014-09-26 129 views
1

我是爱尔兰本地科技第一年的学生。我们正如你所猜测的那样使用java,而目前我们正在处理扫描器。我在底部添加了我的代码,并且有点困惑。扫描仪多输入问题

当我运行该程序时,它工作正常,直到我进入“输入国家”和“输入课程”,这两条线陆续打印在一起,无论是否将扫描仪更改为f.nextLine或f.next 。

任何人都可以帮助我,因为我失去了一些东西,我无法弄清楚它是什么。

非常感谢

马修

public static void main(String[] args) 
{ 
    String name; //declaring string name 
    String town; //declaring string town 
    double dist; //declaring double distance from AIT the person lives 
    int age; //declaring their age 
    double height; //declaring their height 
    double weight; //declaring their weight 
    String country; //declaring their country that they are from 
    String course; //declaring their course that they are taking 
    int years; //declaring the amount of years they are in the course 
    String sNum; //declaring there email address string 

    Scanner f = new Scanner(System.in); 
    //Scanner s = new Scanner(System.in); 
    // below is the prompt for all the information and there keyboard input with scanner 
    System.out.print("Enter your name here & press enter: "); 
    name = f.nextLine(); 

    System.out.print("Enter the name of the town or city you live in & press enter: "); 
    town = f.nextLine(); 

    System.out.print("Enter the distance from AIT that you live in kilometers & press enter: "); 
    dist = f.nextDouble(); 

    System.out.print("Enter your age & press enter: "); 
    age = f.nextInt(); 

    System.out.print("Enter your height & press enter: "); 
    height = f.nextDouble(); 

    System.out.print("Enter your weight & press enter: "); 
    weight = f.nextDouble(); 

    System.out.print("Enter the name of country that you are from & press enter: "); 
    country = f.nextLine(); 

    System.out.print("Enter the name of course you are taking & press enter: "); 
    course = f.nextLine(); 

    System.out.print("Enter the number of years the course is & press enter: "); 
    years = f.nextInt(); 

    System.out.print("Enter your student number & press enter: "); 
    sNum = f.next(); 

    //all the outputs of the information collected 
    System.out.println("Your name: "+name+" Town: "+town); 
    System.out.println(town+" is "+dist+" away from AIT."); 
    System.out.println("You are "+height+" meters tall & "+weight+" kgs & you are a "+country+" citizen."); 
    System.out.println("Your studying "+course+" for "+years+" years at AIT."); 
    System.out.println("Your student number is "+sNum+" & your student email address is   "+sNum+"@student.ait.ie"); 
    System.out.println("Please review the information."); 
} 
+0

请仔细阅读本之一: http://stackoverflow.com/questions/11551985/java-nextline-and-nextdouble-differences 您需要的国家之前多加一个“换行”读 – Hovav 2014-09-26 13:12:17

+0

如果有任何的答案解决了问题请接受一个(绿色选中标记),你也可以提出有用的答案。 – user1803551 2014-09-29 18:30:16

回答

0

nextDouble()扫描下一个令牌,并将其解析成一个双。这就是你面临这个问题的原因。您可以简单地将nextLine()移动到nextDouble()之上。它是解决问题的临时解决方案。

请尝试以下代码。我刚刚编辑了你的代码。

public static void main(String[] args) 
{ 
    String name; //declaring string name 
    String town; //declaring string town 
    double dist; //declaring double distance from AIT the person lives 
    int age; //declaring their age 
    double height; //declaring their height 
    double weight; //declaring their weight 
    String country; //declaring their country that they are from 
    String course; //declaring their course that they are taking 
    int years; //declaring the amount of years they are in the course 
    String sNum; //declaring there email address string 

    Scanner f = new Scanner(System.in); 
    //Scanner s = new Scanner(System.in); 
    // below is the prompt for all the information and there keyboard input with scanner 
    System.out.print("Enter your name here & press enter: "); 
    name = f.nextLine(); 

    System.out.print("Enter the name of the town or city you live in & press enter: "); 
    town = f.nextLine(); 

     System.out.print("Enter the name of country that you are from & press enter: "); 
    country = f.nextLine(); 

    System.out.print("Enter the name of course you are taking & press enter: "); 
    course = f.nextLine(); 

    System.out.print("Enter the number of years the course is & press enter: "); 
    years = f.nextInt(); 

    System.out.print("Enter your student number & press enter: "); 
    sNum = f.next(); 

    System.out.print("Enter the distance from AIT that you live in kilometers & press enter: "); 
    dist = f.nextDouble(); 

    System.out.print("Enter your age & press enter: "); 
    age = f.nextInt(); 

    System.out.print("Enter your height & press enter: "); 
    height = f.nextDouble(); 

    System.out.print("Enter your weight & press enter: "); 
    weight = f.nextDouble(); 



    //all the outputs of the information collected 
    System.out.println("Your name: "+name+" Town: "+town); 
    System.out.println(town+" is "+dist+" away from AIT."); 
    System.out.println("You are "+height+" meters tall & "+weight+" kgs & you are a "+country+" citizen."); 
    System.out.println("Your studying "+course+" for "+years+" years at AIT."); 
    System.out.println("Your student number is "+sNum+" & your student email address is   "+sNum+"@student.ait.ie"); 
    System.out.println("Please review the information."); 

    } 
    } 
0

如果使用

System.out.print("Enter the name of country that you are from & press enter: "); 
    country = f.next(); 
    System.out.print("Enter the name of course you are taking & press enter: "); 
    course = f.next(); 

它会奏效。你也可以为其他nextLine做这个。出于某种原因,您将学生号码读取为字符串而不是数字,但由于您将所有输入的字符串视为字符串,因此您可以使用next来表示所有内容。

编辑:

不要忘记调用f.close()关闭扫描仪。