2016-04-14 11 views
1

auf Java ziemlich neu zu sein, habe ich dieses Programm geschrieben haben verschiedene Währungen zu berechnen. Ich möchte den Benutzer fragen, ob er den Prozess wiederholen möchte, bis der Benutzer es beenden möchte. Bitte helfen Sie mir in dieser Angelegenheit. Mein Code so weit:Implementierung tun, während und Ausfahrt

public static void main(String[] args) { 


    Scanner input = new Scanner(System.in); 
    DecimalFormat formatter = new DecimalFormat("#0.00"); 

    String CType, c1 = "US", c2 = "EUR", c3 = "RM", c4 = "SAR"; 
    double CValue , US, EUR, RM, SGD, SAR; 


    System.out.println("Welcome to Bonus Calculator"); 

    System.out.println("Enter any of the following Currencies:"); 
    System.out.print("US\n" + 
        "EUR\n" + 
        "RM\n" + 
        "SGD\n" + 
        "SAR: "); 
    CType = input.next(); 



    if(CType.equalsIgnoreCase("US")){ 
    System.out.print("Enter Value: "); 
    CValue = input.nextInt(); 

     EUR = CValue * .88; 
     RM = CValue * 3.92; 
     SGD = CValue * 1.35; 
     SAR = CValue * 3.75; 

    System.out.print("US = " + formatter.format(CValue) + "EUR =" + formatter.format(EUR) + "RM =" + formatter.format(RM) + "SGD =" + formatter.format(SGD) + "SAR =" + formatter.format(SAR)); 

    } 

    else if(CType.equalsIgnoreCase("EU")){ 
     System.out.print("Enter Value: "); 
     CValue = input.nextInt(); 
     US = CValue * 1.14; 
     RM = US * 3.92; 
     SGD = US * 1.35; 
     SAR = US * 3.75; 

    System.out.print("EUR = " + formatter.format(CValue) + " US = " + formatter.format(US) + " RM = " + formatter.format(RM) +" SGD = " + formatter.format(SGD) +" SAR = " + formatter.format(SAR)); 

    } 

    else if (CType.equalsIgnoreCase("RM")){ 
     System.out.print("Enter Value: "); 
     CValue = input.nextInt(); 
     US = CValue * .26; 
     EUR = US * .88; 
     SGD = US * 1.35; 
     SAR = US * 3.75; 

    System.out.print("RM = " + formatter.format(CValue) + " US = " + formatter.format(US) + " EUR = " + formatter.format(EUR) + " SGD = " + formatter.format(SGD) + " SAR = " + formatter.format(SAR)); 

    } 

    else if (CType.equalsIgnoreCase("SGD")){ 
     System.out.print("Enter Value: "); 
     CValue = input.nextInt(); 
     US = CValue * 0.74; 
     EUR = US * .88; 
     RM = US * 3.92; 
     SAR = US * 3.75; 

    System.out.print("SGD = " + formatter.format(CValue) + " US = " + formatter.format(US) + " EUR = " + formatter.format(EUR) + " SAR = " + formatter.format(SAR) + " RM = " + formatter.format(RM)); 

    } 

    else if(CType.equalsIgnoreCase("SAR")){ 
     System.out.print("Enter Value: "); 
     CValue = input.nextInt(); 
     US = CValue * 0.39; 
     EUR = US * .88; 
     RM = US * 3.92; 
     SGD = US * 1.35; 

    System.out.print("SAR = " + formatter.format(CValue) + " US = " + formatter.format(US) + " EUR = " + formatter.format(EUR) + " SGD = " + formatter.format(SGD) + " RM = " + formatter.format(RM)); 

    } 

} 
+0

Also, wo ist das Problem? Sie haben Java bereits im Griff, was ist mit einer Schleife los? – Thomas

+0

Ich bin verwirrt darüber, wo und wie man den Benutzer fragen kann, ob er einen anderen Wert eingeben und beenden möchte, wenn nicht. Ich weiß aus dem Code sind Nähte wie sollte das wissen. Aber ich bin verloren. –

Antwort

0

Wrap den gesamten Code in einer Schleife und verwenden Sie eine boolean Variable wie keepRunning, die auf true initialisiert wird. Am Ende der Schleife fragen Sie den Benutzer (Eingang vom Scanner erhalten) Sie dann prüfen, ob die Eingabemittel „ja“ oder „nein“ (oder in anderen Begriffen wahr und falsch) und stellen keepRunning entsprechend (das heißt, Sie setzen ihn auf false, wenn der Benutzer "Nein" wählt.

Beispiel:

boolean keepRunning = true; 
while(keepRunning) { 
    //ask for input, calculate, print - the bulk of your code above 

    System.out.println("Would you like to do another? y/n"); 

    String userInput = ...; //get from scanner, I'll leave this as an excercise for you 
    keepRunning = "y".equalsIgnoreCase(userInput); //you might want to be more lenient here, e.g. also accept "yes" etc. 
} 

Alternativ verwenden while(true)break; und, wenn der Benutzer "nein" auswählt.

Beispiel:

while(true) { //keep running until we stop it from the inside 
    //same as above 

    ... 

    //break the loop if the user didn't select "y" 
    if(!"y".equalsIgnoreCase(userInput)) { 
    break; 
    } 
} 
+0

Vielen Dank. Ich konnte das Programm wie gewünscht abschließen und ausführen. Haben Sie einen guten Tag! –

+0

@ Try.Coder Ich bin froh, dass es funktioniert hat und hoffe, dass Sie auch etwas gelernt :) - Btw, vergessen Sie nicht, eine Antwort zu akzeptieren. – Thomas

