Kenny Bowen Kenny Bowen - 1 month ago 17
Java Question

Fish simulation game error

I have a A code for class that requires this:

The problem
For this assignment, you will write a program that simulates a fishing game. In this game, a six-sided die is rolled to determine what the user has caught. Each possible item is worth a certain number of fishing points. The points will remain hidden until the user is finished fishing, and then a message is displayed congratulating the user, depending on the number of fishing points gained.

Here are some suggestions for the game's design:

Each round of the game is performed as an iteration of a loop that repeats as long as the player wants to fish for more items.

At the beginning of each round, the program will ask the user whether or not he or she wants to continue fishing.

The program simulates the rolling of a six-sided die

Each item that can be caught is represented by a number generated from the die; for example, 1 for "huge fish", 2 for "an old shoe", 3 for a "little fish", and so on.

Each item the user catches is worth different amount of points
The loop keeps a running total of the user's fishing points.

After the loop has finished the total number of fishing points is displayed, a long with a message that varies depending on the points earned.

The thing is, when I execute the code, It keeps giving me zero as the final number?

Anyone know what I can do to fix this?

import java.util.Random;
import java.util.Scanner;
import java.io.*;

public class apples {

public static void main(String[] args) {
Random dice = new Random();
Scanner kenny = new Scanner(System.in);
int die, choice;
int score = 0;
int points = 0;
int counter = 0;

//Opening statement
System.out.println("Let's go fishing!!!");
System.out.println("");

System.out.println("Would you like to fish?");
System.out.println("Type 1 for yes");
System.out.println("Type 2 for no");

//Variables set

//issue making the loop

do {
die = dice.nextInt(5);
choice = kenny.nextInt();

if (die == 0)
System.out.println("You caught: Aquaman, haha nice. +20");
points += 20;

if (die == 1)
System.out.println("You caught: A shark!! -30");
points -= 30;

if (die == 2)
System.out.println("You caught..Oh, there's my keys. +30");
points += 30;

if (die == 3)
System.out.println("You caught: Nemo. Come on man, that's not cool. -50");
points -= 50;

if (die == 4)
System.out.println("You caught: A still working copy of fight club! +50");
points += 50;

System.out.println("");

if (die == 5)
System.out.println("You caught: Iggy Azalea's newest album. Burn it. Now. -20");
points -= 20;

System.out.println("Would you like to fish?");
System.out.println("Type 1 for yes");
System.out.println("Type 2 for no");

counter++;

} while (choice == 1);

if (choice == 2) {

System.out.println("Game Over");
System.out.println("");
System.out.println("Your final score is: " + (points));

if (points > 0)
System.out.println("Yay! you won!");
if (points < 0)
System.out.println("Oh no! You lost :(");
System.out.println("Thanks for playing!");
}
}
}

Answer

Your indentation should be a big clue. Take this statement:

if (die == 0)
    System.out.println("You caught: Aquaman, haha nice. +20");
points += 20;

Since you don't have brackets around this code, the points += 20 line is executed independently of the if statement. All your point-changing code cancels itself out, so each time through the loop you still wind up with 0 at the end.

You want this on each if statement:

if (die == 0)
{
    System.out.println("You caught: Aquaman, haha nice. +20");
    points += 20;
}