2013-03-30 47 views
0

我需要帮助我是Java编程的新手,我不知道如何修复我的代码。Java while循环和其他

我想做一个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

首先:为了使循环运行良好,对用户和计算机随机决定的问题必须在循环内部。通过这种方式,每个循环都有一组新的变量值。

// 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

编辑:于编码风格等

  • 它是一种常见的很好的做法变量的名字开始与低字母的一些注意事项(例如,而不是使用UserammouserAmmo)。以大写字母开头的名称表示类别。
  • 如果名称具有某些含义,它使代码更容易阅读。比如我会建议为你的一些变量
    • 输入→ userAction
    • Userammo → userAmmo
    • 输出→压实
    • 弹药的以下变化→ 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); 
    ... 
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"))); 
+0

我改变了我的代码,但是,我怎么让它去继续,直到有人失去 –

0

根据你的条件如下

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

仅在3个条件为真循环将被执行。

你需要在你的病情

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.