0

Hallo überprüfen Sie den Code unten

public static void main(String args[]){ 
Scanner input = new Scanner(System.in); 
    DecimalFormat formatter = new DecimalFormat("#0.00"); 

    String CType, c1 = "US", c2 = "EUR", c3 = "RM", c4 = "SAR"; 
    double CValue , US, EUR, RM, SGD, SAR; 

    char choice='Y';//modified 
    System.out.println("Welcome to Bonus Calculator"); 
    do{//modified 
    System.out.println("Enter any of the following Currencies:"); 
    System.out.print("US\n" + 
        "EUR\n" + 
        "RM\n" + 
        "SGD\n" + 
        "SAR: "); 
    CType = input.next(); 



    if(CType.equalsIgnoreCase("US")){ 
    System.out.print("Enter Value: "); 
    CValue = input.nextInt(); 

     EUR = CValue * .88; 
     RM = CValue * 3.92; 
     SGD = CValue * 1.35; 
     SAR = CValue * 3.75; 

    System.out.print("US = " + formatter.format(CValue) + "EUR =" + formatter.format(EUR) + "RM =" + formatter.format(RM) + "SGD =" + formatter.format(SGD) + "SAR =" + formatter.format(SAR)); 
    } 
    System.out.println("\nDo you which to Continue if Yes press 'Y' other wise press 'N'");//modified 
    choice = input.next().toCharArray()[0];//modified 
}while(choice!='N');//modified 
} 
0

können Sie verwenden, während (true) als

public static void main(String[] args) { 


     Scanner input = new Scanner(System.in); 
     DecimalFormat formatter = new DecimalFormat("#0.00"); 

     String CType, c1 = "US", c2 = "EUR", c3 = "RM", c4 = "SAR"; 
     double CValue , US, EUR, RM, SGD, SAR; 


     System.out.println("Welcome to Bonus Calculator"); 
     while (true) { 

      System.out.println("Enter any of the following Currencies:"); 
      System.out.print("US\n" + 
        "EUR\n" + 
        "RM\n" + 
        "SGD\n" + 
        "SAR: "); 
      CType = input.next(); 
      if(CType.equalsIgnoreCase("EXIT")) 
       break; 


      if (CType.equalsIgnoreCase("US")) { 
       System.out.print("Enter Value: "); 
       CValue = input.nextInt(); 

       EUR = CValue * .88; 
       RM = CValue * 3.92; 
       SGD = CValue * 1.35; 
       SAR = CValue * 3.75; 

       System.out.print("US = " + formatter.format(CValue) + "EUR =" + formatter.format(EUR) + "RM =" + formatter.format(RM) + "SGD =" + formatter.format(SGD) + "SAR =" + formatter.format(SAR)); 

      } else if (CType.equalsIgnoreCase("EU")) { 
       System.out.print("Enter Value: "); 
       CValue = input.nextInt(); 
       US = CValue * 1.14; 
       RM = US * 3.92; 
       SGD = US * 1.35; 
       SAR = US * 3.75; 

       System.out.print("EUR = " + formatter.format(CValue) + " US = " + formatter.format(US) + " RM = " + formatter.format(RM) + " SGD = " + formatter.format(SGD) + " SAR = " + formatter.format(SAR)); 

      } else if (CType.equalsIgnoreCase("RM")) { 
       System.out.print("Enter Value: "); 
       CValue = input.nextInt(); 
       US = CValue * .26; 
       EUR = US * .88; 
       SGD = US * 1.35; 
       SAR = US * 3.75; 

       System.out.print("RM = " + formatter.format(CValue) + " US = " + formatter.format(US) + " EUR = " + formatter.format(EUR) + " SGD = " + formatter.format(SGD) + " SAR = " + formatter.format(SAR)); 

      } else if (CType.equalsIgnoreCase("SGD")) { 
       System.out.print("Enter Value: "); 
       CValue = input.nextInt(); 
       US = CValue * 0.74; 
       EUR = US * .88; 
       RM = US * 3.92; 
       SAR = US * 3.75; 

       System.out.print("SGD = " + formatter.format(CValue) + " US = " + formatter.format(US) + " EUR = " + formatter.format(EUR) + " SAR = " + formatter.format(SAR) + " RM = " + formatter.format(RM)); 

      } else if (CType.equalsIgnoreCase("SAR")) { 
       System.out.print("Enter Value: "); 
       CValue = input.nextInt(); 
       US = CValue * 0.39; 
       EUR = US * .88; 
       RM = US * 3.92; 
       SGD = US * 1.35; 

       System.out.print("SAR = " + formatter.format(CValue) + " US = " + formatter.format(US) + " EUR = " + formatter.format(EUR) + " SGD = " + formatter.format(SGD) + " RM = " + formatter.format(RM)); 

      } 
     } 

    } 

, wenn der Benutzer getippt EXIT es Ausgang aus dem Schleife. Sie müssen als Option für den Benutzer beenden.

Verwandte Themen