【问题标题】:correct way to check user input with scanner使用扫描仪检查用户输入的正确方法
【发布时间】:2023-03-17 19:33:01
【问题描述】:

我对编程非常陌生,我已经编写了一个应用程序来处理特定形状的区域。

如果需要,我可以在另一个班级提供AreaCalculations,但效果很好。

我的问题是检查用户何时键入字符而不是双精度字符。正如您从我的代码中看到的那样,我通过使用 while 循环和 (!reader.NextDouble()) 让它工作。

这可行,但我必须重复这个问题。我可以在整个程序中这样做,但是有没有更简单/更整洁的方法呢???

谢谢,

克雷格

到目前为止我的代码:

package areaprog;

import java.util.Scanner;

public class Mainprog {


public static void main (String [] args){

    //Area Menu Selection
    System.out.println("What shape do you need to know the area of?\n" +
    "1: Square?\n" +
    "2: Rectangle?\n" +
    "3: Triangle?\n" +
    "4: Circle? \n" +
    "5: Exit\n"     
    );

    //User input for menu

    Scanner reader = new Scanner(System.in);
    System.out.println("Number: ");

    //Menu syntax checking
    while (!reader.hasNextDouble())
    {
        System.out.println("Thats not a number you tool.\n");
        System.out.println("Now pick again\n" +
                "1: Square?\n" +
                "2: Rectangle?\n" +
                "3: Triangle?\n" +
                "4: Circle? \n" +
                "5: Exit\n"     
                );

        reader.next(); //ask for next token?        
    }               
        double input = reader.nextDouble();
        reader.nextLine();



    //Depending on user selection, depends on what method is called using switch.
Scanner scan = new Scanner(System.in);

    //Square selection
    if (input == 1){
        System.out.println("What is a length of 1 side of the Square?\n");
            double s1 = scan.nextDouble();
            double SqAns = AreaCalculator.getSquareArea(s1);
            System.out.println("The area of you square is: " + SqAns);

                   }

    //Rectangle selection    
        if (input == 2){
        System.out.println("What is the width of your rectangle?.\n");
            double r1 = scan.nextDouble();
        System.out.println("What is the height of your rectangle?\n");
            double r2 = scan.nextDouble();
            double RecAns = AreaCalculator.getRectArea(r1, r2);
        System.out.println("The area of your rectangle is: " + RecAns);    
        }
    //Triangle selection
    if (input == 3){
        System.out.println("What is the base length of the triangle?.");
            double t1 = scan.nextDouble();
        System.out.println("What is the height of your triangle?");
            double t2 = scan.nextDouble();
            double TriAns = AreaCalculator.getTriArea(t1, t2);
        System.out.println("The area of your triangle is " + TriAns);
    }
    //Circle selection
    if (input == 4){
        System.out.println("What is the radius of your circle?.");
            double c1 = scan.nextDouble();
            double CircAns = AreaCalculator.getCircleArea(c1);
        System.out.println("The area of your circle is " + CircAns);    

    }
    //Exit application
    if (input == 5){
        System.out.println("Goodbye.");

    }


}

}

好的,所以我添加了一个异常来捕获错误。所以它现在在处理不使用整数的人的方式上更简洁了。

号码: 1 正方形的一条边的长度是多少?

一个 你为什么要变得聪明?使用整数。

但是程序刚刚结束......我如何让他们回到主菜单,甚至让他们在最后的努力中重新输入?

谢谢,

克雷格

【问题讨论】:

    标签: java syntax java.util.scanner


    【解决方案1】:

    就菜单驱动程序而言,它很好。但正如你所说: “这可行,但我必须重复这个问题。”

    请阅读Exception handling tutorial in java

    因此,您的代码将如下所示:

        try
       {
        System.out.println("Thats not a number you tool.\n");
        System.out.println("Now pick again\n" +
                "1: Square?\n" +
                "2: Rectangle?\n" +
                "3: Triangle?\n" +
                "4: Circle? \n" +
                "5: Exit\n"
                );
    
    
    input = reader.nextDouble();
     }
       catch(InputMismatchException err)
        {
      System.out.print(err.getMessage());
    
         }
    

    别忘了导入import java.util.InputMismatchExceptionimport java.util.*

    并尝试在try块之外声明变量,外面可能看不到。

    【讨论】:

      猜你喜欢
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 2022-01-07
      • 1970-01-01
      相关资源
      最近更新 更多