我想知道是否有人可以告诉我,在输入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);
}
}
答案 0 :(得分:2)
使用内循环:
for(--- each day of the week ---)
// do something
for(int i=0; i<6; i++) {
// do something else
}
}
要查找一周中的几天(如何实现第一个循环),请查看课程日历:https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getActualMinimum(int)
int firstDay = Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_WEEK);
int lastDay = Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_WEEK);
答案 1 :(得分:2)
DayOfWeek
枚举 DayOfWeek
枚举定义七个对象,一周中的每一天,周一至周日编号为1-7。
EnumSet
是Set
的高度优化实现,用于收集枚举对象。
EnumSet<DayOfWeek> dows = EnumSet.allOf( DayOfWeek.class ); // Collect all seven day-of-week objects, in order Monday-Sunday.
循环设定。
for( DayOfWeek dow : dows ) {
String dowName = dow.getDisplayName( TextStyle.FULL , Locale.US ) ; // Generate localized string for the name of the day-of-week.
System.out.println( dowName ) ;
… // Ask for product code and do your math
}
答案 2 :(得分:1)
对于未格式化的文字,我很抱歉,我正在通过手机回复。
我认为你需要的是这个运算符&#34;%&#34;
在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;
}
%运营商会给你提醒分组。
答案 3 :(得分:0)
DayOfWeek枚举
DayOfWeek枚举定义了七个对象,一周中的每一天,周一至周日编号为1-7,可以更轻松地访问:
for(DayOfWeek day : DayOfWeek.values())
{
//Your code goes here
}