【问题标题】:Do While Loop does not recognize scanner input before while loop is testedDo While 循环在测试 while 循环之前无法识别扫描仪输入
【发布时间】:2016-09-05 15:28:44
【问题描述】:
import java.util.Scanner;

public class Switch {


    private static void case1() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter side A");
        String a = input.next();

        System.out.println("Enter side B");
        String b = input.next();

        double number1 = Double.parseDouble(a);
        double number2 = Double.parseDouble(b);


        double A2 = number1 * number1;
        double B2 = number2 * number2;

        System.out.println(A2);
        System.out.println(B2);

        double Csq = A2 + B2;

        double C = Math.sqrt(Csq);

        System.out.println("C equals " + C);
        input.close();
    }

    private static void case2() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter side A");
        String a = input.next();

        System.out.println("Enter side C");
        String c = input.next();

        double number1 = Double.parseDouble(a);
        double number2 = Double.parseDouble(c);


        double A2 = number1 * number1;
        double C2 = number2 * number2;

        System.out.println(A2);
        System.out.println(C2);

        double b1 = C2 - A2;

        double B = Math.sqrt(b1);

        System.out.println("B equals " + B);
        input.close();
    }

    private static void case3() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter side B");
        String b = input.next();

        System.out.println("Enter side C");
        String c = input.next();

        double number1 = Double.parseDouble(b);
        double number2 = Double.parseDouble(c);


        double B2 = number1 * number1;
        double C2 = number2 * number2;

        System.out.println(B2);
        System.out.println(C2);

        double a1 = C2 - B2;

        double A = Math.sqrt(a1);

        System.out.println("A equals " + A);
        input.close();
    }


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String cont = "y";

        do {

            try{
                System.out.println("Welcome to the Pythagorean Theorm Program!!");
                System.out.println("");
                System.out.println("Choose one of the following:");
                System.out.println("");
                System.out.println("1. A2 + B2 = X");
                System.out.println("2. A2 + X = C2");
                System.out.println("3. X + B2 = C2");

                int choice = input.nextInt();

                switch (choice) {

                    case 1: case1();
                            break;
                    case 2: case2();
                            break;
                    case 3: case3();
                            break;
                }
            }
            catch(Exception e) {
                System.out.println("error");
            }

            System.out.println("Do you want to continue?");
            String more = input.next();
        } while (more.equals(cont));



    }
}

我需要在 while 循环之前进行用户输入,这样它就会知道程序是否要继续。问题是while 条件里面的更里面看不到扫描仪之前输入的一行。有什么建议吗?

【问题讨论】:

  • 你试过调试吗?
  • 不要在你的方法中调用input.close()。它将关闭您的输入流。
  • 它说更多没有解决。那是我的问题,它没有看到在前一行创建了更多作为输入字符串。

标签: java loops input java.util.scanner do-while


【解决方案1】:

将 String more 声明放在 while 循环之外:

`public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String cont = "y";
        String more = null;


        do {

            try{
                System.out.println("Welcome to the Pythagorean Theorm Program!!");
                System.out.println("");
                System.out.println("Choose one of the following:");
                System.out.println("");
                System.out.println("1. A2 + B2 = X");
                System.out.println("2. A2 + X = C2");
                System.out.println("3. X + B2 = C2");

                int choice = input.nextInt();

                switch (choice) {

                    case 1: case1();
                            break;
                    case 2: case2();
                            break;
                    case 3: case3();
                            break;
                }
            }
            catch(Exception e) {
                System.out.println("error");
            }

            System.out.println("Do you want to continue?");
            more = input.next();
        } while (more.equals(cont));



    }`

【讨论】:

    【解决方案2】:

    变量more 只在do while 循环的范围内。一种解决方案是在循环外声明更多内容,仅在内部更新其值。

    像这样……

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String cont = "y";
        String more = null;
    
        do {
            .
            .// code here
            .
    
            System.out.println("Do you want to continue?");
            more = input.next();
        } while (more.equals(cont));
    

    发生这种情况是因为在 do-while 构造中声明的任何变量都只在同一个循环内的范围内,不能在循环外或在循环条件下访问。

    在循环外和 main 内声明变量可以访问 main 方法内的任何位置。

    【讨论】:

      【解决方案3】:

      您的问题是变量范围,您在 do 语句中定义了更多的字符串,然后在它之外添加了 while 条件,要修复它,您需要将范围更改为 main 函数的本地,以执行此操作只需在执行之前将其添加到您的代码中

       public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          String cont = "y";
          String more = null;
          do {
          // rest of code
      

      你还需要去掉 "String" 关键字来定义更多的数据类型,但应该就是这样。

      尽管如果您的目标是在他们需要再次使用该程序时重复该程序,为什么不将整个 main 函数封装在 while 循环中,而条件取决于末尾的用户输入例如循环

      boolean more = True;
      While(more = True){
          // your current code
          // then check to see if they want to go again, if not , change more to false
      }
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-25
        • 2011-04-07
        • 1970-01-01
        • 2019-11-28
        • 1970-01-01
        • 2022-11-30
        • 1970-01-01
        • 2021-05-13
        相关资源
        最近更新 更多