2014-03-27 53 views
0

編輯:只想對迄今爲止幫助過的所有人表示感謝!我是Java新手,我的教授推薦這個網站提供外部幫助。到目前爲止,這是非常棒的。我要花點時間研究代碼並實施我收到的建議。謝謝!不知道要使用哪個迴路

我正在嘗試創建一個程序,將溫度從華氏轉換爲攝氏度,反之亦然。如果用戶沒有輸入正確的單位,程序將循環返回,並要求他們重新輸入單位或輸入Q退出程序。這是我到目前爲止:

我的想法是將一個循環合併到我的switch語句的缺省部分,但我不確定使用哪個循環(可能用於?),它將循環回去並詢問用戶重新進入單位或Q退出。任何幫助真的,真的很感激!我已經堅持了幾個小時!

System.out.print("\tEnter 'F' (or 'f') for Fahrenheit or " 
      + "'C' (or 'c') for Celcius: "); 
f_or_c = console.next(); 

double fahrenheit = 5*(temperature-32)/9; 
double celcius = (9*(temperature)/5)+32; 

switch (f_or_c) { 
    case "C": 
     case "c": 
     System.out.println("\n\t" + temperature + " " + " degrees Celcius " 
     + "= " + celcius + " degrees Fahrenheit."); 
     break; 
    case "F": 
     case "f": 
     System.out.println("\n\t" + temperature + " " + " degrees Fahrenheit " 
     + "= " + fahrenheit + " degrees Celcius."); 
     break; 
    default: 
     System.out.println("\n\tUnknown units -" 
     + "\n\tCannot do calculation -" 
     + "\n\tPlease next time enter either 'F' for Fahrenheit or 'C' Celcius"); 
     System.out.print("\nEnter 'Q' to quit or " + 
    "any other character to perform another temperature conversion: "); 
     break; 
+0

你實際上正在尋找一種叫做['do-while']的東西(http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html)循環。我試着在我的答案中解釋一下它是如何工作的。 – MirroredFate

+0

嗨。真的很抱歉遲到的回覆。我能夠弄明白,並得到了代碼來執行我的教授想要使用嵌套while循環的方式。我確實看了一下你的do-while循環的例子,因爲它從來沒有傷害知道更多。我將嘗試使用do-while循環方法創建另一個副本。謝謝。 – user3470441

回答

0

你可以使用while循環。有更好的方法來做到這一點。但只是幫助你的代碼。

System.out.print("\tEnter 'F' (or 'f') for Fahrenheit or " 
     + "'C' (or 'c') for Celcius: "); 
f_or_c = console.next(); 
boolean flag=false; 
double fahrenheit = 5*(temperature-32)/9; 
double celcius = (9*(temperature)/5)+32; 
while(true) { 
    switch (f_or_c) { 
     case "C": 
     case "c": 
      System.out.println("\n\t" + temperature + " " + " degrees Celcius " 
      + "= " + celcius + " degrees Fahrenheit."); 
      flag=false; 
      break; 
    case "F": 
    case "f": 
      System.out.println("\n\t" + temperature + " " + " degrees Fahrenheit " 
      + "= " + fahrenheit + " degrees Celcius."); 
      flag=false; 
      break; 
    default: 
      System.out.println("\n\tUnknown units -" 
      + "\n\tCannot do calculation -" 
      + "\n\tPlease next time enter either 'F' for Fahrenheit or 'C' Celcius"); 
      System.out.print("\nEnter 'Q' to quit or " + 
      "any other character to perform another temperature conversion: "); 
      flag=true; 
      break; 
    } 
    if(flag) 
     continue; 
    break; 
} 
0

讓我們通過邏輯思考:直到用戶輸入正確的輸入爲止。那麼,這是否意味着在啓動循環之前你會知道循環應該迭代多少次?這是關鍵。

一旦你瞭解了這些信息,正確的循環掉出來,因爲:

  • for循環,當你事先知道有多少次,你會循環使用。
  • while循環適用於您事先不知道此信息的情況。
+0

是的,你是對的!我們不知道循環會循環多少次迭代。何時結束循環完全取決於用戶,因此一個while循環聽起來正確! 在switch語句的默認內嵌套while循環看起來明智嗎?或者,我應該創建while循環,並在其中嵌入switch語句? 謝謝! – user3470441

+0

@ user3470441:循環應包含您想要重複調用的任何代碼/邏輯。所以開關會進入while循環。 –

0

將switch語句放入方法中,然後在默認情況下調用該方法。它使用的是遞歸,但請記住,如果將來使用遞歸,請確保它不會無限循環,否則會發生堆棧溢出錯誤。

+0

我曾經想過要做些事情,但是我的教授希望我們提交一個.java文件,所以我認爲最好將它全部保存在這個程序中。謝謝你。 – user3470441

+0

他也提到單班,但多種方法 – spiderman

0

do{}while()怎麼樣?

boolean badInput; 
do { 

    badInput = false; 
    System.out.print("\tEnter 'F' (or 'f') for Fahrenheit or " 
       + "'C' (or 'c') for Celcius: "); 
    f_or_c = console.next(); 

    double fahrenheit = 5*(temperature-32)/9; 
    double celcius = (9*(temperature)/5)+32; 

    switch (f_or_c) { 
     case "C": 
      case "c": 
      System.out.println("\n\t" + temperature + " " + " degrees Celcius " 
      + "= " + celcius + " degrees Fahrenheit."); 
      break; 
     case "F": 
      case "f": 
      System.out.println("\n\t" + temperature + " " + " degrees Fahrenheit " 
      + "= " + fahrenheit + " degrees Celcius."); 
      break; 
     default: 
      System.out.println("\n\tUnknown units -" 
      + "\n\tCannot do calculation -" 
      + "\n\tPlease next time enter either 'F' for Fahrenheit or 'C' Celcius"); 
      System.out.print("\nEnter 'Q' to quit or " + 
     "any other character to perform another temperature conversion: "); 
      badInput = true; //we got bad input so we'll continue looping 
      break; 

} while(badInput); 

本質上講,我們設置badInput假之初,假定它是良好的輸入。如果我們最終輸入錯誤,我們將使badInput爲真,因此我們將繼續循環。

一個do-whilewhile環路之間的一般區別在於do-while s的使用要至少一次執行代碼。它在這種情況下效果很好,因爲您知道您至少要讀取一次輸入。

在我的示例中,變量badInput被稱爲flag。基本上,一個標誌只是一個跟蹤某個事物是「開」還是「關」的變量。在這種情況下,badInput正在跟蹤我們是否有錯誤的輸入。它是一個簡單的「開」,「關」,「真」,「假」情景。

0

我可能會改變你的思維方式,但我會以不同的方式實施。在開關盒周圍纏繞一段時間。這將確保在下一個用戶輸入值的情況下重新執行開關盒。我列舉了一個它可能看起來像什麼的例子。請注意,我沒有包含輸入代碼,因爲有很多選項可供選擇。另外,我沒有包含退出代碼,它只打破了循環。

bool repeat = TRUE; 
while (repeat) 
{ 
switch (f_or_c) 
{ 
    case "C": 
     case "c": 
     System.out.println("\n\t" + temperature + " " + " degrees Celcius " 
     + "= " + celcius + " degrees Fahrenheit."); 
     repeat = FALSE; 
     break; 
    case "F": 
    case "f": 
    System.out.println("\n\t" + temperature + " " + " degrees Fahrenheit " 
    + "= " + fahrenheit + " degrees Celcius."); 
    repeat = FALSE; 
    break; 
    case "Q": 
    ***YOUR CODE TO QUIT*** 
    repeat = FALSE; 
    break; 
    default: 
    System.out.println("\n\tUnknown units -" 
    + "\n\tCannot do calculation -" 
    + "\n\tPlease next time enter either 'F' for Fahrenheit or 'C' Celcius"); 
    System.out.print("\nEnter 'Q' to quit or " + 
    "any other character to perform another temperature conversion: "); 
    ***CODE TO PROMPT USER FOR F_OR_C VALUE*** 
    repeat = TRUE; 
    break; 
    } 
} 
+0

我真的想這樣做。另一位用戶指出,我意識到將交換機嵌套在while循環內可能更有效。謝謝!我將嘗試重寫代碼,以便交換機嵌套在這段時間內。 – user3470441

+0

圍繞switch語句打包while循環的建議工作順利!感謝大家! – user3470441

相關問題