問題的答案:謝謝大家的幫助!如何添加我的循環中的所有結果
我在完成我的代碼時遇到了一些麻煩,主要是因爲我對編碼非常陌生,但仍然在努力。任何幫助是極大的讚賞!
我有3個問題:
- 我的主要問題是,我不知道如何讓我的代碼從每個環路添加所有的總數。
- 此外,循環開始後,它不會結束,當我輸入'0'時,但如果我結束循環,當我第一次運行循環它將工作。
- 最後,我如何使小數總數以這種格式顯示; xx.xx而不是xx.xxxxxxx?
預先感謝您,我真的很感激所有幫助
import java.util.Scanner;
public class takeOrders {//Find totals and average price
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int euro; // what country the candy canes are from
int us;// what country the candy canes are from
int holder; //place holder for user input of location
int v110 = 0; //110v
int v240 = 0; //240v
int sum = 0, i = 1;
double total = 0;
double discount = 0;
do {
//Prompt what country the order is for
System.out.println("What country is the order for? (press '0' to see the Net Total of order) ");
System.out.println("1: Europe\n2: U.S.");
holder = input.nextInt();
// 110 or 240 voltage
if (holder == 1) {
//How many boxes are ordered EUROPE
System.out.println("Input number of 240v boxes needed");
v240 = input.nextInt();
total = 2.40 * v240;
System.out.println("Order total: $" + total);
} else if (holder == 2) {
// How many boxes are ordered US
System.out.println("Input number of 110v boxes needed");
v110 = input.nextInt();
total = 2.40 * v110;
}
// Discount for U.S.
if (holder == 2) {
if (v110 >= 3)
discount = total * .05;
} else if (v110 >= 10) {
discount = total * .10;
}
if (discount > 0) {
System.out.println("Order total: $" + total);
System.out.println("Total with Discount: $" + (total - discount));
}
} while ((v240 != 0) || (v110 != 0));
}
}
給你的問題添加更多標籤 –