gpt4 book ai didi

Java 嵌套 while 循环未按预期运行

转载 作者:行者123 更新时间:2023-11-30 06:12:02 24 4
gpt4 key购买 nike

我在布置类(class)作业时遇到问题。我需要能够在输入某些数据后打印销售报告,并认为跟踪所有内容的最佳方法是使用数组

我几个小时以来一直试图解决这个问题,但我被难住了。任何帮助将不胜感激。

作为引用,用户需要输入:

  1. 员工姓名
  2. 年初至今的销售额
  3. 交易编号
  4. 交易类型
  5. 交易金额

然后它应该循环回交易编号并继续该循环,直到给出值 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);
}

最佳答案

据我所知,您希望将销售报告的值存储到数组中,其中数组名称、数组 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;
}
}

为了清理代码,可以创建一种方法来为您获取值。因此,对于您的交易值(value),您可以创建这样的方法

public double getSalesReportTransaction()
{
System.out.print("Enter Transaction Amount: ");
return Double.parseDouble(stdin.readLine());
}

为销售报告中的每个值创建一个方法是清理 for 循环内代码的好方法。

最后,我建议为您的销售报告创建一个类,并创建一个包含这些销售报告对象的容器。但我不确定你对类了解多少而忽略了它。

Here is a link to Java Classes

要循环直到为事务输入 0,您可以在 else block 中执行以下操作

    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();
}
}

关于Java 嵌套 while 循环未按预期运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50031481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com