Java 程序终止时出现问题
我正在为班级做一个节目。基本上,该程序会浏览当天的一些销售信息,并在最后打印一份报告 我唯一的问题是,该计划似乎并没有在最后退出。当我为员工姓名键入“done”时,它会打印报告,但程序会返回并继续运行,再次询问员工今年迄今的销售情况 我很难弄清楚程序为什么要这样做。如果有人能帮我指出是什么导致它循环而不是退出,这样我就能修复它,那将不胜感激。谢谢Java 程序终止时出现问题,java,infinite-loop,exit,Java,Infinite Loop,Exit,我正在为班级做一个节目。基本上,该程序会浏览当天的一些销售信息,并在最后打印一份报告 我唯一的问题是,该计划似乎并没有在最后退出。当我为员工姓名键入“done”时,它会打印报告,但程序会返回并继续运行,再次询问员工今年迄今的销售情况 我很难弄清楚程序为什么要这样做。如果有人能帮我指出是什么导致它循环而不是退出,这样我就能修复它,那将不胜感激。谢谢 import java.io.*; import java.text.DecimalFormat; public class Project04Dr
import java.io.*;
import java.text.DecimalFormat;
public class Project04Driver {
public static void main(String args[]) throws IOException {
Project04 app;
app = new Project04();
app.appMain();
}
}
class Project04 {
private static DecimalFormat df2 = new DecimalFormat ("$#,###.00");
BufferedReader stdin;
int trans, tCount;
double ytd, amount, empTotal, empBonus, totA, totF, totS, totSales, totYtd, hiTrans, ytdSales;
String date, hiEmp, name, type;
public void appMain() throws IOException {
rptInit();
displayHeader();
getDate();
while (!name.equalsIgnoreCase("Done")) {
salesData();
}
}
void rptInit() {
name = "f";
trans = 1;
amount = totF = totA = totS = totSales = totYtd = hiTrans = ytdSales = 0;
stdin = new BufferedReader(new InputStreamReader(System.in));
}
void displayHeader() {
System.out.println("--------------------------------------------");
System.out.println("Project #04 Solution");
System.out.println("Written by Jordan Hawkes");
System.out.println("--------------------------------------------");
System.out.println("");
}
void getDate() throws IOException {
System.out.print("Enter Date: ");
date = stdin.readLine();
}
void salesData() throws IOException {
System.out.print("Enter Name: ");
name = stdin.readLine();
if (name.equalsIgnoreCase("done")) {
rptOut();
return;
}
System.out.print("Enter Year to Date Sales: ");
ytd = Double.parseDouble(stdin.readLine());
ytdSales = ytdSales + ytd;
totYtd = totYtd + ytdSales;
trans();
}
void trans() throws IOException {
while (trans != 0) {
System.out.print("Enter Transaction Number: ");
trans = Integer.parseInt(stdin.readLine());
if (trans == 0) {
outputSalesData();
empTotal = 0;
empBonus = 0;
ytdSales = 0;
trans = 1;
salesData();
} else {
System.out.print("Enter Transaction Type: ");
type = stdin.readLine();
System.out.print("Enter Transaction Amount: ");
amount = Double.parseDouble(stdin.readLine());
totSales = totSales + amount;
totYtd = totYtd + amount;
ytdSales = ytdSales + amount;
empTotal = empTotal + amount;
empBonus = empBonus + (amount * 0.05);
outputUpdate();
calcSalesData();
tCount++;
}
}
}
void calcSalesData() {
if (type.equalsIgnoreCase("F")) {
totF = totF + amount;
}
if (type.equalsIgnoreCase("A")) {
totA = totA + amount;
}
if (type.equalsIgnoreCase("S")) {
totS = totS + amount;
}
hiTrans();
}
void hiTrans() {
if (amount > hiTrans) {
hiTrans = amount;
hiEmp = name;
}
}
void outputUpdate() {
System.out.println("");
System.out.println("--------------------------------------------");
System.out.println(name + "'s Total Sales for Day: " + df2.format(empTotal));
System.out.println("--------------------------------------------");
System.out.println("");
}
void outputSalesData() {
System.out.println("");
System.out.println("--------------------------------------------");
System.out.println("Employee Name: \t\t" + name);
System.out.println("YTD Sales: \t\t" + df2.format(ytdSales));
System.out.println("Employee Bonus: \t" + df2.format(empBonus));
System.out.println("--------------------------------------------");
System.out.println("");
}
void rptOut() {
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\t" + df2.format(totS));
System.out.println("--------------------------------------------");
System.out.println("Total Sales for Day: \t\t" + df2.format(totSales));
System.out.println("Total YTD: \t\t\t" + df2.format(totYtd));
System.out.println("--------------------------------------------");
System.out.println("Highest Trans Amount: \t\t" + df2.format(hiTrans));
System.out.println("Employee w/ Highest Trans: \t" + hiEmp);
System.out.println("--------------------------------------------");
return;
}
}在我的独立Java程序中,以下调用会执行此操作:
System.exit(0);
我不能直接评论你的问题,所以我会把它放在这里。我认为问题在于您调用salesData(): -在appMain()中,您无法选择while循环中的“完成”,因此它永远不会结束
- 我的建议是让salesData在使用完后返回一个布尔值,您可以这样说!boolean并返回true,这将结束appMain()中的while循环李>
希望这能解决问题 trans()和appMain()调用salesData()。如果trans()调用salesData(),那么salesData()中的返回将返回trans(),trans()本身有一个while循环。要允许程序自然退出,不应在循环中调用方法,而应允许方法退出并控制主循环中的流 以下是重新设计的方法: 新的主循环成对调用
salesData()
和trans()
(假设名称未“完成”)
现在,方法salesData()
不能调用trans()
:
而且trans()
不得调用salesData()
:
小结:既然方法
salesData()
和trans()
彼此不调用(它们在完成方法后自然返回),而且由于控件现在来自主循环,您的应用程序将按预期退出。当我在打印的报告末尾添加此项时(在rptOut()中)然后程序只打印报告的第一行(“食品和软饮料销售总额”),而不打印其余部分…啊-在您的情况下,输出流没有正确刷新。在调用“exit”之前调用System.out.flush()。出于某种原因,仍然只打印第一行。。。我实际上只是在添加:System.out.flush();系统出口(0);就在rptOut()方法中的最后一个print语句之后。。。这是正确的吗?您是否尝试过使用调试器来查看发生了什么?谢谢,我现在知道我是通过调用彼此内部的方法来循环的。
while (!name.equalsIgnoreCase("Done")) {
salesData();
if (!name.equalsIgnoreCase("Done"))
trans();
}
void salesData() throws IOException {
System.out.print("Enter Name: ");
name = stdin.readLine();
if (name.equalsIgnoreCase("done")) {
rptOut();
return;
}
// ... processing ...
}
void trans() throws IOException {
while (trans != 0) {
System.out.print("Enter Transaction Number: ");
trans = Integer.parseInt(stdin.readLine());
if (trans == 0) {
outputSalesData();
empTotal = 0;
empBonus = 0;
ytdSales = 0;
} else {
System.out.print("Enter Transaction Type: ");
type = stdin.readLine();
System.out.print("Enter Transaction Amount: ");
amount = Double.parseDouble(stdin.readLine());
// ... processing ...
outputUpdate();
calcSalesData();
tCount++;
}
}
}