2017-07-07 59 views
0

我对编码很陌生,希望你们能帮助我。 我有一个nextDouble没有等待print.ln收集数据的问题。 随着我的启动输入,我必须输入两个数字来移动到下一行,并要求输入第二个数字,因为它没有等待。 到了这一点,它也决定要求第三个数字,并接受该输入,但可以毫无问题地要求第三个和第四个数字。 重复时,它也发生在循环内部。扫描仪不等待NewLine收集循环输入

我知道使用nextDouble并不意味着它会“等待”换行符,而且我知道nextNumbertype没有换行符标志,但 在得到一个double之后加入“input.nextLine”似乎没有工作。

我知道我可以将所有输入作为字符串并稍后进行转换,但这是一项家庭作业,我的教授似乎想要一种非常特定的方式。

对于什么是错的或我可以尝试的想法?

这里是输出:

Enter x1 or <crtl + z> to quit 
1 
4 
Please input the x2 number: 
Please input the y1 number: 
1 
Please input the y2 number: 
5 
Distance is 5.000000 

Enter x1 or <crtl + z> to quit 

下面是实际的代码:

import java.util.Scanner; 

public class distanceFinder { 

public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 
    double x1 = 0; 

    System.out.println("Enter x1 or <crtl + z> to quit"); 
    x1 = input.nextDouble(); 


    while(input.hasNext()) 
    { 

    System.out.println("Please input the x2 number:"); 
    double x2 = input.nextDouble(); 

    System.out.println("Please input the y1 number:"); 
    double y1 = input.nextDouble(); 

    System.out.println("Please input the y2 number:"); 
    double y2 = input.nextDouble(); 

    double x3 = 0; 
    double y3 = 0; 
    double xy1 = 0; 

    x3 = Math.pow((x2 - x1), 2); 
    y3 = Math.pow((y2 - y1), 2); 

    xy1 = x3 + y3; 

    double distance = Math.sqrt(xy1); 

    System.out.printf("Distance is %.6f %n%n%n", distance); 

    System.out.println("Enter x1 or <crtl + z> to quit"); 
    x1 = input.nextDouble(); 

    } 
input.close(); 
} 

}

+0

问题是与'hasNext'按照javadcos *此方法可以在等待要扫描的输入框* –

回答

1

的问题是,Scanner.hasNext()阻塞,直到有输入。一旦你输入了一些东西,它就进入循环,执行第一个打印语句,抓取输入,打印第二个语句,并等待下一个输入。

另一个问题是,您在检查hasNext()之前,请先阅读x2而不是之前x1。尝试这种结构代替:

System.out.println("Enter x1 or <crtl + z> to quit"); 
while (input.hasNext()) { 
    double x1 = input.nextDouble(); 

    System.out.println("Please input the x2 number:"); 
    double x2 = input.nextDouble(); 

    //... 

    System.out.println("Enter x1 or <crtl + z> to quit"); 
} 
+0

非常感谢回复的人!它现在可以工作(除了正常的戒烟,但我会努力)。 –

+0

@ C.Miller查看我的更新。 – shmosel

0

您需要更正代码:

import java.util.Scanner; 

public class DistanceFinder { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     System.out.println("Enter x1 or <crtl + z> to quit"); 

     while (input.hasNext()) { 
      double x1 = input.nextDouble(); 

      System.out.println("Please input the x2 number:"); 
      double x2 = input.nextDouble(); 

      System.out.println("Please input the y1 number:"); 
      double y1 = input.nextDouble(); 

      System.out.println("Please input the y2 number:"); 
      double y2 = input.nextDouble(); 

      double x3 = 0; 
      double y3 = 0; 
      double xy1 = 0; 

      x3 = Math.pow((x2 - x1), 2); 
      y3 = Math.pow((y2 - y1), 2); 

      xy1 = x3 + y3; 

      double distance = Math.sqrt(xy1); 

      System.out.printf("Distance is %.6f %n%n%n", distance); 

      System.out.println("Enter x1 or <crtl + z> to quit"); 
     } 
     input.close(); 
    } 
} 

OUTPUT:

Enter x1 or <crtl + z> to quit 
1 
Please input the x2 number: 
2 
Please input the y1 number: 
1 
Please input the y2 number: 
3 
Distance is 2.236068 


Enter x1 or <crtl + z> to quit 
1 
Please input the x2 number: 
2 
Please input the y1 number: 
1 
Please input the y2 number: 
3 
Distance is 2.236068 


Enter x1 or <crtl + z> to quit 
+0

所以推CTRL-Z'输入X1或退出在 异常线程 “主” java.util.NoSuchElementException \t在java.util.Scanner.throwFor(Scanner.java:907) \t在java.util.Scanner.next(Scanner.java:1530) \t在java.util中。 Scanner.nextDouble(Scanner.java:2456) \t at Main.main(Main。Java的:22) ' –

+0

绕第二圈时也不起作用 - 基本上按使用'@ScaryWombat它为我hasNext'没有受益 –

+0

我的初步意见,我查了一些时间 –