2010-11-09 65 views
-1

我正在編寫一個Java貨幣兌換程序,其中我爲用戶編程3種外幣,以便用戶從美元兌換成外幣。我想打出來的while當用戶輸入「000」如何改善我的貨幣兌換計劃?

import java.lang.StringBuffer; 
import java.io.IOException; 

class Project6_5 
{ 
    public static void main(String args[]) 
    { 
     int usa, amount, stop; 
     System.out.println("This is a Currencies Exchange program"); 
     System.out.println("From US Dollar to Foreign Currency");  //Explaining to user 
     stop = 0; 
     while(true/*stop!=999*/) 
     { 
      try 
      { 
       System.out.print("What is your amount of US dollars?"); 
       System.out.println("      000 to end Program"); 
       usa = Integer.parseInt(GCS()); 
       System.out.println("What currency would you prefer?"); 
       System.out.println("Avaliable currencies are:"); 
       System.out.println("1 for Japanese Yen, 2 for Taiwanese Dollar, 3 for Euro"); 
       amount = Integer.parseInt(GCS()); 
       if(amount == 1) 
       { 
        System.out.println("Exchange rate"); 
        System.out.println("80.75 Yen for 1 Dollar"); 
        System.out.println(" "); 
        System.out.println("Your Yen is "+ usa*80.75); 
        System.out.println("________________________________________"); 
       } 
       if(amount == 2) 
       { 
        System.out.println("Exchange rate"); 
        System.out.println("30.125 NTD for 1 Dollar"); 
        System.out.println(" "); 
        System.out.println("Your NTD is "+ usa*30.125); 
        System.out.println("________________________________________"); 
       } 
       if(amount == 3) 
       { 
        System.out.println("Exchange rate"); 
        System.out.println("1.4201 Euro for 1 Dollar"); 
        System.out.println(" "); 
        System.out.println("Your Euro is "+ usa*1.4201); 
        System.out.println("________________________________________"); 
       } 
       if(usa==000) 
       { 
        System.out.println("End Program"); 
        return; 
       }  
      } 
      catch(NumberFormatException NFE) //catch if integer's not number 
      { 
       System.err.println("ERROR"); 
       System.err.println("Type in Integer only"); 
      } 
      catch(Exception E) //catch Genaral Error 
      { 
       System.err.println("ERROR"); 
      } 
     } 
    }//end of Main 
    public static String GCS() //GetConsoleString User-Input 
    { 
     int noMoreInput = -1; 
     char enterKeyHit = '\n'; 
     int InputChar; 
     StringBuffer InputBuffer = new StringBuffer(100); 

     try 
     { 
      InputChar = System.in.read(); 
      while(InputChar != noMoreInput) 
      { 
       if((char)InputChar != enterKeyHit) 
       { 
        InputBuffer.append((char) InputChar); 
       } 
       else 
       { 
        InputBuffer.setLength(InputBuffer.length()-1); 
        break; 
       } 
       InputChar = System.in.read(); 
      } 
     }//close Try(21) 
     catch(IOException IOX) 
     { 
      System.err.println(IOX); 
     } 
     return InputBuffer.toString(); 
    }//end of GCS 
} 
+1

我認爲你會陷入低估,因爲你沒有具體說明你認爲你的程序出了什麼問題。你需要什麼樣的建議? – John 2010-11-09 22:13:45

+0

我認爲標題是有爭議的。它確實應該是「當用戶鍵入某個值時如何退出程序」或類似的東西。 – Catchwa 2010-11-09 22:40:33

+0

請遵循[general](http://tinyurl.com/so-hints)問題[準則](http://meta.stackexchange.com/q/10812),陳述任何特殊的限制,顯示你所嘗試過的到目前爲止,並詢問具體是什麼令你困惑。 – 2010-11-10 00:47:01

回答

1

移動的代碼塊:

  if(usa==000) 
      { 
       System.out.println("End Program"); 
       return; 
      }   

略低於:

  usa = Integer.parseInt(GCS()); 

因爲要檢查用戶輸入000直後,他們打的回報。

而且我會建議更換與return

  System.exit(0); 

這說明你的明確意圖,退出程序,並認爲這是一個正常的終止。

+0

呃,$ 0仍然是一個有效的金額,所以寫出來的方式他可以在退出之前看到(也許是微不足道的)轉換。 :) – John 2010-11-09 22:11:47

+0

@約翰:好點。真正應該發生的是,「美元數額」最初是一個字符串,它通過「equals(String)」與退出代碼(「000」)進行比較,如果不匹配,則將其解析爲「 int'並將其分配給'usa'變量。 – Catchwa 2010-11-09 22:38:36

1

每當你看到自己用很少的差異不止一次編寫的代碼相同的塊,你應該重構該塊進入的方法。

從代碼:

if(amount == 1) 
{ 
    System.out.println("Exchange rate"); 
    System.out.println("80.75 Yen for 1 Dollar"); 
    System.out.println(" "); 
    System.out.println("Your Yen is "+ usa*80.75); 
    System.out.println("________________________________________"); 
} 
if(amount == 2) 
{ 
    //all the same as above except for 30.125 instead of 80.75 
    //and NTD instead of Yen 
} 
//... 

的方法應在這一改變的值,然後重複你的語句,輸出變量,而不是硬編碼的數字和貨幣類型。

需要注意的是,因爲你檢查之前,分析用戶的輸入到Integer你的程序不符合您的要求目前,讓Integer.parseInt計算結果爲0(不只是「000」)將導致你的程序由用戶輸入任意字串回來。