-3

我正在尝试创建一个 Java 代码来计算某些画作的投票。我想收集用户投票的内容并将其添加到运行总数中,以便在每个用户投票后打印总票数。

到目前为止,这是我的代码:

public static void Gallery()
    {

       // Declare variables
       //
    String total = "-33";
        String[] paintings = {"Mona Lisa","Water Lilies","The Scream","A Young Rembrandt"};
    int[] scores = {0,0,0,0};
        String searchKey; //the thing looked for
  // 


       //now can get an answer quickly without calculating just looking it up
    for (int y=0; y<paintings.length; y++)
    {
        System.out.println("Vote "+ (y+1) + " for " + paintings[y]);
    }


       for (int i=0; i<paintings.length; i++)

            {
            String vote = JOptionPane.showInputDialog("Which painting would you like to vote for?");
            if (paintings[i].equals(vote))
            {
                    JOptionPane.showMessageDialog(null, "You voted for " + vote); 
            paintings[i] + 1 = scores[i];
            for(int z=0; z<paintings.length; z++)
            System.out.println(scores[z] + " " + paintings[z]);
        }

        if(total.equals(vote))
        {


        }
        else 
        {
            JOptionPane.showMessageDialog(null, "Invalid entry");
        }

       }


    }   

我想要这样说用户投票'蒙娜丽莎'这会将'1'添加到'int []分数'中的第一个整数?我该怎么做?

谢谢

4

1 回答 1

2

这条线对我来说有点奇怪:painting[i] + 1 = scores[i];

我想你想放在这里的是:scores[i]++;

要解决有关重置分数数组的问题,请进行如下修改:

static String[] paintings = {"Mona Lisa"...};
static int[] scores = {0,0,0,0};

通过静态声明这些变量,您的变量将只被初始化一次,这意味着它们将在后续调用您的函数时保持其状态。将绘画数组声明为静态实际上只是一种优化,但将分数数组声明为静态可以解决您的实际问题。

于 2013-10-29T21:28:11.280 回答