Java嵌套while循环未按预期运行
我在课堂作业上遇到了麻烦。我需要能够在输入特定数据后打印销售报告,并认为跟踪所有内容的最佳方法是使用数组 几个小时来我一直在想这个问题,但我被难住了。任何帮助都将不胜感激 为了便于参考,用户需要输入:Java嵌套while循环未按预期运行,java,while-loop,nested-loops,Java,While Loop,Nested Loops,我在课堂作业上遇到了麻烦。我需要能够在输入特定数据后打印销售报告,并认为跟踪所有内容的最佳方法是使用数组 几个小时来我一直在想这个问题,但我被难住了。任何帮助都将不胜感激 为了便于参考,用户需要输入: 员工姓名 本年度迄今为止的销售额 交易号 交易类型 交易金额 然后它应该循环回事务编号,并继续该循环,直到将值0作为事务编号的输入 然后它应该循环回员工姓名,并继续循环,直到Done作为员工姓名的输入 下面是代码(我认为这是唯一相关的部分,但是如果您想看到整个代码,我可以发布它。) 再次感谢您的帮
Done
作为员工姓名的输入
下面是代码(我认为这是唯一相关的部分,但是如果您想看到整个代码,我可以发布它。)
再次感谢您的帮助和建议
void salesData() throws IOException {
for (int i = 0; i < 100; i++) {
System.out.print("Enter Name: ");
n = stdin.readLine();
if (n.equalsIgnoreCase("done")) {
break;
}
else {
System.out.print("Enter Transaction Number: ");
t = Integer.parseInt(stdin.readLine());
if (t == 0) {
break;
}
else {
System.out.print("Enter Transaction Type: ");
tp = stdin.readLine();
System.out.print("Enter Transaction Amount: ");
a = Double.parseDouble(stdin.readLine());
totSales = totSales + a;
totYtd = totYtd + a;
empTotal = empTotal + a;
empBonus = empBonus + (a * 0.05);
name[i] = n;
ytd[i] = y;
tNum[i] = t;
type[i] = tp;
amount[i] = a;
outputUpdate();
calcSalesData();
}
}
}
outputSalesData();
}
void salesData()引发IOException{
对于(int i=0;i<100;i++){
系统输出打印(“输入名称:”);
n=stdin.readLine();
如果(n.equalsIgnoreCase(“完成”)){
打破
}
否则{
系统输出打印(“输入交易编号:”);
t=Integer.parseInt(stdin.readLine());
如果(t==0){
打破
}
否则{
系统输出打印(“输入交易类型:”);
tp=stdin.readLine();
系统输出打印(“输入交易金额:”);
a=Double.parseDouble(stdin.readLine());
总销售额=总销售额+a;
totYtd=totYtd+a;
排空总量=排空总量+a;
empBonus=empBonus+(a*0.05);
名称[i]=n;
ytd[i]=y;
tNum[i]=t;
类型[i]=tp;
金额[i]=a;
outputUpdate();
calcSalesData();
}
}
}
outputSalesData();
}
好的,多亏你们的帮助,我一直在做这件事,我已经取得了很大的进步。不过还有一个问题。该数组仅保存为每个员工输入的上一笔交易的交易编号、类型和金额,而不是每笔交易
我相信错误在于我需要以不同于name和ytd数组的速率迭代tNum、type和amount数组
还是有点麻烦,所以非常感谢您的帮助。。。这是我的更新代码以及最后的print语句
void salesData() throws IOException {
for (int i = 0; i < 100; i++) {
System.out.print("Enter Name: ");
n = stdin.readLine();
if (n.equalsIgnoreCase("done")) {
outputSalesData();
}
System.out.print("Enter Year to Date Sales: ");
y = Double.parseDouble(stdin.readLine());
ytdSales = ytdSales + y;
totYtd = totYtd + ytdSales;
while (t != 0) {
System.out.print("Enter Transaction Number: ");
t = Integer.parseInt(stdin.readLine());
if (t == 0) {
t = 1;
empBonus = 0;
ytdSales = 0;
break;
}
else {
System.out.print("Enter Transaction Type: ");
tp = stdin.readLine();
System.out.print("Enter Transaction Amount: ");
a = Double.parseDouble(stdin.readLine());
totSales = totSales + a;
totYtd = totYtd + a;
ytdSales = ytdSales + a;
empTotal = empTotal + a;
empBonus = empBonus + (a * 0.05);
name[i] = n;
ytd[i] = y;
tNum[i] = t;
type[i] = tp;
amount[i] = a;
outputUpdate();
calcSalesData();
tCount++;
}
}
}
}
void salesData()引发IOException{
对于(int i=0;i<100;i++){
系统输出打印(“输入名称:”);
n=stdin.readLine();
如果(n.equalsIgnoreCase(“完成”)){
outputSalesData();
}
System.out.print(“输入年初至今的销售额:”);
y=Double.parseDouble(stdin.readLine());
ytdSales=ytdSales+y;
总销售额=总销售额+总销售额;
while(t!=0){
系统输出打印(“输入交易编号:”);
t=Integer.parseInt(stdin.readLine());
如果(t==0){
t=1;
EMP奖金=0;
ytdSales=0;
打破
}
否则{
系统输出打印(“输入交易类型:”);
tp=stdin.readLine();
系统输出打印(“输入交易金额:”);
a=Double.parseDouble(stdin.readLine());
总销售额=总销售额+a;
totYtd=totYtd+a;
ytdSales=ytdSales+a;
排空总量=排空总量+a;
empBonus=empBonus+(a*0.05);
名称[i]=n;
ytd[i]=y;
tNum[i]=t;
类型[i]=tp;
金额[i]=a;
outputUpdate();
calcSalesData();
t计数++;
}
}
}
}
这是印刷品:
void rptOut() {
System.out.println("");
System.out.println("--------------------------------------------");
System.out.println("Employee:\tYTD:\t\tT #:\tType:\tAmount:");
while (index < tCount)
{
System.out.println(name[index] + "\t\t$" + df2.format(ytd[index]) + "\t" + tNum[index] + "\t" + type[index] + "\t$" + amount[index]);
index++;
}
System.out.println("--------------------------------------------");
System.out.println("Total Food & Soft Drink Sales: \t$" + df2.format(totF));
System.out.println("Total Alcohol Sales: \t\t$" + df2.format(totA));
System.out.println("Total Sundries Sales: \t$" + df2.format(totS));
System.out.println("--------------------------------------------");
System.out.println("Total Sales for Day: \t$" + df2.format(totSales));
System.out.println("Total YTD: \t\t$" + df2.format(totYtd));
System.out.println("--------------------------------------------");
System.out.println("Highest Trans Amount: \t$" + df2.format(hiTrans));
System.out.println("Employee w/ Highest Trans: \t" + hiEmp);
System.out.println("--------------------------------------------");
//System.exit(0);
}
void rptOut(){
System.out.println(“”);
System.out.println(“--------------------------------------------------”);
System.out.println(“Employee:\tYTD:\t\tT#::\t类型:\tAmount:”;
while(索引
据我所知,您希望将销售报告的值存储到数组中,其中数组名称、数组ytd、数组tNum和数组类型都包含特定销售报告的值。现在,为了使用此概念,您需要确保索引0在整个镜像阵列中引用相同的销售报告数据
Sales Report 0 = {name[0], ytd[0], tNum[0], type[0]}
Sales Report 1 = {name[1], ytd[1], tNum[1], type[1]}
etc....
为此,可以使用单个for循环。试试下面的方法
void salesData() throws IOException {
for (int srIndex = 0; srIndex < 100; srIndex++)
{
System.out.print("Enter Name: ");
n = stdin.readLine();
name[srIndex] = n;
System.out.print("Enter Year to Date Sales: ");
y = Double.parseDouble(stdin.readLine());
ytd[srIndex] = y;
totYtd = totYtd + y;
System.out.print("Enter Transaction Number: ");
t = Integer.parseInt(stdin.readLine());
if (t == 0) {
break;
} else {
tNum[srIndex] = t;
}
System.out.print("Enter Transaction Type: ");
tp = stdin.readLine();
type[srIndex] = tp;
System.out.print("Enter Transaction Amount: ");
a = Double.parseDouble(stdin.readLine());
totSales = totSales + a;
totYtd = totYtd + a;
empTotal = empTotal + a;
empBonus = empBonus + (a * 0.05);
calcSalesData();
outputSalesData();
//ask to enter another sales report
System.out.print("Do you want to enter another Sales Report? (yes)");
String userInput = stdin.readLine();
if(!userInput.equalsIgnoreCase("yes"))
break;
}
}
为销售报告中的每个值创建一个方法是清理for循环中的代码的好方法
最后,我建议为您的销售报告创建一个类,并创建这些销售报告对象的容器。但我很高兴
public double getSalesReportTransaction()
{
System.out.print("Enter Transaction Amount: ");
return Double.parseDouble(stdin.readLine());
}
while(true)
{
System.out.print("Enter Transaction Number: ");
t = Integer.parseInt(stdin.readLine());
if (t == 0) {
break;
}
else {
System.out.print("Enter Transaction Type: ");
tp = stdin.readLine();
System.out.print("Enter Transaction Amount: ");
a = Double.parseDouble(stdin.readLine());
totSales = totSales + a;
totYtd = totYtd + a;
empTotal = empTotal + a;
empBonus = empBonus + (a * 0.05);
name[i] = n;
ytd[i] = y;
tNum[i] = t;
type[i] = tp;
amount[i] = a;
outputUpdate();
calcSalesData();
}
}