在测试While循环之前,Do While Loop无法识别扫描仪输入

泰勒·布尔
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条件的更多内部看不到扫描仪之前输入的行。有什么建议么?

Elgayed

将更多的String声明放在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));



    }`

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章