如何结束while循环

时间:2017-01-21 20:13:31

标签: java

只要用户没有输入-99的输入,我的课程就会要求运行程序。当我运行程序并输入一个不可用的数字时,控制台将运行一个连续循环的答案,直到我必须按结束。 如何更改程序,以便每个输入都有一个答案,程序重新启动,直到用户输入-99?

import java.util.Scanner; //import scanner 
import java.io.*; //import library 

public class is_odd_or_even_number {//begin class

   public static void main(String []args) {//begin main

      Scanner input = new Scanner(System.in);

      //use try/catch method to test for invalid input
      try{
      //promt user to input a value 
      System.out.print("Enter a positive integer value: ");
      int number = input.nextInt();

      //PART I NEED HELP WITH **************
      while (number != -99){
      //Start of if statement to test which to print and call of isEven method
         if (isEven(number)) {
         System.out.println("Your number is Even!");  
         }
         else 
         System.out.println("Your number is Odd!");

         }
      }
      //open catch method and print invalid 
      catch(Exception notNumber) {
         System.out.println("Input not a number, try again.");
      }
   }
   //begin testing for odd or even in new method 
   public static boolean isEven(int num){
         return(num & 1) == 0;
      }
}

4 个答案:

答案 0 :(得分:4)

在这里,你不要让用户在循环之前输入第一个输入的其他内容。

检索用户的输入:

  int number = input.nextInt();

应该在循环中。

试试:

  int number = 0;

  //PART I NEED HELP WITH **************
  while (number != -99){
    number = input.nextInt();

    //Start of if statement to test which to print and call of isEven method
     if (isEven(number)) {
       System.out.println("Your number is Even!");  
     }
     else 
       System.out.println("Your number is Odd!");
     }
  }

答案 1 :(得分:4)

你可以这样做;)

System.out.print("Enter a positive integer value: ");
        int number = input.nextInt();

        //PART I NEED HELP WITH **************
        while (number != -99){
            System.out.print("Not good, please enter a new one : ");
            number = input.nextInt();
        }
        //Start of if statement to test which to print and call of isEven method
        if (isEven(number)) {
            System.out.println("Your number is Even!");  
        }
        else {
            System.out.println("Your number is Odd!");

        }

所以它会问你,直到你没有像你说的那样写-99但是如果你要求"一个积极的int"通常nobofy会写-99:p

答案 2 :(得分:0)

结束while循环

您可以使用布尔值shouldContinue来控制程序是否应该继续下一个输入。

if (number != -99) {
    shouldContinue = true;
} else {
    shouldContinue = false;
}

这可简化如下:

shouldContinue = number != -99 ? true : false;
// or even shorter
shouldContinue = number != -99;

正确读取值

但您需要确保在每次循环执行时重置输入数字,以便您可以读取下一个数字:

while (shouldContinue) {
    ...
    number = input.nextInt();
}

其他增强功能

  • 不要导入未使用的包或类
  • 使用camel case for Java class name
  • 对Javadoc
  • 使用评论样式/** ... */
  • 始终尽量避免无限循环,例如使用整数计数tries并在每个循环计数。

这里的最终答案如下:

import java.util.Scanner;

public class IsOddOrEvenNumber {

    public static void main(String []args) {

        Scanner input = new Scanner(System.in);
        boolean shouldContinue = true;
        int tries = 0;

        while (shouldContinue && tries < 10) {

            try {
                System.out.print("Enter a positive integer value: ");
                int number = input.nextInt();
                if (isEven(number)) {
                    System.out.println("Your number is Even!");
                } else {
                    System.out.println("Your number is Odd!");
                }
                shouldContinue = number != -99 ? true : false;
            } catch (Exception notNumber) {
                System.out.println("Input not a number, try again.");
            }
            tries--;
        }
        System.out.println("Game over.");
    }

    /**
     * Begin testing for odd or even in new method
     */
    public static boolean isEven(int num){
        return (num & 1) == 0;
    }
}

答案 3 :(得分:-1)

只要用户没有输入-99,就会运行main方法;

您应该在while循环中包含所有代码(甚至是try / catch)。

public static void main(String []args) {//begin main

  Scanner input = new Scanner(System.in);
  int number = 0;
  //Keep application running as long as the input is not -99
  while (number != -99){

     //use try/catch method to test for invalid input
     try{
        //promt user to input a value 
        System.out.print("Enter a positive integer value: ");
        number = input.nextInt();

        //Start of if statement to test which to print and call of isEven method
        //if the entered number is -99, the following code will skipped.
        if(number == -99) continue;

        if (isEven(number))
           System.out.println("Your number is Even!");  
        else 
           System.out.println("Your number is Odd!");
     }
     //open catch method and print invalid 
     catch(Exception notNumber) {
         System.out.println("Input not a number, try again.");
     }
  }
}

如果您正在寻找的话,您可以接受这个答案:)