java - 我的智囊游戏中的 For 循环

标签 java for-loop

我试图在我的游戏中创建一个 for 循环,以便当猜测的数量相加时,它会进入 for 循环并给出正确猜测的数量。这是我的计算机科学课的内容,我们必须将 for 循环合并到程序中。

if (guess1 == Secret_code[0]) {
    System.out.println(" You have guessed one correctly in the right spot!");
}

if (guess2 == Secret_code[1]) {
    System.out.println(" You have guessed one correctly in the right spot!");
}

if (guess3 == Secret_code[2]) {
    System.out.println(" You have guessed one correctly in the right spot!");
}

if (guess4 == Secret_code[3]) {
    System.out.println(" You have guessed one correctly in the right spot!");
}

do {
    System.out.println("What is the number one peg in my code???");
    guess1 = Guess.nextInt();
    System.out.println("What is the number two peg in my code???");
    guess2 = Guess.nextInt();
    System.out.println("What is the number three peg in my code???");
    guess3 = Guess.nextInt();
    System.out.println("What is the number four peg in my code???");
    guess4 = Guess.nextInt();
    if (guess1 == Secret_code[0]) {
        System.out.println(" You have guessed one correctly in the right spot!");
    }
    if (guess2 == Secret_code[1]) {
        System.out.println(" You have guessed one correctly in the right spot!");
    }
    if (guess3 == Secret_code[2]) {
        System.out.println(" You have guessed one correctly in the right spot!");
    }
    if (guess4 == Secret_code[3]) {
        System.out.println(" You have guessed one correctly in the right spot!");
    }
}
while ((guess1 != Secret_code[0]) & (guess2 != Secret_code[1]) & (guess3 != Secret_code[2]) & (guess4 != Secret_code[3]));    

public static void CorrectGuess(int[] Secret_code) {
    //Variables
    int Correctspot;

    for (Secret_code[0] = 0; Secret_code[0] < 3; Secret_code[0]++)
        Correctspot = +1;
}

最佳答案

我只会向您展示一个示例,因为就像他们说的那样,这不是我们的工作,但我感觉很糟糕,因为他们是一些糟糕的老师。我还会构建一个数组,因为这将解决您可能遇到的所有问题

//make a Scanner obj
Int[] guessArray = {10, 20, 30, 5};    
int useranswer = 0;
int loopCount = 0;

while(loopCount < 4) {
    System.out.println("blalalala for Guess #" + (loopCount+1));
    useranswer = input.nextInt();

    if(useranswer == guessArray[loopCount]) {
        loopCount++;
    }//end of if

}//end of while loop

拥有一个数组列表将有助于保持一切有序,并且您可以在程序稍后的循环之后放弃这些值。您始终需要小心 while 循环,因为如果代码错误,您最终可能会导致计算机崩溃。

我还会查找 for 循环,因为根据您 future 的问题,forwhile 可能效果最好。

致编辑编码风格的人[1]: 注意:我通过将所有内容分开进行编码,因为它可以帮助我看到所有内容,如果这是一个问题,请告诉我。这样我就可以事后更改它

关于java - 我的智囊游戏中的 For 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30541737/

相关文章:

优化嵌套循环的算法

python - 如何从多个矩阵列表中创建矩阵?

java - 使用 protostuff 反序列化数组

java - 如何让我的应用程序绑定(bind)到端口 161?

java - 需要在 Windows 中使用 wget 复制文件

javascript - For 循环迭代对象

java 可执行 jar 文件无法在另一台计算机上运行

java - Spring boot - 在 @Qualifier 上使用可配置值

loops - Clojure:当当前变量的值与我的输入值匹配时,我如何让 for 循环停止?

java - 从迭代器转移到 for each