JAVA 功課

  1. int num;
  2. final int MAX = 5;
  3. double price1, price2, price3, price4, price5;
  4. String descript;
  5. String another =  "y";
  6.                        
  7. while (another.equalsIgnoreCase("y"))
  8. {
  9.                        
  10. System.out.println("Starting new customer...");
  11. System.out.print("How many items are being purchased: ");
  12. num = sc.nextInt();
  13.                        

  14. while (num > MAX || num <=0)
  15. {
  16. System.out.println("Invalid input. Please enter again.");
  17. System.out.print("How many items are being purchased: ");
  18. num = sc.nextInt();
  19. }
  20.                                
  21. if (num > 0 && num <= MAX)
  22. {
  23. int count = 1;
  24. do
  25. {
  26.                                        
  27. System.out.print("Enter description of product #" + count + ": ");
  28. descript = sc.next();
  29.                                
  30. System.out.print("Enter price of "+ descript +": ");
  31. price1 = sc.nextDouble();                       

  32. count++;
  33. }
  34. while (count <= num);
  35.                                
  36. double total = price1;                       
  37.                                
  38. System.out.println("\n-------------------------------");
  39. System.out.println("        The Greengrocery");
  40. System.out.println("        Transaction: " + num + "");
  41. System.out.println("        Total Amount Due: $" + total + "");
  42. System.out.println("-------------------------------");
  43. }
複製代碼
要計個total price出黎 (例如5件貨就要計5件既total price)
但係我每次loop都會overwrite左個price1
點算?

[ 本帖最後由 silzai 於 2008-11-25 20:14 編輯 ]

price1 = price1 + sc.nextDouble();

[ 本帖最後由 DarkHero 於 2008-11-25 21:47 編輯 ]

TOP

將呢 part

System.out.print("Enter price of "+ descript +": ");
price1 = sc.nextDouble();     

改成

System.out.print("Enter price of "+ descript +": ");
price1 += sc.nextDouble();     

仲有 declare price1 時變成 double price1 = 0d;

快靚正

TOP

原帖由 astray 於 2008-11-25 21:48 發表
仲有 declare price1 時變成 double price1 = 0d;

唔該曬

[ 本帖最後由 silzai 於 2008-11-25 22:57 編輯 ]

TOP