2017-07-08 2 views
-1
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

+0

erfahren Sie über statische Schlüsselwort –

Antwort

0

Sie brauchen nur einmal die

first = num.nextDouble(); 
second = num.nextDouble(); 

nennen. Nennen Sie es, wo der Benutzer die Zahlen eingeben soll.

Für Ihr Beispiel, sollten Sie es auf diese ändern:

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!!"); 
    } 
} 
0

Wenn Sie

Pause entfernen;

von jedem Fall ist es von den einem jeden Fall tun wird der Benutzer wählte

Ist das, was Sie wollen zu beginnen?

+0

nein Ich möchte entscheiden, welche Berechnung es von mir selbst tun sollte, aber es sollte die gleichen Zahlen für jeden Fall nehmen, ich traf die Fallnummer. Zum Beispiel: wenn ich zwei Zahlen gebe, sollte die gleiche Zahl die Berechnung für jeden Fall durchführen –

Verwandte Themen