Java while循环及其他

标签 java loops logic

我需要帮助,我是 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/

相关文章:

r - 如何在 R 中的 foreach 循环中使用多重估算数据集(mids)对象?

c++ - C++中的重复循环

java - 谷歌数据流 : Write to Datastore without overwriting existing entities

java - 如何获取并使用 session 对象来填充 JSF 中的数据表?

java - 从多个txt文件中读取整数并将值相加

R 中 Cor() 的结果没有按照我的预期进行逻辑评估

jquery - 使用 jquery 在选项选择的基础上添加新的表单输入字段

java - 在日志中按字母顺序排序?

java - 验证所有场景的正则表达式

python - 为什么这些语句不返回 'true' ?