0

我知道这个问题已经被问过很多次了,但我仍然无法解决这个问题。问题是我必须存储用户输入并打印出一个值。

例如,有 4 个人,person1、person2、person3 和 person4。如果我投票给 person1,person1 的投票数变为 1,其他人保持 0。然后,如果我投票给 person2,person2 的投票数变为 1,person1 也为 1。

我可以编译代码。但是,如果我投票给 person1,输出变为 4。如果我再投票给 person2,则 person2 的输出变为 4,投票给 person1 又回到 0。我是一个完全的编程初学者,并被困在这个程序中整整 4 天,非常感谢任何帮助。非常感谢您提前。

import javax.swing.*; // import swing lib for i/o
public class Arrays4
{
public static void main (String[] args)
{
    voteperson();
    voterepeat();
    System.exit(0);
} // end method main

public static int voteperson()
{
    // Initialize String Arrays
    String[] person = new String[4];
    person[0] = "person1";
    person[1] = "person2";
    person[2] = "person3";
    person[3] = "person4";

    // Initialize int Arrays
    int[] votescount = new int[4];
    votescount[0] = 0;
    votescount[1] = 0;
    votescount[2] = 0;
    votescount[3] = 0;

    // Declare String Variables
    String userinput;
    userinput = JOptionPane.showInputDialog
    ("Please tell us which painting you think is the best."+"\n"+
    "Vote 1 "+person[0]+"\n"+
    "Vote 2 "+person[1]+"\n"+
    "Vote 3 "+person[2]+"\n"+
    "Vote 4 "+person[3]);

    int answer = Integer.parseInt(userinput);

    int i;
    for (i=0; i<votescount.length; i++)
    {
        if (answer == 1)
        {
            votescount[0] = votescount[0]+1;
        }
        else if (answer == 2)
        {
            votescount[1] = votescount[1]+1;
        }
        else if (answer == 3)
        {
            votescount[2] = votescount[2]+1;
        }
        else if (answer == 4)
        {
            votescount[3] = votescount[3]+1;
        }
        else
        {

        }
    } // end for loop

    JOptionPane.showMessageDialog
    (null, "The current votes are" + "\n" +
    votescount[0] + " :" + person[0] + "\n" +
    votescount[1] + " :" + person[1] + "\n" + 
    votescount[2] + " :" + person[2] + "\n" +
    votescount[3] + " :" + person[3]);

    return 0;
}
public static void voterepeat()
{
    for (int j=1; j<=4; j++)
    {
        int repeat;
        repeat = voteperson();
        System.out.println(j);
    }
}
}
4

3 回答 3

3

当你这样做时:

for (i=0; i<votescount.length; i++){...
} // end for loop

循环发生 4 次。这意味着该位发生了 4 次:

if (answer == 1)
    {
        votescount[0] = votescount[0]+1;
    }

这意味着投票数增加了 4!

于 2013-10-12T23:35:33.143 回答
0

首先你做,

int[] votescount = new int[4];

然后,你做

for (i=0; i<votescount.length; i++)
    {
}

因此,该循环迭代 4 次。在循环内,你做,

if (answer == 1)
    {
        votescount[0] = votescount[0]+1;
    }

这就是为什么,你的计数增加了 4!

于 2013-10-12T23:51:39.670 回答
0

摆脱你的 for 循环:

for (i=0; i<votescount.length; i++)

并使人员和选票成为全局和静态的。

这是更新的代码:

import javax.swing.*; // import swing lib for i/o
public class Arrays4
{
     static String[] person = new String[4];//these have been made global and static
     static int[] votescount = new int[4];
    public static void main (String[] args)
    {
        // Initialize String Arrays
        person[0] = "person1";//these have been moved so that it is only called once
        person[1] = "person2";
        person[2] = "person3";
        person[3] = "person4";
        // Initialize int Arrays

        votescount[0] = 0;
        votescount[1] = 0;
        votescount[2] = 0;
        votescount[3] = 0;


        voteperson();
        voterepeat();
        System.exit(0);
    } // end method main

    public static int voteperson()
    {
        // Declare String Variables
        String userinput;
        userinput = JOptionPane.showInputDialog
        ("Please tell us which painting you think is the best."+"\n"+
        "Vote 1 "+person[0]+"\n"+
        "Vote 2 "+person[1]+"\n"+
        "Vote 3 "+person[2]+"\n"+
        "Vote 4 "+person[3]);

        int answer = Integer.parseInt(userinput);
        System.out.println(answer);
        int i;


            if (answer == 1)
            {
                votescount[0] = votescount[0]+1;
            }
            else if (answer == 2)
            {
                votescount[1] = votescount[1]+1;
            }
            else if (answer == 3)
            {
                votescount[2] = votescount[2]+1;
            }
            else if (answer == 4)
            {
                votescount[3] = votescount[3]+1;
            }
            else
            {

            }


        JOptionPane.showMessageDialog
        (null, "The current votes are" + "\n" +
        votescount[0] + " :" + person[0] + "\n" +
        votescount[1] + " :" + person[1] + "\n" + 
        votescount[2] + " :" + person[2] + "\n" +
        votescount[3] + " :" + person[3]);

        return 0;
    }
    public static void voterepeat()
    {
        for (int j=1; j<=4; j++)
        {
            int repeat;
            repeat = voteperson();
            System.out.println(j);
        }
    }
}
于 2013-10-13T01:23:39.060 回答