【问题标题】:How to implement the while loop in this piece of code?如何在这段代码中实现while循环?
【发布时间】:2012-03-18 10:31:00
【问题描述】:

我的程序在我输入无效输入后结束。我知道我需要使用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.
        }
    }
}

【问题讨论】:

  • 标识你想要重复的代码块;代码块应该在循环体内。确定您希望循环在什么条件下结束,这应该是被评估的 bool 表达式。可能有人会提出答案,但构建循环是非常基本的东西,我认为值得花时间自己学习。如果你在一个小时内仍然无法弄清楚,并且没有人回答你的问题,那么我很乐意自己提交一个例子=)
  • 这与 Swing 没有任何关系。在将它们应用于问题之前,请仔细阅读标签。
  • 你已经问过类似的问题here你自己尝试过什么吗??

标签: java loops java.util.scanner


【解决方案1】:

只需围绕您想要继续执行的部分进行无限循环,直到您获得有效输入,如果您获得有效输入,则将其中断。

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 

【讨论】:

  • 上述代码在输入无效时会陷入无限循环。请检查。
  • 不,它不会因为从输入中读取是由 sc.nextDouble() 完成的。
【解决方案2】:

放双 r = sc.nextDouble();和 While 循环内的异常。

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

【讨论】:

    【解决方案3】:

    您宁愿在这里需要一个 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);
    
    
    
        }
    }
    

    【讨论】:

    • @user1190386 在 Java 中可能有其他处理异常的方法。我刚才给你看的,是一个例子。
    猜你喜欢
    • 1970-01-01
    • 2014-02-15
    • 2017-02-21
    • 1970-01-01
    • 2017-03-23
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    相关资源
    最近更新 更多