简体   繁体   English

Java在不循环的同时做

[英]Java do while not looping

I'm sure this is going to be something dumb, but I cannot get my do while loop to work can someone take look please? 我确定这将是愚蠢的,但是我不能让while循环工作,有人可以看一下吗? I need the user to enter a number, then this is validated in a while loop. 我需要用户输入一个数字,然后在while循环中对此进行验证。 Then this number gets added to an arraylist and keeps looping until the user enters "-1". 然后,此数字将添加到arraylist中,并一直循环播放,直到用户输入“ -1”为止。 Here is my code: 这是我的代码:

public void enterValues(Scanner scan, ArrayList<Double> values) {

  do {
    System.out.print("Enter value to convert: £");
    while (!scan.hasNextDouble()) {
      System.out.println("Please enter a double");
      scan.nextLine();
    }

    values.add(scan.nextDouble());
    System.out.print("Value entered. Enter -1 to stop: ");
  }

  while (!scan.next().equals("-1"));

  System.out.println("Values entered. Returning to main menu.");
  mainMenu(scan, values);

This will work for you. 这将为您工作。

public void enterValues(Scanner scan, ArrayList<Double> values){
    Double dValue= (double) 0;
    do{
        System.out.println("Enter value to convert: £");
        dValue= scan.nextDouble();
        if(dValue != -1)
            values.add(dValue);
    }while(dValue != -1);

    System.out.println("Values entered. Returning to main menu.");
    mainMenu(scan, values);
}

This is similar to the other answer below, but a little more idiomatic. 这与下面的其他答案类似,但更为惯用。 There's no need to check the input value twice. 无需检查输入值两次。 Just check it once, and use break to exit the loop if it's negative. 只需检查一次即可,如果结果为负,请使用break退出循环。

   public void enterValues(Scanner scan, ArrayList<Double> values) {

        for( ;; ) {
            System.out.print("Enter value to convert: £");
            while (!scan.hasNextDouble()) {
                System.out.println("Please enter a double");
                scan.nextLine();
            }
            double input = scan.nextDouble();
            if( input < 0 ) break;
            values.add( input );
            System.out.println("Value entered. Enter -1 to stop: "+ values );
        }
   }

If you actually need "-1" and not just any negative number, note that it's poor practice to compare doubles with == . 如果您实际上需要“ -1”,而不仅仅是负数,请注意,将double与==进行比较是不明智的做法。 Use a tolerances with the difference. 使用公差与差异。 Also notice below that I supply my input with the program to make an SSCCE . 还要注意下面的内容,我在程序中提供输入以创建SSCCE

public class ScannerTest
{
   private static final String[] testVectors = {
      "123\n456\n890\n-1\n",
   };

   public static void main(String[] args) {
      for (int i = 0; i < testVectors.length; i++) {
         String testInput = testVectors[i];
         Scanner scan = new Scanner(testInput);
         ArrayList<Double> output = new ArrayList<>();
         new ScannerTest().enterValues( scan, output );
         System.out.println( output );
      }
   }

   public void enterValues(Scanner scan, ArrayList<Double> values) {

        for( ;; ) {
            System.out.print("Enter value to convert: £");
            while (!scan.hasNextDouble()) {
                System.out.println("Please enter a double");
                scan.nextLine();
            }
            double input = scan.nextDouble();
            if( compareDouble( input, -1.0, 0.00001 ) ) break;
            values.add( input );
            System.out.println("Value entered. Enter -1 to stop: "+ values );
        }
   }

   private boolean compareDouble( double d1, double d2, double tolerance ) {
      return Math.abs( d1 - d2 ) < tolerance;
   }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM