2013-05-13 37 views
1

,我終於完成(幾乎)我的Java菜單程序。但是,我無法讓我的返回更改函數在代碼的末尾工作。這是非常奇怪的數字。有任何想法嗎?經歷了漫長的艱苦工作後,雙打

代碼:

import java.io.*; 
import java.text.*; 
import java.util.*; 

public class JavaBurger 
{ 
    public static double amountowed = 0; 
    public static double amount; 
    public static double amount1 = 0; 
    public static double amount2 = 0; 
    public static double amount3 = 0; 
    public static double amount4 = 0; 
    public static double amount5 = 0; 
    public static double amount6 = 0; 
    public static double amount7 = 0; 
    public static double amount8 = 0; 
    public static double amount9 = 0; 
    public static double amount10 = 0; 


    static ArrayList MenuItems = new ArrayList(); 

    public static void main(String[] args) 
    { 
     InputStreamReader inp = null; 
     BufferedReader input = null; 
     int nOption = 0; 
    DecimalFormat x = new DecimalFormat("###.##"); 


     try 
     { 
      inp = new InputStreamReader(System.in); 
      input = new BufferedReader(inp); 
      while(true) 
      { 
     System.out.println("Choose a Menu Option"); 
       System.out.println("1. Burger - 13.49"); 
       System.out.println("2. Pasta - 16.79"); 
       System.out.println("3. Salad - 13.49"); 
       System.out.println("4. Salmon - 18.99"); 
       System.out.println("5. Chicken - 16.99"); 
       System.out.println("6. Nachos - 13.99"); 
     System.out.println("7. Soup - 6.99"); 
     System.out.println("8. Fajitas - 18.49"); 
     System.out.println("9. Ribs - 23.99"); 
     System.out.println("10. Calamari-9.99"); 
     System.out.println("11. Clear Order"); 
     System.out.println("12. Finish Order"); 
       System.out.println("\nChoose an option(1-12) >> "); 
     System.out.println("Subtotal: $" + x.format(amount)); 
     System.out.println("Total: $" + x.format(amount * 1.13)); 
     System.out.println("For error correction, choose an option and enter a negative value to void the item."); 

       nOption = Integer.parseInt(input.readLine()); 

       switch(nOption) 
       { 
        case 1: 
         Burger(input); 
         break; 
      case 2: 
         Pasta(input); 
         break; 
      case 3: 
         Salad(input); 
         break; 
      case 4: 
         Salmon(input); 
         break; 
      case 5: 
         Chicken(input); 
         break; 
        case 6: 
         Nachos(input); 
         break; 
        case 7: 
         Soup(input); 
         break; 
        case 8: 
         Fajitas(input); 
         break; 
        case 9: 
         Ribs(input); 
         break; 
        case 10: 
         Calamari(input); 
         break; 
      case 11: 
      Clear(input); 
      break; 
      case 12: 
      Finish(input); 
      break; 

       } 
      } 
     } 
     catch(Exception exp) 
     { 
     } 
    } 

    private static void Burger(BufferedReader input) throws IOException 
    { 

     while(true) 
     { 

      System.out.println("How many Burgers would you like? "); 
     int a = Integer.parseInt(input.readLine()); 

     double aaa = Math.pow(1 + a, a); 
      amount1 = (a * 13.49); 
     amount += amount1; 


       break; 
     } 
    } 

    private static void Pasta(BufferedReader input) throws IOException 
    { 
     while(true) 
     { 

      System.out.println("How many orders of Pasta would you like? "); 
     int b = Integer.parseInt(input.readLine()); 

     double bbb = Math.pow(1 + b, b); 
      amount2 = (bbb * 16.79); 
     amount += amount2; 


       break; 
     } 
    } private static void Salad(BufferedReader input) throws IOException 
    { 
     while(true) 
     { 

      System.out.println("How many Salads would you like? "); 
     int c = Integer.parseInt(input.readLine()); 

     double ccc = Math.pow(1 + c, c); 
      amount3 = (ccc * 13.49); 
     amount += amount3; 


       break; 
     } 
    } private static void Salmon(BufferedReader input) throws IOException 
    { 

     while(true) 
     { 

      System.out.println("How many orders of Salmon would you like? "); 
     int d = Integer.parseInt(input.readLine()); 

     double ddd = Math.pow(1 + d, d); 
      amount4 = (ddd * 18.99); 
     amount += amount4; 


       break; 
     } 
    } private static void Chicken(BufferedReader input) throws IOException 
    { 

     while(true) 
     { 

      System.out.println("How many orders of Chicken would you like? "); 
     int e = Integer.parseInt(input.readLine()); 

     double eee = Math.pow(1 + e, e); 
      amount5 = (eee * 16.99); 
     amount += amount5; 


       break; 
     } 
    } private static void Nachos(BufferedReader input) throws IOException 
    { 

     while(true) 
     { 

      System.out.println("How many orders of Nachos would you like? "); 
     int f = Integer.parseInt(input.readLine()); 

     double fff = Math.pow(1 + f, f); 
      amount6 = (fff * 13.99); 
     amount += amount6; 

       break; 
     } 
    } private static void Soup(BufferedReader input) throws IOException 
    { 
     while(true) 
     { 

      System.out.println("How many orders of Soup would you like? "); 
     int g = Integer.parseInt(input.readLine()); 

     double ggg = Math.pow(1 + g, g); 
      amount7 = (ggg * 6.99); 
     amount += amount7; 

       break; 
     } 
    } private static void Fajitas(BufferedReader input) throws IOException 
    { 

     while(true) 
     { 

      System.out.println("How many orders of 2 Fajitas would you like? "); 
     int h = Integer.parseInt(input.readLine()); 

     double hhh = Math.pow(1 + h, h); 
      amount8 = (hhh * 18.49); 
     amount += amount8; 

       break; 
     } 
    } private static void Ribs(BufferedReader input) throws IOException 
    { 

     while(true) 
     { 

      System.out.println("How many racks of Ribs would you like? "); 
     int i = Integer.parseInt(input.readLine()); 

     double iii = Math.pow(1 + i, i); 
      amount9 = (iii * 23.99); 
     amount += amount9; 

       break; 
     } 
    } private static void Calamari(BufferedReader input) throws IOException 
    { 

     while(true) 
     { 

      System.out.println("How many orders of Calamari would you like? "); 
     int j = Integer.parseInt(input.readLine()); 

     double jjj = Math.pow(1 + j, j); 
      amount10 = (jjj * 9.99); 
     amount += amount10; 

       break; 
     } 
    } private static void Clear(BufferedReader input) throws IOException 
    { 

    while(true) 
    { 

     amount = 0; 

     break; 
    } 
    } private static void Finish(BufferedReader input) throws IOException 
    { 

    while(true) 
    { 

     DecimalFormat x = new DecimalFormat("###.##"); 
     System.out.println("Amount Due"); 
     System.out.println("**********"); 
     System.out.println("Subtotal:" + x.format(amount)); 
     System.out.println("Total:" + x.format(amount * 1.13)); 
     System.out.println("Please enter the amount tendered"); 
     int k = Integer.parseInt(input.readLine()); 
     double kk = Math.pow(1 + k, k); 
     amountowed = ((amount * 1.13) - kk); 
     if(amountowed == 0) 
     { 
     System.out.println("Thanks for paying with exact change!"); 
     System.exit(0); 
     } 
     else if(amountowed < 0) 
     { 
     System.out.println("Change due:" + x.format(amountowed * -1.00)); 
     System.exit(0); 
     } 
     else 
     { 
     System.out.println("Amount still owed:" + x.format(amountowed * -1.00)); 
     } 
    } 
    } 
} 

結果:

Choose a Menu Option 
1. Burger - 13.49 
2. Pasta - 16.79 
3. Salad - 13.49 
4. Salmon - 18.99 
5. Chicken - 16.99 
6. Nachos - 13.99 
7. Soup - 6.99 
8. Fajitas - 18.49 
9. Ribs - 23.99 
10. Calamari-9.99 
11. Clear Order 
12. Finish Order 

