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!!");
}
}
Dies ist der Java-Code für die Berechnung von Benutzer für die Eingabe zu fragen. Wie kann ich die gleiche Zahl für alle Berechnungen in diesem Code verwenden? Ich frage Benutzer jedes Mal nach der neuen Nummer, aber ich möchte, dass dieser Code die gesamte Berechnung mit nur einer Eingabe durchführt.Wie kann ich die gleiche Nummer für alle Berechnungen verwenden
erfahren Sie über statische Schlüsselwort –