如何在for循环中记录值以便稍后用于计算

时间:2013-10-28 15:40:12

标签: java for-loop

为此,我想编写一个程序,让一个人输入动物解决了多少个谜题并存储值并将它们加在一起

我如何实现记录每个动物解决的谜题数量并将它们加在一起的东西?

例如:

动物1解决了多少个谜题? 5

动物2解决了多少个谜题? 2

动物3解决了多少个谜题? 1

动物4解决了多少个谜题? 3

动物共解决了11个谜题

     for(int puzzleNum = 1; puzzleNum <= 4; puzzleNum++)

     {
         textinput = JOptionPane.showInputDialog("How many puzzles did animal "  + mazeNum + " solve?");

         mazesSolved = Integer.parseInt(textinput);

   }

2 个答案:

答案 0 :(得分:0)

你只是在路上添加它。

int mazesSolved = 0;   
for(int puzzleNum = 1; puzzleNum <= 4; puzzleNum++)
{
    textinput = JOptionPane.showInputDialog("How many puzzles did animal "  + mazeNum + "solve?");
    mazesSolved += Integer.parseInt(textinput);
}

答案 1 :(得分:0)

int mazesSolved = 0;

for(int puzzleNum = 1; puzzleNum <= 4; puzzleNum++)
{
     textinput = JOptionPane.showInputDialog("How many puzzles did animal "  + mazeNum + " solve?");
     mazesSolved += Integer.parseInt(textinput);
}