Choose an option(1-12) >> 
Subtotal: $0 
Total: $0 
For error correction, choose an option and enter a negative value to void the it 
em. 
1 
How many Burgers would you like? 
1 
Choose a Menu Option 
1. Burger - 13.49 
2. Pasta - 16.79 
3. Salad - 13.49 
4. Salmon - 18.99 
5. Chicken - 16.99 
6. Nachos - 13.99 
7. Soup - 6.99 
8. Fajitas - 18.49 
9. Ribs - 23.99 
10. Calamari-9.99 
11. Clear Order 
12. Finish Order 

Choose an option(1-12) >> 
Subtotal: $13.49 
Total: $15.24 
For error correction, choose an option and enter a negative value to void the it 
em. 
12 
Amount Due 
********** 
Subtotal:13.49 
Total:15.24 
Please enter the amount tendered 
100 
Change due:270481382942152600000000000000000000000000000000000000000000000000000 
00000000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000 

爲什麼會出現古怪的號碼,由於這種變化?

+0

如何將其實施到我的代碼中?我不是很擅長編程......這讓我花了一個多月的時間去做,這只是我第一年編碼。 – Frisbee68 2013-05-13 14:01:51

+2

這不是浮點舍入錯誤,因爲對於包含double的簡單計算而言,在正確答案的10^15左右的範圍內可以給出答案,而不是完全無用的答案。 – 2013-05-13 14:03:06

+0

@Zutty在這種情況下,這會有什麼幫助?順便說一句,爲了保持平衡,我總是建議人們使用'double'來賺錢。 ;) – 2013-05-13 14:06:42

回答

10

那麼......下面這行是否需要這裏?

double kk = Math.pow(1 + k, k); 

它完全搞砸了變化計算,因爲你支持用戶輸入(k)+1到第k個功率。

如果您想將其更改爲雙倍,(double)k會做。當然,對於貨幣計算,你最好使用:

  1. 的BigDecimal
  2. 基於美分Integer/Long計算
+0

它將它變成雙倍,所以我可以在變化 – Frisbee68 2013-05-13 14:02:41

+3

(雙)k會得到小數。 – 2013-05-13 14:02:57

+0

