Java嵌套while循环不能按预期运行

时间:2018-04-25 21:17:25

标签: java while-loop nested-loops

我在上课时遇到了麻烦。我需要能够在输入某些数据后打印销售报告,并确定跟踪所有内容的最佳方法是使用数组

我一直试图弄清楚这几个小时,我很难过。任何帮助将不胜感激。

供参考,用户需要输入:

  1. 员工姓名
  2. 年初销售
  3. 交易号
  4. 交易类型
  5. 交易金额
  6. 然后它应该循环回到事务编号并继续该循环,直到将值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();
    }
    

    好的,所以我一直在努力,感谢你们的帮助,我取得了很大的进步。但仍有一个问题。该数组仅保存为每个员工输入的最后一笔交易的转账号码,类型和金额,而不是每笔交易。

    我认为错误是我需要以与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);
    }
    

1 个答案:

答案 0 :(得分:1)

根据我的收集,您希望将Sales Report的值存储到数组中,其中Array name,Array ytd,Array tNum和Array type都包含特定Sales Report的值。现在,为了使用这个概念,您需要确保索引0在整个镜像阵列中引用相同的Sales Report数据。

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

为Sales Report中的每个值创建一个方法,这是清理for循环中代码的好方法。

最后,我建议为您的销售报告创建一个类,并创建这些Sales Report对象的容器。但我不确定你对课程有多了解并将其排除在外。

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();
        }
    }
相关问题