作者: silzai 時間: 2008-11-25 20:13 標題: JAVA 功課
- int num;
- final int MAX = 5;
- double price1, price2, price3, price4, price5;
- String descript;
- String another = "y";
-
- while (another.equalsIgnoreCase("y"))
- {
-
- System.out.println("Starting new customer...");
- System.out.print("How many items are being purchased: ");
- num = sc.nextInt();
-
- while (num > MAX || num <=0)
- {
- System.out.println("Invalid input. Please enter again.");
- System.out.print("How many items are being purchased: ");
- num = sc.nextInt();
- }
-
- if (num > 0 && num <= MAX)
- {
- int count = 1;
- do
- {
-
- System.out.print("Enter description of product #" + count + ": ");
- descript = sc.next();
-
- System.out.print("Enter price of "+ descript +": ");
- price1 = sc.nextDouble();
- count++;
- }
- while (count <= num);
-
- double total = price1;
-
- System.out.println("\n-------------------------------");
- System.out.println(" The Greengrocery");
- System.out.println(" Transaction: " + num + "");
- System.out.println(" Total Amount Due: $" + total + "");
- System.out.println("-------------------------------");
- }
但係我每次loop都會overwrite左個price1
點算?
[ 本帖最後由 silzai 於 2008-11-25 20:14 編輯 ]
作者: DarkHero 時間: 2008-11-25 21:45
price1 = price1 + sc.nextDouble();
[ 本帖最後由 DarkHero 於 2008-11-25 21:47 編輯 ]
作者: astray 時間: 2008-11-25 21:48
將呢 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;
快靚正
作者: silzai 時間: 2008-11-25 22:32
唔該曬
[ 本帖最後由 silzai 於 2008-11-25 22:57 編輯 ]

