我已经研究过这个问题,但找不到解决问题的答案。我继续得到输出0.对于这个赋值,我不允许使用任何方法。在我的程序中,1 + 2等于0
public static void main(String[] args) {
int result = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
int number = sc.nextInt();
System.out.println("Enter operation");
System.out.println("1.+");
System.out.println("2.-");
System.out.println("3.*");
System.out.println("4./");
System.out.println("5.=");
int operation = sc.nextInt();
while (operation != 5) {
System.out.println("Enter next number");
number = sc.nextInt();
System.out.println("Enter operation");
operation = sc.nextInt();
switch (operation) {
case 1:
result += number;
System.out.println("result= " + result);
break;
case 2:
result -= number;
System.out.println("result= " + result);
break;
case 3:
result *= number;
System.out.println("result= " + result);
break;
case 4:
result/ = number;
System.out.println("result= " + result);
break;
}
System.out.println(result);
}
答案 0 :(得分:0)
import java.lang.System;
import java.util.Scanner;
public class Java{
public static void main(String args[])
{
int result = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
result = sc.nextInt();
System.out.println("Enter operation");
System.out.println("1.+");
System.out.println("2.-");
System.out.println("3.*");
System.out.println("4./");
System.out.println("5.=");
int operation = sc.nextInt();
while (operation != 5) {
System.out.println("Enter next number");
int number = sc.nextInt();
switch (operation) {
case 1:
result += number;
System.out.println("result= " + result);
break;
case 2:
result -= number;
System.out.println("result= " + result);
break;
case 3:
result *= number;
System.out.println("result= " + result);
break;
case 4:
result /= number;
System.out.println("result= " + result);
break;
}
System.out.println(result);
System.out.println("Enter operation");
operation = sc.nextInt();
}
}
}
答案 1 :(得分:0)
对于您使用变量number的第一个数字和第二个数字,所以你将得到零。
答案 2 :(得分:0)
你的第一次行动没有被执行。
试试这个:
public static void main(String[] args) {
int result = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
result = sc.nextInt();
System.out.println("Enter operation");
System.out.println("1.+");
System.out.println("2.-");
System.out.println("3.*");
System.out.println("4./");
System.out.println("5.=");
int operation = sc.nextInt();
while (operation != 5) {
System.out.println("Enter next number");
int operand2 = sc.nextInt();
switch (operation) {
case 1:
result += operand2;
System.out.println("result= " + result);
break;
case 2:
result -= operand2;
System.out.println("result= " + result);
break;
case 3:
result *= operand2;
System.out.println("result= " + result);
break;
case 4:
result /= operand2;
System.out.println("result= " + result);
break;
}
System.out.println("Enter operation");
operation = sc.nextInt();
}
System.out.println("Global result = " + result);
}
}
答案 3 :(得分:0)
问题是你在循环的第一次迭代中读取number
和operation
两次而不是在切换情况之后移动两个读取条件,如下所示。
public static void main(String[] args) throws java.lang.Exception {
int result = 0;
Scanner sc = new Scanner(System. in );
System.out.println("Enter number");
int number = sc.nextInt();
System.out.println("Enter operation");
System.out.println("1.+");
System.out.println("2.-");
System.out.println("3.*");
System.out.println("4./");
System.out.println("5.=");
int operation = sc.nextInt();
try {
while (operation != 5) {
switch (operation) {
case 1:
result += number;
System.out.println("result= " + result);
break;
case 2:
result -= number;
System.out.println("result= " + result);
break;
case 3:
result *= number;
System.out.println("result= " + result);
break;
case 4:
result /= number;
System.out.println("result= " + result);
break;
}
System.out.println("Enter next number");
number = sc.nextInt();
System.out.println("Enter operation");
operation = sc.nextInt();
}
System.out.println("Final esult is " + result);
} catch (Exception e) {
System.out.println(e);
}
}
输出
Enter number 2
Enter operation 1
1.+
2.-
3.*
4./
5.=
result= 2
Enter next number 5
Enter operation 5
Final result is 2