我想知道是否有人可以告訴我是否有可能在輸入6個產品輸入或/和6個輸入量的數據後輸入一週中的某幾天。所以基本上程序顯示星期一,然後循環6次,然後到週二循環6次,然後星期三。如果有可能我可以得到一些想法?我真的很感謝一些幫助。我沒有運氣Google搜索,這是因爲我知道我不是在問谷歌正確的問題。通過星期幾環路
import java.util.Scanner;
public class Mailorder {
public static void main(String[] args) {
//create a scanner
Scanner input = new Scanner(System.in);
//declare variables
double product1 = 3.75;
double product2 = 5.95;
double product3 = 8.75;
double product4 = 6.92;
double product5 = 8.75;
double product6 = 7.87;
double sum2 = 0;
int sum1 = 0;
double total = 0.00;
int product;
int quantity;
//Monday
System.out.print("Monday");
System.out.println();
//read in product #
System.out.print("Enter a product number: ");
product = input.nextInt();
//read in quantity sold
System.out.print("Enter quantity sold: ");
quantity = input.nextInt();
//keep reading data until the input is 0
while (quantity != -1) {
sum1 += quantity;
//switch case
switch (product)
{
case 1: total = product1 * quantity; break;
case 2: total = product2 * quantity; break;
case 3: total = product3 * quantity; break;
case 4: total = product4 * quantity; break;
case 5: total = product5 * quantity; break;
case 6: total = product6 * quantity; break;
}
sum2 +=total;
//read the next data
System.out.print("Enter a product number: ");
product = input.nextInt();
System.out.print("Enter quantity sold: ");
quantity = input.nextInt();
}
//print results
System.out.println("The total retail value of all products sold last week $" + sum2);
}
}
您的單詞「loop」正處於正確的軌道上。嘗試搜索「java loop」。 – VGR
如果它不在你的頭上,你可能會從使用枚舉中受益。 – user1803551
您可以使用一個更多來自用戶的輸入,不管它的星期一或星期二等等,然後循環將像現在一樣工作 – Abhishekkumar