【问题标题】:Java while loop and otherJava while 循环等
【发布时间】:2023-03-11 09:26:01
【问题描述】:

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

  }
}

【问题讨论】:

    标签: java loops logic


    【解决方案1】:
    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")));
    

    【讨论】:

    • 我改变了我的代码,但是,我如何让它继续下去,直到有人输了
    【解决方案2】:

    首先:为了使循环正常工作,用户的问题和计算机的随机决定必须在循环内。这样,每个循环都有一组新的变量值。

    // 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)。
    • 用户/计算机弹药和用户/计算机射击组合的一些条件。也就是说,如果某个玩家被击中并且弹药为零,您可以结束游戏。

    在所有这些情况下,您都需要

    • 在循环之前声明一个游戏结束的变量
    • 决定在循环中做什么
    • 如果游戏没有结束则继续循环

    这是sn-p:

    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)。以大写字母开头的名称表示类别。
    • 如果名称有某种含义,它会使代码更易于阅读。例如,我会建议对您的一些变量进行以下更改
      • 输入 → 用户操作
      • 用户弹药 → 用户弹药
      • 输出 → 压缩动作
      • 弹药 → compAmmo
    • 使用 final 常量为代码中重复出现的数字或字符串赋予意义。
    • 在新行上开始 if 语句。 (否则如果在同一行就可以)

    使用这些您可以编写(并且不需要一些 cmets)

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

    【讨论】:

      【解决方案3】:

      根据您的情况如下

      while((output == 3 && INput == "shoot") && (output == 1 && INput == "reload") && (output ==  1 && INput == "shoot"));
      

      只有当 3 个条件为真时才会执行循环。

      你需要对你的状况做一些改变

      【讨论】:

        【解决方案4】:

        针对此循环评估的所有 && 案例都会导致错误。

        假设 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。

        【讨论】:

          猜你喜欢
          • 2015-07-18
          • 2018-07-07
          • 2011-01-20
          • 1970-01-01
          • 1970-01-01
          • 2022-09-24
          • 2010-11-26
          • 2014-03-29
          相关资源
          最近更新 更多