Nub - 1 year ago 54

Java Question

First of all, I have found two other threads that have similar questions. There problem is that they didn't use the right equal sign for string and not formatting the if statements correctly for their particular problem.

For my assignment, I need to create a game called Pig where the player verses a computer in getting 100 points first in rolling pairs of dice. If the player rolls 1 on one turn, they get no additional points. If the player rolls two 1's, then they lose all their points. I have not coded the computer's turn yet, just focusing on the player. Please tell me what I am doing wrong. Thank you very much in advance.

`import java.util.Scanner;`

public class FourFive

{

public static void main (String[] args)

{

Pigs myopp = new Pigs();

Scanner scan = new Scanner (System.in);

final int Round = 20;

int num1, num2;

int roundTotal = 0;

int playerTotal = 0;

int compTotal = 0;

int win = 100;

int turnOver = 1;

Pigs die1 = new Pigs();

Pigs die2 = new Pigs();

String play = "y";

System.out.println("Type y to play");

play = scan.nextLine();

`while (play.equalsIgnoreCase("y"))`

{

for (int roll = 1; roll <= 6; roll++)//Each die has 6 sides

{

num1 = die1.roll();//rolls first die

num2 = die2.roll();//rolls second die

int points = num1 + num2;// adds dies up to get total for this turn

System.out.println("You roll " + num1 + " and " + num2);

if (num1 == 1 || num2 == 1)//If either of the dies roll 1, no points

points += 0;

else if (num1 == 1 && num2 == 1)//if both are 1, lose ALL points

playerTotal = 0;

else

System.out.println("you earned " + points + " this round");

playerTotal += points; total number of points per round added

System.out.println("You have a total of " + playerTotal);

System.out.println("Type y to play");

play = scan.nextLine();

}

}

}

}

My Output:

`Type y to play`

y

You roll 4 and 2

you earned 6 this round

You have a total of 6

Type y to play

y

You roll 6 and 5

you earned 11 this round

You have a total of 17

Type y to play

y

You roll 1 and 1

You have a total of 19 //total should be 0 because of the two 1's

Type y to play

y

You roll 6 and 3

you earned 9 this round

You have a total of 28

Type y to play

y

You roll 1 and 1

You have a total of 30 //total should be 0 because of the two 1's

Type y to play

y

You roll 6 and 4

you earned 10 this round

You have a total of 40

Type y to play

y

You roll 5 and 2

you earned 7 this round

You have a total of 47

Type y to play

y

You roll 5 and 1

You have a total of 53 //shouldn't add any additional points because of the "1"

Type y to play

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Your if logic is flawed and a bit redundant. Try this:

```
if (num1 == 1 && num2 == 1) {
playerTotal = 0;
}
else if (num1 != 1 && num2 != 1) {
playerTotal += points;
System.out.println("you earned " + points + " this round");
}
System.out.println("You have a total of " + playerTotal);
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**