尝试理解 Exit、Loop、Method、If 和 Else [JAVA]

huangapple go评论77阅读模式
英文:

Trying to understanding Exit, Loop, Method and If and Else [JAVA]

问题

你好,以下是你的代码的翻译部分:

import java.util.Scanner;

public class Testing {

    int ans;
    boolean Loop = true;

    public void SimpleCalculation() {

        Scanner input = new Scanner(System.in);

        while (Loop) {
            System.out.println("[1] 计算 ");
            System.out.println("[2] 退出");
            System.out.print("您的选择: ");
            ans = input.nextInt();

            if (ans == 1) {
                System.out.print("输入第一个数字: ");
                int number1 = input.nextInt();

                System.out.print("输入第二个数字: ");
                int number2 = input.nextInt();

                int result = number1 + number2;
                System.out.println("总计: " + result);

            } else if (ans == 2) {
                System.out.println("谢谢您");
                input.close();
                break;
            } else {
                System.out.println("请只选择1或2");
            }
        }
        System.exit(0);
    }

    public static void main(String[] args) {

        Testing t = new Testing();
        t.SimpleCalculation();
    }
}

希望这对你有帮助。如果你需要进一步的帮助,请随时告诉我。

英文:

Hello guys I need help with my code. I am trying to understand how to use the method, loop,if-else statement, and exit code. So I'm writing a simple calculation base on the user choice but right now I can't figure out how to make the input read to loop back when user input else than the number (mean no alphabet are allowed) and it will continue back to the option till the user enter the right option that is either 1 or 2.

Do let me know if I make any mistake or is there a way to simplify this code more.

WANT OUTPUT TO BE LIKE THIS:-

> [1] Calculation
> [2] Exit
> Your choice: a
>
> Please choose only 1 or 2
>
> [1] Calculation
> [2] Exit
> Your choice: 1
>
> Enter 1st number: 1
> Enter 2nd number: 1
> Total: 2

CODE:-

import java.util.Scanner;
public class Testing {
int ans;
boolean Loop = true;
public void SimpleCalculation() {
Scanner input = new Scanner(System.in);
while (Loop) {
System.out.println("[1] Calculation ");
System.out.println("[2] Exit");
System.out.print("Your choice: ");
ans = input.nextInt();
if (ans == 1) {
System.out.print("Enter 1st number: ");
int number1 = input.nextInt();
System.out.print("Enter 2nd number: ");
int number2 = input.nextInt();
int result = number1 + number2;
System.out.println("Total: " + result);
} else if (ans == 2) {
System.out.println("Thank you");
input.close();
break;
} else {
System.out.println("Please choose only 1 or 2");
}
}
System.exit (0);
}
public static void main(String[] args) {
Testing t = new Testing();
t.SimpleCalculation();
}
}

答案1

得分: 2

我已经更新了你的代码:

public class Testing {

    public static void SimpleCalculation() {
        boolean Loop = true;

        Scanner input = new Scanner(System.in);

        while (Loop) {
            System.out.println("[1] 计算 ");
            System.out.println("[2] 退出");
            System.out.print("您的选择: ");
            while(!input.hasNextInt()) {
                System.out.println("请选择1或2");
                input.nextLine();
                continue;
            }
            int ans = input.nextInt();

            if (ans == 1) {
                System.out.print("输入第一个数字: ");
                int number1 = input.nextInt();

                System.out.print("输入第二个数字: ");
                int number2 = input.nextInt();

                int result = number1 + number2;
                System.out.println("总计: " + result);
            } else if (ans == 2) {
                System.out.println("谢谢您");
                input.close();
                break;
            } else {
                System.out.println("请选择1或2");
            }
        }

    }

    public static void main(String[] args) {
        SimpleCalculation();
    }

}

输出:

[1] 计算 
[2] 退出
您的选择: a
请选择1或2
[1] 计算 
[2] 退出
您的选择: 1
输入第一个数字: 1
输入第二个数字: 2
总计: 3
英文:

I have updated your code :

public class Testing {
public static void SimpleCalculation() {
boolean Loop = true;
Scanner input = new Scanner(System.in);
while (Loop) {
System.out.println("[1] Calculation ");
System.out.println("[2] Exit");
System.out.print("Your choice: ");
while(!input.hasNextInt()) {
System.out.println("Please choose only 1 or 2");
input.nextLine();
continue;
}
int ans = input.nextInt();
if (ans == 1) {
System.out.print("Enter 1st number: ");
int number1 = input.nextInt();
System.out.print("Enter 2nd number: ");
int number2 = input.nextInt();
int result = number1 + number2;
System.out.println("Total: " + result);
} else if (ans == 2) {
System.out.println("Thank you");
input.close();
break;
} else {
System.out.println("Please choose only 1 or 2");
}
}
}
public static void main(String[] args) {
SimpleCalculation();
}
}

Output :

[1] Calculation 
[2] Exit
Your choice: a
Please choose only 1 or 2
[1] Calculation 
[2] Exit
Your choice: 1
Enter 1st number: 1
Enter 2nd number: 2
Total: 3

huangapple
  • 本文由 发表于 2020年9月24日 01:43:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64033527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定