我希望程序保持循环,直到用户输入-33。 对于每个循环,它显示4个供用户投票的选项。 在用户投票之后,它将用户投票的绘画的值增加1,然后进入下一轮。
这是我目前的代码,它并没有像我计划的那样真正起作用:
import javax.swing.*; // import swing lib for i/o
public class arrraa
{
static String[] painting = 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
painting[0] = "Mona Lisa";//these have been moved so that it is only called once
painting[1] = "Water Lillies";
painting[2] = "The Scream";
painting[3] = "A Young Rembrandt";
// Initialize int Arrays
votescount[0] = 0;
votescount[1] = 0;
votescount[2] = 0;
votescount[3] = 0;
voteperson();
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 "+painting[0]+"\n"+
"Vote 2 "+painting[1]+"\n"+
"Vote 3 "+painting[2]+"\n"+
"Vote 4 "+painting[3]);
int answer = Integer.parseInt(userinput);
System.out.println(answer);
while(answer!=-33);
{
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, "Vote for one of these four paintings!");
}
JOptionPane.showMessageDialog
(null, "The current votes are" + "\n" +
votescount[0] + " :" + painting[0] + "\n" +
votescount[1] + " :" + painting[1] + "\n" +
votescount[2] + " :" + painting[2] + "\n" +
votescount[3] + " :" + painting[3]);
answer++;
}//ENDS LOOP
return 0;
}//ENDS voteperson
}//ENDS CLASS
有什么建议吗?
答案 0 :(得分:0)
您可以尝试删除semicolon ;
,如下所示:
while(answer!=-33) //Remove the semicolon
而不是
while(answer!=-33);
答案 1 :(得分:0)
在while循环之前删除分号
<强>而(回答= - 33!); 强>
答案 2 :(得分:0)
你的问题是你在每次迭代后增加answer
。所以它会变为无限,但永远不会到-33,你最终会无限循环。
改变你的循环:
while(answer!=-33)
{
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, "Vote for one of these four paintings!");
}
JOptionPane.showMessageDialog
(null, "The current votes are" + "\n" +
votescount[0] + " :" + painting[0] + "\n" +
votescount[1] + " :" + painting[1] + "\n" +
votescount[2] + " :" + painting[2] + "\n" +
votescount[3] + " :" + painting[3]);
userinput = JOptionPane.showInputDialog
("Please tell us which painting you think is the best."+"\n"+
"Vote 1 "+painting[0]+"\n"+
"Vote 2 "+painting[1]+"\n"+
"Vote 3 "+painting[2]+"\n"+
"Vote 4 "+painting[3]);
answer = Integer.parseInt(userinput);
}//ENDS LOOP
在每次迭代后询问新用户输入。这应该可以解决你的问题。