gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:57:20 27 4
gpt4 key购买 nike

我需要制作一个表格来记录每幅画的投票,但是我很难计算每幅画的票数,所以如果我要回答 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]);

关于java - Java 中的字符串和计数器数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33528228/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com