Loops and conventions in Java

Change Part of the string comparison has been resolved, but the code still does not go to the next player when rolling only one 1 out of 2 dice, only when both dice are 1 s, it goes to the next player.

Here is the code so far, the first class was pretty much from the tutorial, and the second class with the main method is what I did.

The program is trying to create a dice game called a pig . Simple rules are at the bottom of the code if they are interested, but the main problems.

I have that it doesn't loop properly when I don't put y in the scanner to scroll it again again as soon as I entered y when I don't. In addition, if do not work correctly, because when a player rolls 1 with one of the dice, he does not move on to the next player.

Sorry if I did not explain the problem properly. Also I do not want to use more methods or classes. I guess there are faster ways to achieve this than how I do it, but this is for later if I want to use additional methods in my code. I also have a problem with point of delivery, because sometimes it does not change them properly, but I can understand it as soon as the rest works.

Here is the first bit of code. This code is not very important for the problem, but if you want to know what methods I call in main , you can look at them:

 import java.util.Random; public class PairOfDice { private final int MAX = 6; private int faceValue; private int faceValue1; Random generator0 = new Random(); Random generator1 = new Random(); public PairOfDice(){ faceValue = 1; faceValue1 = 1; } public int getFaceValue() { return faceValue; } public void setFaceValue(int faceValue) { this.faceValue = faceValue; } public int getFaceValue1() { return faceValue1; } public void setFaceValue1(int faceValue1) { this.faceValue1 = faceValue1; } public int rollOne() { faceValue = generator0.nextInt(MAX) + 1; return faceValue; } public int rollTwo() { faceValue1 = generator1.nextInt(MAX) + 1; return faceValue1; } public int sumOfRoll() { return faceValue + faceValue1; } @Override public String toString() { return "First roll of the die: \t" + rollOne() + "\nSecond roll of the die: " + rollTwo() + "\nThe sum of both rolls: \t" + sumOfRoll(); } } 

The next bit of code is my own. I updated some things in the code using .equals now that I am comparing the string, and I changed the while conditions and simplified the if statements a bit.

 public class NewClass { public static void main(String[] args) { PairOfDice player1 = new PairOfDice(); PairOfDice player2 = new PairOfDice(); Scanner scan = new Scanner(System.in); int p1 = 33, p2 = 0, turnp1 = 0, turnp2 = 0, signal = 1; while (p1 <= 100 || p2 >= 100) { int newp1total = p1; turnp1 = 0; while (turnp1 <= 20 && signal == 1) { System.out.println("Player 1s Turn!"); int die1 = player1.rollOne(); int die2 = player1.rollTwo(); int sumofdice = player1.sumOfRoll(); System.out.println("Player 1: First Die: " + die1 + " Second Die:" + die2 + " Sum of Roll: " + sumofdice); if (sumofdice == 2) { p1 = 0; turnp1 = 0; signal = -1; System.out.println("Player rolled a two 1s. All players points are forfeited. Next Players turn."); System.out.println("Points this turn:" + turnp1); System.out.println("Points this game: " + p1); System.out.println(); } else if (die1 == 1 || die2 == 1) { turnp1 = 0; signal = -1; p1 = newp1total; System.out.println("Player rolled a 1. All points on this round are forfeited. Next Players turn."); System.out.println("Points this turn:" + turnp1); System.out.println("Points this game: " + p1); System.out.println(); } else { turnp1 += sumofdice; p1 += sumofdice; System.out.println("Points this turn:" + turnp1); System.out.println("Points this game: " + p1); System.out.println(); signal = 1; } } signal = 1; String yesno = "y"; int newp2total = p2; while (yesno.toLowerCase().equals("y") && signal == 1) { System.out.println("Player 2s Turn!"); int die1 = player2.rollOne(); int die2 = player2.rollTwo(); int sumofdice = player2.sumOfRoll(); System.out.println("Player 2: First Die: " + die1 + " Second Die:" + die2 + " Sum of Roll: " + sumofdice); if (sumofdice == 2) { p2 = 0; turnp2 = 0; signal = -1; System.out.println("Player rolled a two 1s. All players points are forfeited. Next Players turn."); System.out.println("Points this turn:" + turnp2); System.out.println("Points this game: " + p2); System.out.println(); } else if (die1 == 1 || die2 == 1) { signal = -1; turnp2 = 0; p2 = newp2total; System.out.println("Player rolled a 1. All points on this round are forfeited. Next Players turn."); System.out.println("Points this turn:" + turnp2); System.out.println("Points this game: " + p2); System.out.println(); } else { turnp2 += sumofdice; p2 += sumofdice; System.out.println("Points this turn:" + turnp2); System.out.println("Points this game: " + p2); System.out.println(); System.out.println("Try your luck? Y/N"); yesno = scan.next(); System.out.println(); signal = 1; } } } } } 
+4
source share
2 answers

First of all, and this is an important concept, when comparing strings ALWAYS use .equals, not ==. for instance

 yesno.toLowerCase() == "y" && signal == 1 

it should be

  yesno.toLowerCase().equals("y") && signal.equals 1 

This is because y is a string. If y was int y, you would use ==. If you were comparing y and you were constantly changing the values, you would use =. Next thing; I understand that using getters and setters can be part of the assignment from your tutorial, but if it is not; DO NOT USE THEM.

Read this article .

It is very difficult to make code. Another useful work in code, I like that you respect the Java code convention!

Also, be sure to use .equals for strings and == for integers. If you are comparing integers that I believe you are for an int variable, you need to use ==. In addition, I think you may be confused and / or concepts. If you have an if statement and you are comparing things and need the BOTH statements to be true, use "& &" if you only need one ONE statement to be true, use || (this key is the key above shift input)

+4
source
  • You compare the string with == , which results in a reference equality comparison: Replace

     while (yesno.toLowerCase() == "y" && signal == 1) { 

    from

     while (yesno.toLowerCase().equals("y") && signal == 1) { 

    or

     while (yesno.equalsIgnoreCase("y") && signal == 1) { 

    == compared to equals() :

    == tests for referential equality (i.e. whether they are the same objects that belong to the same row in the row pool).

    But, .equals() checks for equality of values ​​(regardless of whether their contents are the same or not).

    But be careful with null strings. == can handle these null strings fine, but calling .equals() from a null string will throw an exception because the method is called using a null object.

    For more information, you can refer to String Pools

  • Replace operator:

     while (p1 <= 100 ^ p2 >= 100) { 

    from

     while (p1 <= 100 || p2 >= 100) { 

    Consider:

     int p1=1; int p2=2; if( p1==1 ^ p2==2){ // False here since it is TRUE^TRUE which is FALSE System.out.println("Same"); } if( p1==1 || p2==3){ // TRUE if either one is TRUE System.out.println("Same Again"); } 

    Only imprinted

    Again and again

    This is because ^ is an XOR bitwise operator that is false if and only if both operands have the same truth value.

  • Replace the two else if equivalent ones

     else if ( die2 == 1 || die1 == 1 ) { 
+2
source

All Articles