如何在这段代码中实现while循环?

时间:2012-03-18 10:31:00

标签: java loops java.util.scanner

输入无效输入后,我的程序结束。我知道我需要使用while循环来让用户输入一个数字。

以下是代码:

import java.util.InputMismatchException;
import java.util.Scanner; 
public class AreaCircle { 
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner sc = new Scanner(System.in); // read the keyboard
        System.out.println("This program will calculate the area of a circle");
        System.out.println("Enter radius:");//Print to screen

        try {
            double r = sc.nextDouble(); // Read in the double from the keyboard
            double area = (3.14 *r * r);

            String output = "Radius: " + r + "\n";
            output = output + "Area: " + area + "\n";
            System.out.println("The area of the circle  is " + area);

        }
        catch( NumberFormatException e ) {
            System.out.println("Invalid Input, please enter a number");
            //put a message or anything you want to tell the user that their input was weird.
        }
        catch( InputMismatchException e )
        {

            System.out.println("Input Mismatch, please enter a number");
            //put a message or anything you want to tell the user that there is an 
            //input mismatch.
        }
    }
}

3 个答案:

答案 0 :(得分:0)

只需在要获得有效输入的情况下围绕要继续执行的部分进行无限循环,如果获得有效输入,则将其分解。

while (true){
  try {
    double r = sc.nextDouble(); // Read in the double from the keyboard
    double area = (3.14 *r * r);

    String output = "Radius: " + r + "\n";
    output = output + "Area: " + area + "\n";
    System.out.println("The area of the circle  is " + area);
    break;                              <---- New code that will break the loop

  }
  catch( NumberFormatException e ) {
    System.out.println("Invalid Input, please enter a number");
    //put a message or anything you want to tell the user that their input was weird.
  }
  catch( InputMismatchException e )
  {

    System.out.println("Input Mismatch, please enter a number");
    //put a message or anything you want to tell the user that there is an 
    //input mismatch.
  }
}
The execution will continue from here after break 

答案 1 :(得分:0)

put double r = sc.nextDouble();和while循环中的异常。

将自定义消息放在异常捕获块中,如“输入不正确请再次输入。如果循环仍然执行时发生异常。

答案 2 :(得分:0)

你宁愿在这里需要一个do-while循环。你使用do-while循环,其中循环中的代码块必须至少执行一次。这是你将如何实现它:

import java.util.InputMismatchException;
import java.util.Scanner; 
public class Circle { 
    public static void main(String[] args) {

          boolean invalid_input = false;    

        do
        {
        // TODO code application logic here
        Scanner sc = new Scanner(System.in); // read the keyboard
        System.out.println("This program will calculate the area of a circle");
        System.out.println("Enter radius:");//Print to screen

        try {
            double r = sc.nextDouble(); // Read in the double from the keyboard
            double area = (3.14 *r * r);

            String output = "Radius: " + r + "\n";
            output = output + "Area: " + area + "\n";
            System.out.println("The area of the circle  is " + area);
                invalid_input = false;

        }
        catch( NumberFormatException e ) {
              invalid_input = true;
            System.out.println("Invalid Input, please enter a number");
            //put a message or anything you want to tell the user that their input was weird.
        }
        catch( InputMismatchException e )
        {
                invalid_input = true;
            System.out.println("Input Mismatch, please enter a number");
            //put a message or anything you want to tell the user that there is an 
            //input mismatch.
        }
        }while(invalid_input== true);



    }
}