2014-02-11 109 views
-2

我試着做一個循環,但「{」符號防止所有的打印行重複我認爲。我希望循環在用戶完成輸入序列值後重新顯示整個第一個字符串。循環 - 做,而

import java.util.Scanner; 

public class FtoC{ 

    static Scanner cin = new Scanner (System.in); 

    public static void main(String[] args) 
    { 
     char userInput = ' '; 
     System.out.print("\nEnter " + "\"F\"" + " or " + "\"f\"" + " for Fahrenheit to Celsius" 
         + "\nEnter " + "\"C\"" + " or " + "\"c\"" + " for Celsius to Fahrenheit" 
         + "\nEnter something else to quit." +"\n"); 


     userInput = cin.next().charAt(0); 

     if ((userInput == 'F') || (userInput =='f')) 
     {print("Enter a temp in Fahrenheit" 
       +"\n I will tell you temp in Celsius" +"\n"); 
      far2cel(); 
     } 
     else if ((userInput == 'C') || (userInput =='c')) { 
      print("Enter a temp in Celsius:" 
      +"\n I will tell you temp in fahrenheit" +"\n"); 
       cel2far(); 
     } else { 
      print("Terminating the program" + "\n"); 
     } 
    } 

    private static void cel2far() { 
     Scanner input = new Scanner(System.in); 
     Double celsius = input.nextDouble(); 
     print(celsius + " celsius is " + ((celsius * 9/5.0) + 32) 
       + " Fahrenheit"); 

    } 

    private static void far2cel() { 
     Scanner input = new Scanner(System.in); 

     Double Fahrenheit = input.nextDouble(); 
     print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5/9.0)) 
       + " celsius"); 

    } 

    private static void print(String string) { 
     System.out.print("\n" + string); 
    } 
} 
+0

我沒有看到一個循環。你可以編輯這個來發布你試過的循環代碼嗎? – ajb

+3

您是否認真詢問了一個do-while循環,而您的代碼中沒有單個循環?試試這個:https://www.google.com/search?q=java+do+while&oq=java+do+while – aliteralmind

+1

「''符號可以防止所有打印行重複」呃,不。有時間閱讀舊的教程。 –

回答

0

我不認爲我得到你想要什麼,但這樣一來就可以運行該程序,直到用戶輸入的東西比「F」,「F」,「C」,「C」,在不同的該計劃的第一部分。

import java.util.Scanner; 

public class FtoC{ 

    static Scanner cin = new Scanner (System.in); 

    public static void main(String[] args) 
    { 
     while(true) { 
      char userInput = ' '; 
      System.out.print("\nEnter " + "\"F\"" + " or " + "\"f\"" + " for Fahrenheit to Celsius" 
          + "\nEnter " + "\"C\"" + " or " + "\"c\"" + " for Celsius to Fahrenheit" 
          + "\nEnter something else to quit." +"\n"); 

      userInput = cin.next().charAt(0); 

      if ((userInput == 'F') || (userInput =='f')) 
      {print("Enter a temp in Fahrenheit" 
        +"\n I will tell you temp in Celsius" +"\n"); 
       far2cel(); 
      } 
      else if ((userInput == 'C') || (userInput =='c')) { 
       print("Enter a temp in Celsius:" 
       +"\n I will tell you temp in fahrenheit" +"\n"); 
        cel2far(); 
      } else { 
       print("Terminating the program" + "\n"); 
       break; 
      } 
     } 
    } 

    private static void cel2far() { 
     Scanner input = new Scanner(System.in); 
     Double celsius = input.nextDouble(); 
     print(celsius + " celsius is " + ((celsius * 9/5.0) + 32) 
       + " Fahrenheit"); 

    } 

    private static void far2cel() { 
     Scanner input = new Scanner(System.in); 

     Double Fahrenheit = input.nextDouble(); 
     print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5/9.0)) 
       + " celsius"); 

    } 

    private static void print(String string) { 
     System.out.print("\n" + string); 
    } 
} 

我已將它放入while循環並添加「break」在「終止程序」部分。

+0

謝謝。有時最簡單的解決方案是最好的。我一直試圖做一個「儘可能」的聲明,但那是行不通的。 – mo1090

+0

沒問題,我很高興它幫助你!感謝您選擇我最好的答案,我真的很感激它。 – msmolcic