Java while循环等

时间:2013-03-30 09:22:29

标签: 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"));

  }
}

4 个答案:

答案 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中,您可以获得相同的效果, 但通过这种方式,游戏结束的决定更加明确。 也可以在循环中的许多不同位置设置endOfGametrue

编辑:有关编码风格等的一些注意事项

  • 通常的好习惯是以小写字母开头的变量名称(例如,而不是Userammo使用userAmmo)。以大写字母开头的名称表示类。
  • 如果名称有一些含义,它使代码更容易阅读。例如,我建议您对某些变量进行以下更改
    • 输入→userAction
    • Userammo→userAmmo
    • 输出→compAction
    • ammo→compAmmo
  • 使用最终常量为重复出现在代码中的数字或字符串赋予意义。
  • 在新行上启动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);
    ...