我想知道是否有人可以告诉我输入6种产品或/和6种数量后是否可以遍历一周中的每一天。因此,基本上该程序显示星期一,然后循环执行6次,然后转到周二循环6次,然后执行星期三。如果可以,我能得到一些想法吗???我真的很感谢您的帮助。我一直没有运气谷歌,那是因为我知道我不是问谷歌正确的问题。
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);
}
}
最佳答案
抱歉未格式化的文本,我从手机回复。
我认为您需要的是此运算符“%”
在while循环中,您可以增加计数
int count=0;
String currentDay="Monday";
String nextDay;
// your while loop start here
count++;
if(count%6 == 0)
{
switch(currentDay)
{
case "Monday": nextDay="Tuesday"; break;
//etc
}
currentDay = nextDay;
}
%运算符会提醒您除法。