[英]Why is this While Loop not executing properly
游戏的目标是使用 while 循环。 在这个循环中,它会根据输入的数字来决定谁赢了游戏。 数字从 1 到 20。Challenge 是设置为 10 的变量。如果 <= Challenge,则 playerOne 将失去一分。 如果 > 挑战,怪物失去一分。 谁先失去 3 分,谁就输了。 我不需要生成随机数,我只需要通过扫描仪 function 输入即可。
我认为变量对于扫描仪来说是必需的,这就是我添加 Dice 变量的原因。 它们没有被使用,我很困惑是否需要它们来使扫描仪工作以便用户可以进行输入。
当玩家和怪物被击中时,我也对如何从他们那里进行分轨感到困惑。 这就是为什么我在每个块下设置变量以获得它们所拥有的点数。 这是错误的,但我不知道如何正确显示它。
我能够显示一些消息,但任何数字都会给我相同的结果,即玩家一号为 -1。
本质上,我一直在研究如何从这里用代码编写它。 任何帮助是极大的赞赏。
import java.util.Scanner;
public class Task3 {
public static void main(String[] args) {
task3(20);
}
public static void task3(int challenge) {
challenge = 10;
int player = 3;
int monster = 3;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your dice roll");
int diceRollOne = sc.nextInt();
while (player <= challenge) {
System.out.println("Monster misses");
System.out.println("Enter your dice roll");
int diceRollTwo = sc.nextInt();
continue;
if (player <= challenge) {
System.out.println("-1 for player");
player = 2;
System.out.println("Enter your dice roll");
int diceRollThree = sc.nextInt();
} else if (player > challenge) {
System.out.println("-1 for monster");
monster = 2;
System.out.println("Enter your dice roll");
int diceRollFour = sc.nextInt();
continue;
if (player <= challenge) {
System.out.println("-1 for player");
player = 1;
System.out.println("Enter your dice roll");
int diceRollFive = sc.nextInt();
continue;
if (player > challenge) {
System.out.println("-1 for monster");
monster = 1;
System.out.println("Enter your dice roll");
int diceRollSix = sc.nextInt();
continue;
} else if (player <= challenge) {
System.out.println("-1 for player");
player = 0;
System.out.println("Monster Wins");
int diceRollSeven = sc.nextInt();
continue;
if (player > challenge) {
System.out.println("-1 for monster");
monster = 0;
System.out.println("Player wins!");
int diceRollEight = sc.nextInt();
}
}
}
}
}
}
}
在编写 while 循环时,首先要考虑循环何时终止/循环何时继续的条件。 在您的情况下,您希望循环在player
或monster
变为0
时结束。 因此 while 循环继续运行的条件是相反的,即它们都> 0
。
然后想想你想在每个迭代中做什么。 在您的情况下,重复性任务是
challenge
进行比较最后,循环结束后,可以通过player
和monster
的值判断结果打印出来。
import java.util.Scanner;
public class Task3 {
public static void main(String args[]) {
task3(10);
}
public static void task3(int challenge)
{
int player = 3;
int monster = 3;
int dice = 0;
Scanner sc = new Scanner(System.in);
while(player > 0 && monster > 0)
{
System.out.println("Enter your dice roll");
dice = sc.nextInt();
if(dice > challenge)
{
monster--;
}
else
{
player--;
}
}
if(player > monster)
{
System.out.println("Player wins!");
}
else
{
System.out.println("Monster wins!");
}
}
}
PS 尝试理解代码而不是仅仅复制粘贴到你的作业中:)
您不需要单独的变量来从扫描仪获取每个输入。
您可以在 while 循环中每次获取输入并将值与挑战进行比较。
只有当玩家或怪物变为零时,我们才会退出循环。 一旦在循环之外,您可以检查谁赢了并相应地打印结果。
import java.util.Scanner;
public class Task3 {
public static void main(String[] args) {
task3(10);
}
public static void task3(int challenge) {
int player = 3;
int monster = 3;
Scanner sc = new Scanner(System.in);
int diceRoll;
while (player == 0 || monster == 0) {
System.out.println("Enter your dice roll");
diceRoll = sc.nextInt();
if(player < challenge)
player--;
else
monster--;
}
if(player < monster)
System.out.println("Player wins!");
else
System.out.println("Monster Wins");
}
}
如果你只想要 1 到 20 之间的数字作为输入,你还应该添加一个 if 条件,在获取输入后检查这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.