程序终止时遇到问题
Trouble having program terminate
我正在为 class 做一个程序。基本上,该程序会遍历当天的一些销售信息并在最后打印一份报告。
我唯一的问题是程序似乎最后没有退出。当我为员工姓名键入 "done" 时,它会打印报告,但程序循环返回并继续 运行 再次询问员工年初至今的销售额。
我无法弄清楚为什么程序会这样做。如果有人可以帮助我指出导致它循环而不是退出的原因,以便我可以修复它,将不胜感激。谢谢!
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():
-in appMain() 你在 while 循环中没有选择 "Done" 所以它永远不会结束。
- 我的建议是在完成后将 salesData 设置为 return 布尔值,这样你就可以说 !boolean 和 return 为真,这将结束你在 appMain() 中的 while 循环.
希望这能解决问题![=10=]
trans() 和 appMain() 调用 salesData()。如果 salesData() 被 trans() 调用,那么 salesData() return 中的一个 return 到 trans(),它本身有一个 while 循环。
为了让程序自然退出,不要在循环中调用方法,而是让方法从主循环中退出,控制流程。
这是重新设计的方法:
新的主循环成对调用salesData()
和trans()
(假设名字不是"done")
while (!name.equalsIgnoreCase("Done")) {
salesData();
if (!name.equalsIgnoreCase("Done"))
trans();
}
现在,方法 salesData()
不能调用 trans()
:
void salesData() throws IOException {
System.out.print("Enter Name: ");
name = stdin.readLine();
if (name.equalsIgnoreCase("done")) {
rptOut();
return;
}
// ... processing ...
}
而且 trans()
不能调用 salesData()
:
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++;
}
}
}
总结:现在方法 salesData()
和 trans()
不互相调用(它们 return 在完成方法后自然地),并且由于控制现在来自主循环,您的应用程序将按预期退出。
我正在为 class 做一个程序。基本上,该程序会遍历当天的一些销售信息并在最后打印一份报告。
我唯一的问题是程序似乎最后没有退出。当我为员工姓名键入 "done" 时,它会打印报告,但程序循环返回并继续 运行 再次询问员工年初至今的销售额。
我无法弄清楚为什么程序会这样做。如果有人可以帮助我指出导致它循环而不是退出的原因,以便我可以修复它,将不胜感激。谢谢!
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():
-in appMain() 你在 while 循环中没有选择 "Done" 所以它永远不会结束。
- 我的建议是在完成后将 salesData 设置为 return 布尔值,这样你就可以说 !boolean 和 return 为真,这将结束你在 appMain() 中的 while 循环.
希望这能解决问题![=10=]
trans() 和 appMain() 调用 salesData()。如果 salesData() 被 trans() 调用,那么 salesData() return 中的一个 return 到 trans(),它本身有一个 while 循环。
为了让程序自然退出,不要在循环中调用方法,而是让方法从主循环中退出,控制流程。
这是重新设计的方法:
新的主循环成对调用salesData()
和trans()
(假设名字不是"done")
while (!name.equalsIgnoreCase("Done")) {
salesData();
if (!name.equalsIgnoreCase("Done"))
trans();
}
现在,方法 salesData()
不能调用 trans()
:
void salesData() throws IOException {
System.out.print("Enter Name: ");
name = stdin.readLine();
if (name.equalsIgnoreCase("done")) {
rptOut();
return;
}
// ... processing ...
}
而且 trans()
不能调用 salesData()
:
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++;
}
}
}
总结:现在方法 salesData()
和 trans()
不互相调用(它们 return 在完成方法后自然地),并且由于控制现在来自主循环,您的应用程序将按预期退出。