作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在我的游戏中创建一个 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 的问题,for
或 while
可能效果最好。
致编辑编码风格的人[1]: 注意:我通过将所有内容分开进行编码,因为它可以帮助我看到所有内容,如果这是一个问题,请告诉我。这样我就可以事后更改它
关于java - 我的智囊游戏中的 For 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30541737/
我是一名优秀的程序员,十分优秀!