如果我想在用户输入 'n/N' 后终止代码,我应该如何在此代码中编写 'else' 语句?
How should I write 'else' statement in this code if I want to terminate the code after user gives input as 'n/N'?
I am trying to perform arithmetic operations by taking numbers as user input,I want to run the code continuously till user says no.
How should I write 'else' statement in this code if I want to terminate the code after user gives input as 'n/N'?
import java.util.*;
class FirstExample{
static void arithmetic(){
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("\nDo u want to perform the arithmetic operations? (Y/N): ");
String input = sc.nextLine();
if(input=="y"||input=="Y")
sc.nextLine();
System.out.println("Enter first number: ");
double first = sc.nextDouble();
System.out.println("Enter second number: ");
double second = sc.nextDouble();
System.out.println("Addition of the two number is: "+(first+second));
System.out.println("Subtraction of the two number is: "+(first-second));
System.out.println("Multiplication of the two number is: "+(first*second));
System.out.println("Division of the two number is: "+(first/second));
sc.nextLine();
}
}
public static void main(String[]args){
FirstExample.arithmetic();
}
}
```
//else{
// System.exit(0);
// }
//tried adding this block of code but it gets terminated even after giving 'y/Y'
即使您输入 Y 它也会退出的原因是比较 String
s 和 ==
将不起作用,因为它比较的是对字符串的引用,而不是字符串中的字符。相反,使用 .equals 方法:
if(input.equals("y") || input.equals("Y"))
一个可能的解决方案是引入一个布尔标志。因此,与其使用 while (true)
,还可以将其重构为:
class FirstExample {
static void arithmetic() {
Scanner sc = new Scanner(System.in);
boolean shouldStop = false;
while(!shouldStop){
System.out.println("\nDo u want to perform the arithmetic operations? (Y/N): ");
String input = sc.nextLine();
if ("n".equalsIgnoreCase(input)) {
shouldStop = true;
} else if ("y".equalsIgnoreCase(input)) {
sc.nextLine();
System.out.println("Enter first number: ");
double first = sc.nextDouble();
System.out.println("Enter second number: ");
double second = sc.nextDouble();
System.out.println("Addition of the two number is: "+(first+second));
System.out.println("Subtraction of the two number is: "+(first-second));
System.out.println("Multiplication of the two number is: "+(first*second));
System.out.println("Division of the two number is: "+(first/second));
sc.nextLine();
}
}
}
}
另外,还有两条评论(有些已经指出):
- 使用
equals(String)
或 equalsIgnoreCase(String)
(不区分大小写)代替 ==
。 equalsIgnoreCase
上的示例可以在 here. 中找到
- 您可以在字符串文字而不是变量上调用
equals()
(或 equalsIgnoreCase()
);这是为了避免空指针异常。因此,如果 input
是 null
,您可以 "n".equalsIgnoreCase(input)
而不是 input.equalsIgnoreCase("n)
。你可以阅读更多 on this SO thread
if (!input.equalsIgnoreCase("y")) {
break;
}
这里有一些东西。
- 首先,如前所述,使用 .equals() 或 .equalsIgnoreCase() 比较 java.
中的字符串
- 其次,摆脱 while 循环中断是这里最简单的事情。
- 第三,不需要 else 语句,因为您只对此时是否需要停止感兴趣 - 否则您可以继续您正在做的事情。
I am trying to perform arithmetic operations by taking numbers as user input,I want to run the code continuously till user says no. How should I write 'else' statement in this code if I want to terminate the code after user gives input as 'n/N'?
import java.util.*;
class FirstExample{
static void arithmetic(){
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("\nDo u want to perform the arithmetic operations? (Y/N): ");
String input = sc.nextLine();
if(input=="y"||input=="Y")
sc.nextLine();
System.out.println("Enter first number: ");
double first = sc.nextDouble();
System.out.println("Enter second number: ");
double second = sc.nextDouble();
System.out.println("Addition of the two number is: "+(first+second));
System.out.println("Subtraction of the two number is: "+(first-second));
System.out.println("Multiplication of the two number is: "+(first*second));
System.out.println("Division of the two number is: "+(first/second));
sc.nextLine();
}
}
public static void main(String[]args){
FirstExample.arithmetic();
}
}
```
//else{
// System.exit(0);
// }
//tried adding this block of code but it gets terminated even after giving 'y/Y'
即使您输入 Y 它也会退出的原因是比较 String
s 和 ==
将不起作用,因为它比较的是对字符串的引用,而不是字符串中的字符。相反,使用 .equals 方法:
if(input.equals("y") || input.equals("Y"))
一个可能的解决方案是引入一个布尔标志。因此,与其使用 while (true)
,还可以将其重构为:
class FirstExample {
static void arithmetic() {
Scanner sc = new Scanner(System.in);
boolean shouldStop = false;
while(!shouldStop){
System.out.println("\nDo u want to perform the arithmetic operations? (Y/N): ");
String input = sc.nextLine();
if ("n".equalsIgnoreCase(input)) {
shouldStop = true;
} else if ("y".equalsIgnoreCase(input)) {
sc.nextLine();
System.out.println("Enter first number: ");
double first = sc.nextDouble();
System.out.println("Enter second number: ");
double second = sc.nextDouble();
System.out.println("Addition of the two number is: "+(first+second));
System.out.println("Subtraction of the two number is: "+(first-second));
System.out.println("Multiplication of the two number is: "+(first*second));
System.out.println("Division of the two number is: "+(first/second));
sc.nextLine();
}
}
}
}
另外,还有两条评论(有些已经指出):
- 使用
equals(String)
或equalsIgnoreCase(String)
(不区分大小写)代替==
。equalsIgnoreCase
上的示例可以在 here. 中找到
- 您可以在字符串文字而不是变量上调用
equals()
(或equalsIgnoreCase()
);这是为了避免空指针异常。因此,如果input
是null
,您可以"n".equalsIgnoreCase(input)
而不是input.equalsIgnoreCase("n)
。你可以阅读更多 on this SO thread
if (!input.equalsIgnoreCase("y")) {
break;
}
这里有一些东西。
- 首先,如前所述,使用 .equals() 或 .equalsIgnoreCase() 比较 java. 中的字符串
- 其次,摆脱 while 循环中断是这里最简单的事情。
- 第三,不需要 else 语句,因为您只对此时是否需要停止感兴趣 - 否则您可以继续您正在做的事情。