Java 中的字符串和计数器数组

String and counter Arrays in Java

我需要做一个table来记录每幅画的投票,但是我很难计算每幅画的票数,所以如果我回答 D,投票就会被记录下来对于 A. 请帮助。我还需要这是一个无限循环,当输入值 "hello" 时结束。

public static void allo()

{   String[] painting = {"Mona Lisa","Water Lilies","The Scream","A Young Rembrandt"};
    String[] art = {"A","B","C","D"};
    int[] vote = {0,0,0,0}; 
    String searchkey ="";
    for (int i=0; i<art.length; i++)
    {
    searchkey = JOptionPane.showInputDialog("Please tell us which painting you thihnk is the best? \n Vote A for Mona Lisa \n Vote B for Water Lilies \n                                    Vote C for The Scream \n Vote D for A Young Rembrandt");
        if (art[i].equals(searchkey))
        {   vote[i]+=1; 
        }   
    JOptionPane.showMessageDialog(null,"The current votes are:\n" +vote[0]+ " : " +painting[0]+"\n"+vote[1]+" : " +painting[1]+                             "\n" +vote[2]+" : "+painting[2]+ "\n" +vote[3]+" : " +painting[3]);
    }
}

你的 for 循环在错误的地方,它应该在你的对话框之后。你应该用 do/while 循环包装它。

do {
    searchkey = JOptionPane.showInputDialog("Please tell us which painting you thihnk is the best? \n Vote A for Mona Lisa \n Vote B for Water Lilies \n                                    Vote C for The Scream \n Vote D for A Young Rembrandt");
    for (int i=0; i<art.length; i++)
    {
        if (art[i].equals(searchkey))
        {   vote[i]=vote[i]+1; 
        }   
     }
} while (!searchkey.equals("hello"));
JOptionPane.showMessageDialog(null,"The current votes are:\n" +vote[0]+ " : " +painting[0]+"\n"+vote[1]+" : " +painting[1]+                             "\n" +vote[2]+" : "+painting[2]+ "\n" +vote[3]+" : " +painting[3]);