我需要帮助我是Java编程的新手,我不知道如何修复我的代码。
我正在尝试制作007游戏。我创建了if
语句,它没有循环。如果我在!
- do
循环中的每个语句前面添加while
,则会导致无限循环。
如何修复编程。
import java.util.Scanner;
import java.util.Random;
public class DoubleOSeven {
public static void main(String[] args) {
System.out.println("Let's Play a game of 007 ");
System.out.println("You can SHOOT, BLOCK, AND RELOAD");
System.out.println("Both you and I start with one bullet");
Scanner input = new Scanner(System.in);
System.out.println("Let's Start, Enter (shoot, reload , or block) ");
String INput = input.nextLine();
//User and Computer ammo
int Userammo = 1;
int ammo = 1;
//Creates a random number between 1 and 3
Random rand = new Random();
int output = rand.nextInt(3) + 1;
do{
//User chooses shoot
if(INput.equals("shoot")){
Userammo --;
System.out.println("You took a shot, Your ammo count is at: " + Userammo);
//User chooses reload
}else if (INput.equals("reload")){
Userammo ++;
System.out.println("You reloaded, Your ammo count is at: " + Userammo);
//User chooses block
}else if(INput.equals("block")){
System.out.println("You blocked, Your ammo count is at: " + Userammo);
//If random is 1 shoot
}if(output == 1){
ammo ++;
System.out.println("I took a shot at you, My ammo count is at: " + ammo);
//If random is 2 block
}else if(output == 2){
System.out.println("I blocked, My ammo count is at: " + ammo);
//If random is 3 reload
}else if(output == 3){
ammo ++;
System.out.println("I reloaded, My ammo count is at: " + ammo);
//If both User and Computer shoot
}if(output == 1 && INput == "shoot"){
System.out.println("It's a tie you we both die");
}
}while((output == 3 && INput == "shoot") && (output == 1 && INput == "reload") && (output == 1 && INput == "shoot"));
}
}
答案 0 :(得分:2)
while((output == 3 && INput == "shoot") && (output == 1 && INput == "reload") && (output == 1 && INput == "shoot"));
应该是
while((output == 3 && INput.equals("shoot")) || (output == 1 && INput.equals("reload")) || (output == 1 && INput.equals("shoot")));
答案 1 :(得分:0)
根据您的情况如下
while((output == 3 && INput == "shoot") && (output == 1 && INput == "reload") && (output == 1 && INput == "shoot"));
仅当3个条件为真时才会执行循环。
您需要对条件进行一些更改
答案 2 :(得分:0)
所有&&对此循环求值的情况会导致错误。
假设所有真实案例都是T,所有F案件都是F
If T&&T&&T then yes we = T (the case for adding !)
If T&&F&&F then F
IF F&&T&&F then F
...
IF F&&F&&F then F
因此,您需要重新评估您希望循环的条件。
我们正在寻找
(output==3 && Input=="shoot") || (output==1 && input=="reload") || (output==1 && input=="shoot")
导致下一次迭代?
这样就可以了,其中任何一个都是T,我们得到T.如果所有这些都是F,那么我们得到F.
答案 3 :(得分:0)
首先:为了使循环运行良好,对用户和计算机随机决策的问题必须在循环内。通过这种方式,每个循环都有一组新的变量值。
// Declare variables
String INput = "";
Random rand = new Random();
int output = 0;
do{
// Ask user
System.out.println("Enter (shoot, reload , or block) ");
INput = input.nextLine();
// Computer decision
output = rand.nextInt(3) + 1;
...
第二:你需要明确决定何时终止循环(即游戏)。这可以通过以下之一
完成quit
)quit
)。在所有这些情况下,你需要
这是以下内容的摘录:
boolean endOfGame = false;
...
do {
...
if ( INput.equals("quit")
|| (INput.equals("shoot") && ammo == 0)
|| (Userammo == 0 && output == 1) ) {
endOfGame = true;
}
...
} while (!endOfGame);
如果您将决定放在while
中,您可以获得相同的效果,
但通过这种方式,游戏结束的决定更加明确。
也可以在循环中的许多不同位置设置endOfGame
到true
。
编辑:有关编码风格等的一些注意事项
Userammo
使用userAmmo
)。以大写字母开头的名称表示类。if
语句。 (否则如果在同一条线上就可以了)使用这些你可以编写(并且不需要一些注释)
public class DoubleOSeven {
public static final String S_SHOOT = "shoot";
public static final String S_BLOCK = "block";
public static final String S_RELOAD = "reload";
public static final int I_SHOOT = 1;
public static final int I_BLOCK = 2;
public static final int I_RELOAD = 3;
...
System.out.println("Enter ("+ S_SHOOT + ", " + S_RELOAD + " or " + S_BLOCK + ") ");
...
} else if(userAction.equals(S_BLOCK)){
System.out.println("You blocked, Your ammo count is at: " + userAmmo);
}
if(compAction == I_SHOOT){
compAmmo--; // I changed ++ to -- in order to be a fair game
System.out.println("I took a shot at you, My ammo count is at: " + compAmmo);
} else if(compAction == I_BLOCK){
System.out.println("I blocked, My ammo count is at: " + compAmmo);
...