我正在尝试创建一个问题跟踪系统,但是我遇到了一个问题,每次我运行代码时,它都不会返回菜单,而是循环。每当我描述自己的问题时,我都希望代码返回菜单。
package com.company.TrackingSystem;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
ArrayList<TrackingSystem> tracker = new ArrayList<>();
Main myApp = new Main();
myApp.menu();
System.out.print("Select option >> ");
int option = in.nextInt();
switch (option){
case 1:
myApp.CreateIssue(tracker);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Invalid choice...!");
break;
}
}
private ArrayList<TrackingSystem> CreateIssue(ArrayList<TrackingSystem> tracker){
String issueCreator;
String a = " ";
boolean is = true;
do {
System.out.println("*** Create an Issue***");
System.out.println("Describe your Issue: ");
issueCreator = in.nextLine();
}while (is);
TrackingSystem ts = new TrackingSystem(issueCreator,false);
tracker.add(ts);
return tracker;
}
private void menu() {
boolean is = true;
System.out.println("---Menu---");
System.out.println(
"1.Create new Issue\n" +
"2.Mark Issue as solved\n" +
"3.View unsolved Issues\n" +
"4.View solved Issues\n" +
"5.Exit\n"
);
}
}
我的跟踪课程
package com.company.TrackingSystem;
public class TrackingSystem {
private String createIssue;
private boolean issueSolved;
public TrackingSystem(String createIssue, boolean issueSolved) {
this.createIssue = createIssue;
this.issueSolved = issueSolved;
}
public String getCreateIssue() {
return createIssue;
}
public void setCreateIssue(String createIssue) {
this.createIssue = createIssue;
}
public boolean isIssueSolved() {
return issueSolved;
}
public void setIssueSolved(boolean issueSolved) {
this.issueSolved = issueSolved;
}
}
示例输出:
---Menu---
1.Create new Issue
2.Mark Issue as solved
3.View unsolved Issues
4.View solved Issues
5.Exit
Select option >> 1
*** Create an Issue***
Describe your Issue:
*** Create an Issue***
Describe your Issue:
as
*** Create an Issue***
Describe your Issue:
sa
*** Create an Issue***
Describe your Issue:
as
答案 0 :(得分:0)
让我们看看这个功能:
private ArrayList<TrackingSystem> CreateIssue(ArrayList<TrackingSystem> tracker){
String issueCreator;
String a = " ";
boolean is = true;
do {
System.out.println("*** Create an Issue***");
System.out.println("Describe your Issue: ");
issueCreator = in.nextLine();
}while (is);
TrackingSystem ts = new TrackingSystem(issueCreator,false);
tracker.add(ts);
return tracker;
}
请特别注意循环条件:while(is)
。您声明了bool is = true;
,但从未在循环内将其更改为false。这意味着循环将永远继续。
要解决此问题,您必须做出一些决定。首先,您真的要继续在这里循环播放吗?您是否打算允许用户输入他们想要的尽可能多的问题?还是只想创建一个问题,然后返回菜单。如果是前者,那么您需要弄清楚用户将如何告诉程序他们已经完成输入问题。您可以使用它来停止循环。如果是更高版本,则只需删除循环即可。
现在让我们看一下main()
:
public static void main(String[] args) {
ArrayList<TrackingSystem> tracker = new ArrayList<>();
Main myApp = new Main();
myApp.menu();
System.out.print("Select option >> ");
int option = in.nextInt();
switch (option){
case 1:
myApp.CreateIssue(tracker);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Invalid choice...!");
break;
}
}
即使使用代码解决了问题后再创建问题,您也将运行该程序,并且在执行一项操作后便会退出。这是因为您没有循环进入main()
。您需要在此处添加重复以下步骤的循环:
每个步骤都是一种方法,因此您可以使main()
简短。请注意前3个步骤如何在循环内。我认为这是您要尝试使用的用于创建新项目的循环的方法,但是以某种方式将循环放置在错误的位置。如您在这里看到的那样,通过用语言写出步骤,我们对如何组织代码有了一个清晰的认识。在编写计算机程序时,这样做是很棒的工具。