【问题标题】:Java nested while loops not functioning as desiredJava 嵌套的 while 循环无法按预期运行
【发布时间】:2018-04-26 10:15:04
【问题描述】:

我在分配课程时遇到问题。我需要能够在输入某些数据后打印销售报告,并认为跟踪所有内容的最佳方法是使用 arrays

我已经尝试了几个小时来解决这个问题,但我很难过。任何帮助将不胜感激。

供参考,用户需要输入:

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

【问题讨论】:

  • 为您的索引 (i1 - i5) 提供有意义的名称(nameIndex、dateIndex、transactionIndex、typeIndex 等),以便您更好地跟踪它们。
  • while 循环在哪里?
  • 你为什么要打破i5循环?你也可以只使用if (i3 &gt; 0) 而不是 for 循环,因为它不会循环。
  • 很难理解你真正想要实现的是什么。你能提供给你的实际作业文本吗?
  • 这非常需要重新设计。

标签: java while-loop nested-loops


【解决方案1】:

据我所知,您希望将销售报告的值存储到数组中,其中 Array name、Array ytd、Array tNum 和 Array type 都保存特定销售报告的值。现在为了使用这个概念,您需要确保索引 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 循环内代码的好方法。

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

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

【讨论】:

  • 好的,我只是一个初学者,所以这有点过头了哈哈。谢谢您的帮助。我已经重写了代码并将其发布在上面。现在似乎工作得更好,但它在输入交易金额后循环回名称,我希望它只循环回交易编号,直到输入 0,然后它应该回到名称。
  • 单个销售报告有多个交易编号并使用数组存储值的问题是,您将无法跟踪哪些交易编号对应于哪个销售报告。如果您想为单个销售报告提供多个交易编号,那么创建一个销售报告类将是可行的方法。
  • 看你贴的课程链接好像会更容易,但是老师特别说我们不能使用我们还没有学过的东西......这基本上是一个初学者课程,所以即使有更好的解决方案,它也必须简单。我将上面的代码重新发布到我现在拥有的代码上。你知道我怎么能让它只循环回反号码,直到输入 0 吗?是否可以这样写: else do { code } while (tNum != 0)
  • 添加了一些代码来帮助您循环,直到为交易编号输入 0
  • 非常感谢你,我回家后会看看这个,但我肯定越来越近了!感谢您的所有帮助!
猜你喜欢
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
  • 1970-01-01
  • 2022-12-11
  • 1970-01-01
  • 2013-04-20
  • 2015-02-20
  • 1970-01-01
相关资源
最近更新 更多