【发布时间】:2018-04-26 10:15:04
【问题描述】:
我在分配课程时遇到问题。我需要能够在输入某些数据后打印销售报告,并认为跟踪所有内容的最佳方法是使用 arrays。
我已经尝试了几个小时来解决这个问题,但我很难过。任何帮助将不胜感激。
供参考,用户需要输入:
- 员工姓名
- 年初至今的销售额
- 交易号
- 交易类型
- 交易金额
然后它应该循环回事务号并继续该循环,直到将值 0 作为事务号的输入。
然后它应该循环回员工姓名并继续循环,直到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();
}
好的,多亏了你们的帮助,我一直在努力,我已经取得了很大的进步。不过还是有一个问题。该数组仅保存为每个员工输入的最后一笔交易的交易编号、类型和金额,而不是每笔交易。
我认为错误在于我需要以不同于名称和 ytd 数组的速率迭代 tNum、类型和数量的数组?
还有一些麻烦,所以感谢任何帮助...这是我更新的代码以及最后的打印语句。
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 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);
}
【问题讨论】:
-
为您的索引 (i1 - i5) 提供有意义的名称(nameIndex、dateIndex、transactionIndex、typeIndex 等),以便您更好地跟踪它们。
-
while 循环在哪里?
-
你为什么要打破
i5循环?你也可以只使用if (i3 > 0)而不是 for 循环,因为它不会循环。 -
很难理解你真正想要实现的是什么。你能提供给你的实际作业文本吗?
-
这非常需要重新设计。
标签: java while-loop nested-loops