Chris Pickett
Chris Pickett

Reputation: 57

Java while loop and other

I need help I am new to Java programming and I don't know how to fix my code.

I am trying to make a 007 game. I have created the if statements and it isn't looping around. If I add a ! in front of the each statement in the do-while loop it causes a infinity loop.

How can I fix my programming.

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

  }
}

Upvotes: 0

Views: 295

Answers (4)

vortex7
vortex7

Reputation: 381

First of all: In order for the loop to work well, the question to the user and the computer random decision have to be inside the loop. In this way each loop has a new set of values for the variables.

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

    ...

Second: You need to have an explicit decision when to terminate the loop (ie the game). This can be done with one of the following

  • the user wants to end the game (that needs one more option quit in the user choices)
  • the computer wants to end the game (that needs a forth random number that means quit).
  • some condition from the combination of user/computer ammo and user/computer shoot. That is you can end the game if some player is shot and has zero ammo.

In all these cases you need to

  • declare a variable for the end of the game before the loop
  • decide what to do in the loop
  • continue the loop if not end of game

This is snippet for this:

boolean endOfGame = false;
...
do {
    ...
    if (  INput.equals("quit") 
       || (INput.equals("shoot") && ammo == 0)
       || (Userammo == 0 && output == 1) ) {
         endOfGame = true;
    } 
    ...
} while (!endOfGame);

You could have the same efect if you put the decision in the while, but in this way the decision for the end of game is more clear. It is also possible to set the endOfGame to true in many different places in the loop.

Edit: Some notes on coding style etc

  • It is a common good practice the names of the variables to start with low case letters (eg instead of Userammo use userAmmo). Names starting with capital letters denote classes.
  • It makes the code easier to read if the names have some meaning. For example I would suggest the following changes for some of your variables
    • INput → userAction
    • Userammo → userAmmo
    • output → compAction
    • ammo → compAmmo
  • Use final constants to give meaning to numbers or strings that appear in your code repeatedly.
  • Start the if statement on a new line. (else if on the same line is ok)

Using these you could have written (and some comments are not needed)

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

Upvotes: 0

Treston
Treston

Reputation: 3

All your && cases that are evaluated for this loop result in a false.

Let's say, T for all true cases and F for all F cases

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

So you need to re-evaluate what condition you want to cause to loop.

Are we looking for

(output==3 && Input=="shoot") || (output==1 && input=="reload") || (output==1 && input=="shoot") 

to cause the next iteration?

This would make the case, where any of them are T, we get T. If all of them are F, then we get F.

Upvotes: 0

Linga
Linga

Reputation: 10553

As per your condition as below

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

the loop will be executed only if the 3 conditions are true.

You need to make some changes in your condition

Upvotes: 0

Achintya Jha
Achintya Jha

Reputation: 12843

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

should be

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

Upvotes: 2

Related Questions