英文:
How can I get rid of the "wrong input " statement coming along my answer
问题
请检查为什么我的代码在正确的答案旁边输出了一个“错误的输入”语句,尽管我已经将它放在了else块内部...
import java.util.*;
public class Encapsulation {
public static void main(String args[]) throws InterruptedException {
Scanner sc = new Scanner(System.in);
System.out.print("选择操作(1:相加,2:相减):");
int choose = sc.nextInt();
int a, b;
System.out.println("输入第一个数字:");
a = sc.nextInt();
System.out.println("输入第二个数字:");
b = sc.nextInt();
choice(choose, a, b);
System.out.println("是否要再次计算(y/n):");
char select = sc.next().charAt(0);
if (select == 'y') {
main(args);
} else {
System.out.println("程序正在关闭.........");
Thread.sleep(1000);
System.out.println("程序已成功关闭");
}
sc.close();
}
public static int add(int a, int b) {
return a + b;
}
public static int diff(int a, int b) {
return a - b;
}
public static void choice(int choose, int a, int b) {
if (choose == 1) {
System.out.println("和 = " + add(a, b));
}
if (choose == 2) {
System.out.println("差 = " + diff(a, b));
}
}
}
这是终端窗口的消息。我想要删除这个与正确答案一起输出的“错误的输入”语句。
选择操作(1:相加,2:相减):1
输入第一个数字:
10
输入第二个数字:
10
和 = 20
是否要再次计算(y/n):
英文:
Please check why my code is giving a "wrong input" statement along with the correct answer. Though i have used it inside the else block...
import java.util.*;
public class Encapsulation {
public static void main(String args[]) throws InterruptedException {
Scanner sc = new Scanner(System.in);
System.out.print("Select operation(1:add, 2: sub): ");
int choose = sc.nextInt();
int a, b;
System.out.println("Enter 1st number: ");
a = sc.nextInt();
System.out.println("Enter 2nd number: ");
b = sc.nextInt();
choice(choose, a, b);
System.out.println("Do you want to calculate again(y/n): ");
char select = sc.next().charAt(0);
if (select == 'y') {
main(args);
} else {
System.out.println("Closing the program.........");
Thread.sleep(1000);
System.out.println("Program closed succesfully");
}
sc.close();
}
public static int add(int a, int b) {
return a + b;
}
public static int diff(int a, int b) {
return a - b;
}
public static void choice(int choose, int a, int b) {
if (choose == 1) {
System.out.println("The sum = " + add(a, b));
}
if (choose == 2) {
System.out.println("The diff = " + diff(a, b));
} else {
System.out.println("Wrong input!");
}
}
}
This is the message of terminal window.Please i want to remove this "Wrong input" coming along
Select operation(1:add, 2: sub): 1
Enter 1st number:
10
Enter 2nd number:
10
The sum = 20
Wrong input! //This is the problem i don't want this statement
Do you want to calculate again(y/n):
答案1
得分: 1
将您的choice
方法更改为以下内容:
public static void choice(int choose, int a, int b) {
if (choose == 1) {
System.out.println("The sum = " + add(a, b));
} else if (choose == 2) {
System.out.println("The diff = " + diff(a, b));
} else {
System.out.println("Wrong input!");
}
}
示例输出:
Select operation(1:add, 2: sub): 1
Enter 1st number:
10
Enter 2nd number:
10
The sum = 20
Do you want to calculate again(y/n):
英文:
Change your choice
method to this :
public static void choice(int choose, int a, int b) {
if (choose == 1) {
System.out.println("The sum = " + add(a, b));
} else if (choose == 2) { // <---- u shd use else-if here
System.out.println("The diff = " + diff(a, b));
} else {
System.out.println("Wrong input!");
}
}
Sample Output :
Select operation(1:add, 2: sub): 1
Enter 1st number:
10
Enter 2nd number:
10
The sum = 20
Do you want to calculate again(y/n):
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论