【问题标题】:JOptionPane Input for loop with two incrementsJOptionPane 循环输入,具有两个增量
【发布时间】:2013-04-04 21:43:38
【问题描述】:

好的,我在下面的循环中遇到了问题。此循环的预期目标是 userinput[k] 是已经在方法中按字母顺序排序的名称列表。然后这些名称将显示在下面的 InputDialog 中,其中将输入一个代表学位的数字。

我正在尝试匹配名称和学位。例如:在循环中,第一个用户输入将是 userinput[0]。我希望将数字输入然后蜜蜂度数[0],然后以此类推....

问题是我如何使用 J 增量器存储该输入。所以我基本上得到的错误是 String degree[] = Integer......

for ( int k = 0;  k < userinput.length;  k++ ){      
     for (int j = 0; j < userinput.length; j++ ) {                    
         String input = JOptionPane.showInputDialog("Please enter the highest earned degree for the following person : " + userinput [ k ] +  "\n 1 = BS \n 2 = MS \n 3 = PhD");
         String degree[] = Integer.parseInt(input[]);
     }
}

【问题讨论】:

  • 为什么需要嵌套循环?这会让你多次(`userinput.length')向同一个人提问。

标签: java arrays swing for-loop joptionpane


【解决方案1】:

这是你想要做的吗?

int[] degree = new int[userInput.length];
for(int k = 0; k < userInput.length; k++) {
    String input = JOptionPane.showInputDialog("Please enter the highest earned degree for the following person : " + userinput [ k ] +  "\n 1 = BS \n 2 = MS \n 3 = PhD");
    degree[k] = Integer.parseInt(input);
}

【讨论】:

  • 这正是我想要的。谢谢您的帮助!我想了很多。
  • @Chris:不客气。只要您想遍历数组一次,就使用单个for-loop,除非它是多维的。否则每个名称将在输入对话框中出现多次。
【解决方案2】:

这是你想要做的吗?

假设您已经将 userinput 声明为一个字符串数组 一定大小并分配其值,并将度数声明为数组 相同大小的字符串。

for ( int k = 0;  k < userinput.length;  k++ ){      
         String input = JOptionPane.showInputDialog("Please enter the highest earned degree for the following person : " + userinput [ k ] +  "\n 1 = BS \n 2 = MS \n 3 = PhD");
         degree[k] = input;
     }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 2014-01-03
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 2021-07-19
    相关资源
    最近更新 更多