从循环中打印多个用户输入

Printing multiple user input from a loop

这是一个由两部分组成的问题。 1)如果循环运行不止一次,我如何打印所有用户输入,我知道分配给变量的旧字符串每次都会被删除。 2)我如何计算循环运行次数的所有任务的总任务持续时间。谢谢。

    public static String createTaskID()
    { 
       num1 = Integer.parseInt(JOptionPane.showInputDialog(null," Enter the number of tasks you wish to enter "));           
               
         for(i = 0;i<num1;i++){       
      taskName = JOptionPane.showInputDialog(" Enter the name of task ");
     
     taskNumber =0; taskNumber++;
      
      taskDescription = JOptionPane.showInputDialog(null," Enter a short description of the task. ");
           if(checkTaskDescription(taskDescription))  
           { JOptionPane.showMessageDialog(null," Task successfully captured ");}
           else
           { JOptionPane.showMessageDialog(null," Please enter a task description of less than 50 characters "); }
       
      developerDetails = JOptionPane.showInputDialog(" Enter First and last name of the Developer assigned to task. ");
      
      taskDuration = JOptionPane.showInputDialog(" Enter estimated duration of task (in hours) ");
    
     option2 = Integer.parseInt(JOptionPane.showInputDialog(null, " Choose Status of task : \n Option 1)To Do \n Option 2)Done \n Option 3)Doing ")); 
    
    switch(option2)
        { case 1: 
               taskStatus = " To Do ";
            break; 
            
        case 2 :
              taskStatus = " Done ";
            break; 
            
        case 3 : 
              taskStatus = " Doing ";
            break;}

     
    }   
        return null;
    }
    
    
    
    
    public static String printTaskDetails()
{  
   JOptionPane.showMessageDialog(null," TASK DETAILS \n -----------------------\n  Current Task Status : " + taskStatus + 
   "\n Developer Details : " + developerDetails + "\n Task Number : " + taskNumber + "\n Task Name : " + taskName +  
           "\n Task Description : " + taskDescription + "\n Task Duration : " + taskDuration + " hours.");  
return null;
}

您可以为此创建一个 class,它将具有 taskStatusdeveloperDetailstaskNumbertaskName、[= 的数据成员19=]、taskDuration。如果有意义,您可以将这些属性区分为几个 classes(比如 class 代表 Developer,另一个代表 Task),但为了简单起见,我假设开发人员现在将拥有一个 class,我将其称为 MyTask.

因此,如果您创建 MyTaskList,则初始化为

List<MyTask> myTasks = new ArrayList<MyTask>();

就在循环之前(不要忘记实现 MyTaskimport 你需要 ListArrayList 的包),然后在循环内部你在每次迭代时实例化一个 MyTask 对象,然后你可以做类似

myTasks.add(theMyTask);

同样,不要忘记正确创建 theMyTaskcreateTaskID

结束
return myTasks;

但是,既然现在是List,修改方法的声明为

public static List<MyTask> createTaskID()

还有。而且,当你打算显示它时,只需调用该方法,循环结果将它们中的每一个转换成一个看起来像你的 String,连接结果字符串并显示结果。