@ user1965245在你的例子中計算'1010000000000'(和另一個90'0) – 2013-05-13 14:03:49

2

我掙扎明白爲什麼你過於複雜的改變計算這麼多..你不能簡單地計算amount tendered - total

+0

我不得不將第一個數字轉換爲第一個數字,否則它給了我錯誤 – Frisbee68 2013-05-13 14:03:46

+0

'total'是double類型的。算術推廣應該隱式地將'amount tendered'轉換爲double。 – christopher 2013-05-13 14:04:27

+0

你可以通過強制轉換來將某些東西加倍:(double)。我同意它不應該是無情的,但如果是這樣的話,這是簡單的方法。 – 2013-05-13 14:10:41

1
int e = Integer.parseInt(input.readLine()); 
double eee = Math.pow(1 + e, e); 

比方說,我想要5件事情。

double eee = Math.pow(1 + 5, 5) 

這是6 * 6 * 6 * 6 * 6(即非常大的數字)。我想你只是想要一個簡單的乘法?

1

如果您想要字符串中的雙精度型,請使用Double.parseDouble而不是Integer.parseInt。不要做額外的pow計算,這會讓你的結果完全錯誤。

更一般地說,拋出隨機代碼行很少是編譯器錯誤報告的好方法。理解正在發生的事情,並故意解決這個問題要好得多。

在某些情況下,您可能會遇到浮點舍入錯誤,並且會再次建議使用BigDecimal而不是double。

0

第一件事,第一......

  1. 你不需要不必要while循環(下面的代碼修正了)
  2. Math.pow(1 + A,A)不是必需的。(在下面固定代碼)
  3. amount1,量2等可以聲明爲局部變量(不低於代碼固定在),因爲它不危害
private static void Burger(BufferedReader input) throws IOException 
{ 
    System.out.println("How many Burgers would you like? "); 
    int a = Integer.parseInt(input.readLine()); 
    // double aaa = Math.pow(1 + a, a);// Why THis??? 
    amount1 = ((double)a * 13.49); 
    amount += amount1; 

} 

private static void Pasta(BufferedReader input) throws IOException 
{ 
    System.out.println("How many orders of Pasta would you like? "); 
    int b = Integer.parseInt(input.readLine()); 

    // double bbb = Math.pow(1 + b, b); You dont need this 
    amount2 = ((double)b * 16.79); 
    amount += amount2; 

} 
private static void Salad(BufferedReader input) throws IOException 
{ 
    System.out.println("How many Salads would you like? "); 
    int c = Integer.parseInt(input.readLine()); 

    //double ccc = Math.pow(1 + c, c); No Need of this again 
     amount3 = ((double)c * 13.49); 
    amount += amount3; 

    } 
} private static void Salmon(BufferedReader input) throws IOException 
{ 

    System.out.println("How many orders of Salmon would you like? "); 
    int d = Integer.parseInt(input.readLine()); 

    //double ddd = Math.pow(1 + d, d); No Need 
    amount4 = ((double)d * 18.99); 
    amount += amount4; 
} 
private static void Chicken(BufferedReader input) throws IOException 
{ 

    System.out.println("How many orders of Chicken would you like? "); 
    int e = Integer.parseInt(input.readLine()); 
    amount5 = ((double)e * 16.99); 
    amount += amount5; 

} 
private static void Nachos(BufferedReader input) throws IOException 
{ 

    System.out.println("How many orders of Nachos would you like? "); 
    int f = Integer.parseInt(input.readLine()); 

    amount6 = ((double)f * 13.99); 
    amount += amount6; 

} 
private static void Soup(BufferedReader input) throws IOException 
{ 
    System.out.println("How many orders of Soup would you like? "); 
    int g = Integer.parseInt(input.readLine()); 

    amount7 = ((double)g * 6.99); 
    amount += amount7; 

} 
private static void Fajitas(BufferedReader input) throws IOException 
{ 
    System.out.println("How many orders of 2 Fajitas would you like? "); 
    int h = Integer.parseInt(input.readLine()); 
    amount8 = ((double)h * 18.49); 
    amount += amount8; 

} 
private static void Ribs(BufferedReader input) throws IOException 
{ 
    System.out.println("How many racks of Ribs would you like? "); 
    int i = Integer.parseInt(input.readLine()); 
    amount9 = ((double)i * 23.99); 
    amount += amount9; 

} 
private static void Calamari(BufferedReader input) throws IOException 
{ 

    System.out.println("How many orders of Calamari would you like? "); 
    int j = Integer.parseInt(input.readLine()); 

    amount10 = ((double)j * 9.99); 
    amount += amount10; 

} 
private static void Clear(BufferedReader input) throws IOException 
{ 
    amount = 0; 

} 
private static void Finish(BufferedReader input) throws IOException 
{ 
amountowed =amount * 1.13 
DecimalFormat x = new DecimalFormat("###.##"); 

while(amountowed >0) 
{ 
    System.out.println("Amount Due"); 
    System.out.println("**********"); 
    System.out.println("Subtotal:" + x.format(amount)); 
    System.out.println("Total:" + x.format(amountowed)); 
    System.out.println("Please enter the amount tendered"); 
    double k = Double.parseDouble(input.readLine()); 
    // double kk = Math.pow(1 + k, k); No Need 
    amountowed = (amountowed - k); 
    if(amountowed == 0) 
    { 
     System.out.println("Thanks for paying with exact change!"); 
     System.exit(0); 
    } 
    else if(amountowed < 0) 
    { 
     System.out.println("Change due:" + x.format(amountowed * -1.00)); 
     System.exit(0); 
    } 
    else 
    { 
     System.out.println("Amount still owed:" + x.format(amountowed)); 
    } 

    } 
}