import java.util.Scanner;
public class Calculator
{
public static void main(String args[])
{
System.out.println("Enter the numbers only ");
Scanner num =new Scanner(System.in);
System.out.println("Enter the number of times you want to run loop ");
Scanner loop =new Scanner(System.in);
int n =loop.nextInt();
while(n!=0)
{
System.out.println("Enter the Cases 1 for addition,2 for Subtraction,3 for Division 4 for multipication and 5 for reaminder ");
Scanner cas =new Scanner(System.in);
int c = cas.nextInt();
double calc,first,second;
switch (c)
{
case 1:
System.out.println("addition of two number is ");
first = num.nextDouble();
second=num.nextDouble();
calc = first + second;
System.out.println(calc);
break;
case 2:
System.out.println("subtraction of two number is ");
first = num.nextDouble();
second = num.nextDouble();
calc = first - second;
System.out.println(calc);
break;
case 3:
System.out.println("division of two number is ");
first = num.nextDouble();
second = num.nextDouble();
calc = first / second;
System.out.println(calc);
break;
case 4:
System.out.println("multipication of two number is ");
first = num.nextDouble();
second = num.nextDouble();
calc = first * second;
System.out.println(calc);
break;
case 5:
System.out.println("remainder of two number is ");
first = num.nextDouble();
second = num.nextDouble();
calc = first % second;
System.out.println(calc);
break;
default:
System.out.println("you did not enter 1,2,3,4,5 you entered something else ");
}
n--;
}
System.out.println("the program has end, THANK YOU!!");
}
}
这是通过询问用户输入来计算的java代码。如何在此代码中对所有计算使用相同的数字?它每次都询问用户新的数字,但我希望此代码仅通过一个输入进行所有计算。
答案 0 :(得分:0)
您需要致电
first = num.nextDouble();
second = num.nextDouble();
只有一次。在您希望用户输入数字的位置调用它。
对于您的示例,您应将其更改为:
import java.util.Scanner;
public class Calculator
{
public static void main(String args[])
{
System.out.println("Enter the numbers only ");
Scanner num =new Scanner(System.in);
double first,second;
first = num.nextDouble(); //Get first number
second = num.nextDouble(); //Get second number
System.out.println("Enter the number of times you want to run loop ");
Scanner loop =new Scanner(System.in);
int n =loop.nextInt();
while(n!=0)
{
System.out.println("Enter the Cases 1 for addition,2 for Subtraction,3 for Division 4 for multipication and 5 for reaminder ");
Scanner cas =new Scanner(System.in);
int c = cas.nextInt();
double calc;
switch (c)
{
case 1:
System.out.println("addition of two number is ");
calc = first + second;
System.out.println(calc);
break;
case 2:
System.out.println("subtraction of two number is ");
calc = first - second;
System.out.println(calc);
break;
case 3:
System.out.println("division of two number is ");
calc = first / second;
System.out.println(calc);
break;
case 4:
System.out.println("multipication of two number is ");
calc = first * second;
System.out.println(calc);
break;
case 5:
System.out.println("remainder of two number is ");
calc = first % second;
System.out.println(calc);
break;
default:
System.out.println("you did not enter 1,2,3,4,5 you entered something else ");
}
n--;
}
System.out.println("the program has end, THANK YOU!!");
}
}
答案 1 :(得分:0)
如果删除
中断;
从每个案例开始,每个案例都会从用户选择的案例开始
这是你想要的吗?