gpt4 book ai didi

循环运行时的 Java 逻辑错误 : If/else counter incorrectly counting

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

如果这个太模糊,请告诉我:

我有一个可以编译和运行的循环,但它没有按照我想要的方式执行。我正在尝试计算 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);

最佳答案

根据你的问题理解,你需要这样的东西

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);

关于循环运行时的 Java 逻辑错误 : If/else counter incorrectly counting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39036867/

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