如果这个太模糊,请告诉我:
我有一个编译和运行的循环,但它没有按照我的意愿执行。 我试图计算if / else选择的次数,只要循环运行然后显示选择的次数。
选择有效,循环有效,但是如果我将未选择的选项编码为0,那么如果选择了另一个选择,则下次将第一个选择的累计计数返回到0。选择是积累的。
我有4个if / else问题,但它们的编码方式相同。如果我能解决问题,我可以解决其余问题。
以下是代码的相关部分。 (过去我被问过如果只有部分内容正在起作用的话,就不会输入那么多代码。)
int choiceCount;
int choiceCount2;
while(quitYn == 1)
{
System.out.println("I am ready for the next participant.");
System.out.println("");
System.out.println("");
System.out.println("What would you rather have?");
System.out.println("Enter 1 for Love or 2 for Money.");
surveyChoice = input.nextInt();
if(surveyChoice == 1)
{
choice = FIRST_PICK;
message = "Love";
choiceCount = ++ FIRST_CHOICE - 1;
choiceCount2 = ++ SECOND_CHOICE - 3;
}
else if(surveyChoice == 2)
{
choice = SECOND_PICK;
message = "Money";
choiceCount = ++ FIRST_CHOICE - 1;
choiceCount2 = ++ SECOND_CHOICE - 2;
}
else
{
choice = "Broken dreams";
message = "Broken dreams";
choiceCount = ++ FIRST_CHOICE - 2;
choiceCount2 = ++ SECOND_CHOICE - 3;
}
System.out.println(choiceCount + "Participants chose" + FIRST_PICK);
System.out.println(choiceCount2 + "Participants chose" + SECOND_PICK);
答案 0 :(得分:2)
根据您的问题理解,您需要这样的东西
int choiceCount = 0;
int choiceCount2 = 0;
while(quitYn == 1)
{
System.out.println("I am ready for the next participant.");
System.out.println("");
System.out.println("");
System.out.println("What would you rather have?");
System.out.println("Enter 1 for Love or 2 for Money.");
surveyChoice = input.nextInt();
if(surveyChoice == 1)
{
choice = FIRST_PICK;
message = "Love";
choiceCount++;
}
else if(surveyChoice == 2)
{
choice = SECOND_PICK;
message = "Money";
choiceCount2++;
}
else
{
choice = "Broken dreams";
message = "Broken dreams";
}
}
System.out.println(choiceCount + "Participants chose" + FIRST_PICK);
System.out.println(choiceCount2 + "Participants chose" + SECOND_PICK);