Java 嵌套的 while 循环未按预期运行
Java nested while loops not functioning as desired
我在分配 class 时遇到问题。我需要能够在输入某些数据后打印一份销售报告,并认为跟踪所有内容的最佳方法是使用 arrays。
几个小时以来,我一直在努力解决这个问题,但我很困惑。任何帮助将不胜感激。
作为参考,用户需要输入:
- 员工姓名
- 年初至今销售额
- 一个交易号
- 交易类型
- 交易金额
然后它应该循环回到交易编号并继续该循环,直到值 0 作为交易编号的输入。
然后它应该循环回到员工姓名并继续返回该循环,直到 Done
作为员工姓名的输入。
这是代码(我认为这是唯一相关的部分,但如果您想查看整段代码,我可以 post 它。)
再次感谢您的帮助或建议!
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();
}
好的,感谢你们的帮助,我一直在努力,我已经取得了很大的进步。不过仍然有一个问题。该数组仅保存为每个员工输入的最后一笔交易的交易编号、类型和金额,而不是每笔交易。
我认为错误是我需要以不同于 name 和 ytd 数组的速率迭代 tNum、type 和 amount 数组?
还有点小麻烦,所以感谢您的帮助...这是我更新的代码以及末尾的打印语句。
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);
}
据我所知,您想将销售报告的值存储到数组中,其中数组名称、数组 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;
}
}
要清理代码,可以创建一种方法来为您获取值。因此,对于您的交易价值,您可以创建这样的方法
public double getSalesReportTransaction()
{
System.out.print("Enter Transaction Amount: ");
return Double.parseDouble(stdin.readLine());
}
为销售报告中的每个值创建一个方法是清理 for 循环内代码的好方法。
最后,我建议为您的销售报告制作一个 Class 并创建一个包含这些销售报告对象的容器。但我不确定您对 类 了解多少,而忽略了它。
Here is a link to Java Classes
要循环直到为交易输入 0,您可以在 else 块中执行以下操作
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();
}
}
我在分配 class 时遇到问题。我需要能够在输入某些数据后打印一份销售报告,并认为跟踪所有内容的最佳方法是使用 arrays。
几个小时以来,我一直在努力解决这个问题,但我很困惑。任何帮助将不胜感激。
作为参考,用户需要输入:
- 员工姓名
- 年初至今销售额
- 一个交易号
- 交易类型
- 交易金额
然后它应该循环回到交易编号并继续该循环,直到值 0 作为交易编号的输入。
然后它应该循环回到员工姓名并继续返回该循环,直到 Done
作为员工姓名的输入。
这是代码(我认为这是唯一相关的部分,但如果您想查看整段代码,我可以 post 它。)
再次感谢您的帮助或建议!
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();
}
好的,感谢你们的帮助,我一直在努力,我已经取得了很大的进步。不过仍然有一个问题。该数组仅保存为每个员工输入的最后一笔交易的交易编号、类型和金额,而不是每笔交易。
我认为错误是我需要以不同于 name 和 ytd 数组的速率迭代 tNum、type 和 amount 数组?
还有点小麻烦,所以感谢您的帮助...这是我更新的代码以及末尾的打印语句。
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);
}
据我所知,您想将销售报告的值存储到数组中,其中数组名称、数组 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;
}
}
要清理代码,可以创建一种方法来为您获取值。因此,对于您的交易价值,您可以创建这样的方法
public double getSalesReportTransaction()
{
System.out.print("Enter Transaction Amount: ");
return Double.parseDouble(stdin.readLine());
}
为销售报告中的每个值创建一个方法是清理 for 循环内代码的好方法。
最后,我建议为您的销售报告制作一个 Class 并创建一个包含这些销售报告对象的容器。但我不确定您对 类 了解多少,而忽略了它。
Here is a link to Java Classes
要循环直到为交易输入 0,您可以在 else 块中执行以下操作
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();
}
}