gpt4 book ai didi

Java while循环及其他

转载 作者:行者123 更新时间:2023-12-02 07:09:26 29 4
gpt4 key购买 nike

我需要帮助,我是 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"));

}
}

最佳答案

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

关于Java while循环及其他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15716926/

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