简体   繁体   中英

Java - Main method with If statement printing order error

I appoligize in advance if this is a duplicate of any kind, but I couldn't find anything that addressed my specific problem.

Here is my program:

 import java.util.Random; public class CarnivalGame{ public static void main(String[] args){ int count = 0; int wins = 0; for (int i = 0; i < 101; i++){ System.out.println("Roll " + count); count = count + 1; int die1=(dieRoll(6)); int die2=(dieRoll(20)); int die3=(dieRoll(8)); int die4=(dieRoll(4)); int die5=(dieRoll(12)); int sum = die1+die2+die3+die4+die5; System.out.println("Total: " + sum + "\\n"); if ((sum >= 35) || (sum < 20)){ System.out.println("Player wins!\\n"); wins = wins + 1; } if (count == 100){ //PROBLEM AREA System.out.printf("After 100 rolls, the player has won for a total of %d times!\\n", wins); } }//end for loop }//end main public static int dieRoll(int sides){ int num = 0; int roll = 0; Random rng = new Random(); if(sides >=4){ for(int i = 0; i < 1; i++){ roll = rng.nextInt(sides)+1; System.out.println("Roll is: " + roll); num = num + roll; }//end for loop }//end if return num; }//end method }//end class 

What it currently prints out:

roll count = 100 in this example

For loop (x100):

---> "After 100 rolls, the player has won for a total of (wins) times!" Roll (count)

    Roll is:  dieRoll(x)
    Roll is:  dieRoll(x)
    Roll is:  dieRoll(x)
    Roll is:  dieRoll(x)
    Roll is:  dieRoll(x)
    Total:    sum

    if ((sum >= 35) || (sum < 20))
    "Player wins!"

What I'm trying to get it to print out:

Same exact thing but with

 if ((sum >= 35) || (sum < 20))
        "Player wins!"
---> "After 100 rolls, the player has won for a total of (wins) times!

So basically I'm trying to figure out why my problem area is printing along with the roll count, where it should be printing after the sum and "Player wins!"

I'm assuming it is an error with my if statement formatting, but I can't figure out how to fix it. Thanks for your time.

Your problem is that your loop has 101 iterations, from 0 to 100. So after number 99 your 100 dice rolls are over, and THEN you roll one additional time. Just change the loop to go from 0 to 99 (i<100 instead of 101).

Edit (mostly because I can't answer you directly yet):

As you (try) to iterate 100 times anyway, I don't really see why you would need to have the variable count at all. Just let your loop roll the dice a hundred times, add the wins, and then print your statement afterwards.

Sorry guys, really stupid logic error on my part. Here is the fix to my problem, as Jesper and Calvin P. suggested, I just had to put the problem area after the for loop. I was thinking I had to keep it within the loop. Thanks everyone for the help!

import java.util.Random;

public class CarnivalGame
{
public static void main(String[] args)
{
int count = 0;
int wins = 0;
for (int i = 0; i < 100; i++)
{
System.out.println("Roll " + (i + 1));
count = count + 1;
int die1=(dieRoll(6));
int die2=(dieRoll(20));
int die3=(dieRoll(8));
int die4=(dieRoll(4));
int die5=(dieRoll(12));
int sum = die1+die2+die3+die4+die5;
System.out.println("Total:    " +  sum + "\n");

if ((sum >= 35) || (sum < 20)){
System.out.println("Player wins!\n");
wins = wins + 1;
}
if (count == 100){
//FIXED PROBLEM
System.out.printf("After 100 rolls, the player has won for a total of %d times!\n", wins); 
}
}//end for loop
}//end main

public static int dieRoll(int sides)
{ 
   int num = 0;
   int roll = 0;
   Random  rng = new Random(); 
   if(sides >=4) 
      { 
      for(int i = 0; i < 1; i++)
         { 
         roll = rng.nextInt(sides)+1;
         System.out.println("Roll is:  " + roll);
         num = num + roll; 
   }//end for loop
   }//end if
return num;
}//end method   

}//end class

Your variable count is always one ahead of your iterator variable i because you increase it very early in the loop. So you'll reach count == 100 while i is still 99.

Move your output before the increase of count OR increate at the end of the loop maybe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM