2016-04-14 23 views
1

Java için oldukça yeni olmak üzere, bu programı çeşitli para birimlerini hesaplamak için yazdım. Kullanıcıdan çıkmak istediği sürece işlemi tekrarlamak isteyip istemediğini kullanıcıya sormak istiyorum. Lütfen bana bu konuda yardımcı olun. Şimdiye kadar Kodum:Uygulamanın yapılması ve çıkılması

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

Peki sorun nerede? Zaten Java üzerinde biraz kavrayışınız var, bir döngüdeki sorun nedir? – Thomas

+0

Kullanıcıdan başka bir değer girmek istediklerini ve çıkıp çıkmadıklarını sormak için nerede ve nasıl yapılacağı konusunda kafam karıştı. Bu koddan bildiğim gibi dikişler olduğunu biliyorum. Ama kayboldum. –

cevap

0

Wrap tüm bir döngü içinde kod ve gerçek için başlatılır keepRunning gibi bir boolean değişken kullanın. Döngününüzün sonunda, kullanıcıya (tarayıcıdan giriş yapın) soruyorsunuz, sonra girişin "evet" veya "hayır" anlamına gelip gelmediğini (ya da başka bir ifadeyle doğru ve yanlış) kontrol ediyor ve buna göre keepRunning 'u ayarlıyorsunuz. Kullanıcı "hayır" seçerse yanlış.

Örnek:

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. 
} 

Alternatif olarak kullanımı while(true) ve break; kullanıcı "hayır" seçer.

Örnek:

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

Çok teşekkür ederim. Programı istediğiniz gibi tamamlayıp çalıştırabildim. İyi günler! –

+0

@ Try.Coder Çalıştığına sevindim ve birşeyler de öğrendiğini umuyorum :) - Btw, bir cevabı kabul etmeyi unutma. – Thomas

0

Merhaba aşağıdaki kodu

kontrol
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

kullanabilirsiniz iken kullanıcı gelen ÇIKIŞ o çıkış yazıldığında (doğru)

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)); 

      } 
     } 

    } 

olarak loop.You kullanıcı için bir seçenek olarak çıkmak zorunda.