我正在创建一个程序,允许用户添加问题并将其删除(基于菜单)。
我的第一个问题: 如何显示我的问题" arraylist在另一种方法? (在这种情况下,"您的问题是:+问题数组)
我的第二个问题: 如何从问题数组列表中删除用户输入的问题并将其存储到"已解决的"我的第二种方法中的数组列表?
代码段:
public class IssueTrackingObject {
int length;
int solved;
Scanner input = new Scanner(System.in);
public void createIssue() {
System.out.println("Enter the number of issues you will enter: ");
length = input.nextInt();
String[] issues = new String[length]; //issues stored in this array
for(int counter = 0; counter < length; counter++){
System.out.println(counter+1 + (") Enter the first issue: "));
issues[counter] = input.next();
}
System.out.println("Your issues are: ");
for(int counter = 0; counter < length; counter++){
System.out.println(issues[counter]);
}
}
void solvedIssue() {
System.out.println("To MARK an issue as solved, please enter the number of the issue: ");
solved = input.nextInt();
String [] issues = new String [solved];
for(int s = 0; s < length; s++){
System.out.println(issues[s]);
}
}
void printSolvedIssue() {
System.out.println("You have SOLVED the following issues: ");
}
void printUnsolvedIssue() {
System.out.println("Your unsolved Issues are: ");
}
}
我已经阅读了我在网上和书中可以找到的所有内容,但我找不到符合我想要的代码的解决方案。
答案 0 :(得分:0)
问题#1
您必须使您的问题列出该类的成员,以便可以从IssueTrackingObject
类中的任何方法访问它们。
问题#2
您可以添加SolvedIssues列表来存储从总问题列表中删除的那些问题。
您可以尝试按以下方式实施IssueTrackingObject
课程:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class IssueTrackingObject {
Scanner input = new Scanner(System.in);
// Make your issues lists members of the class so that they
// can be accessed from all the methods inside the class
private List<String> _allIssues = new ArrayList<String>();
// Added a Solved issues list to store the solved issues
private List<String> _solvedIssues = new ArrayList<String>();
public void createIssue() {
System.out.println("Enter the number of issues you will enter: ");
int length = input.nextInt();
for (int counter = 0; counter < length; counter++) {
System.out.printf("Enter issue number %d\n", counter + 1);
_allIssues.add(input.next());
}
System.out.println("Your issues are: ");
for (int counter = 0; counter < _allIssues.size(); counter++) {
System.out.printf("%d. %s\n", counter + 1, _allIssues.get(counter));
}
}
void solvedIssue() {
if(_allIssues.size() == 0) {
System.out.println("No issues entered yet. Please enter some issues");
return;
}
System.out.println("To MARK an issue as solved, please enter the number of the issue: ");
int index = input.nextInt();
--index; // List starts at zero
_solvedIssues.add(_allIssues.get(index));
_allIssues.remove(index);
}
void printSolvedIssue() {
System.out.println("You have SOLVED the following issues: ");
for(int i = 0; i < _solvedIssues.size(); ++i) {
System.out.printf("%d. %s\n", i + 1, _solvedIssues.get(i));
}
}
void printUnsolvedIssue() {
System.out.println("Your unsolved Issues are: ");
for(int i = 0; i < _allIssues.size(); ++i) {
System.out.printf("%d. %s\n", i + 1, _allIssues.get(i));
}
}
public static void main(String[] args) {
int exit = 1;
Scanner input = new Scanner(System.in);
IssueTrackingObject itracker = new IssueTrackingObject();
while(exit != -1) {
System.out.println("Select an Option or -1 to exit\n1. Create Issue\n2. Solve Issue\n"
+ "3. View solved issues\n4. View unsolved issues");
exit = input.nextInt();
switch (exit) {
case(0) :
System.out.println("Number of issues has to be greater than zero");
break;
case(1) :
itracker.createIssue();
break;
case(2) :
itracker.solvedIssue();
break;
case(3) :
itracker.printSolvedIssue();
break;
case(4) :
itracker.printUnsolvedIssue();
break;
}
}
input.close();
System.out.println("Program finished");
}
}
如果我没有完全了解你想做的事情,请随意修改。