Question

My point tracker is reseting the points for both players to the original number every time I do it more than once.

I can't figure out why whenever I take away more life-points from either player, instead of using the previous number of life-points, it just resets to whatever I made the life-points start out on and goes form there.

EX: player-1 has 100 life points. I take away 1 life-point. player-1 now has 99 life-points. Now I do it again. I take away 1 more life-point from player-1. But now, instead of taking 1 away from the previous life-point count of 99, it takes 1 away from 100 again and gives me 99 a second time.

I can't tell where I made a mistake that keeps reseting the scores.

 package yu.gi.oh.life.point.counter;

    import java.util.Scanner;
    public class YuGiOhLifePointCounter {

        static Scanner sc = new Scanner(System.in);
        public static void main(String[] args) {

            double choice_1;
            System.out.print("\nEnter the starting life points for player-1: ");
            choice_1 = sc.nextDouble();

            double storage_1;
            storage_1 = choice_1;

            double choice_2;
            System.out.print("\nEnter the starting life points for player-2: ");
            choice_2 = sc.nextDouble();

            double storage_2;
            storage_2 = choice_2;

            double lp1;
            System.out.print("\nEnter a 6 to change the life-points of either player: ");
            lp1 = sc.nextDouble();


            while (lp1 == 6) {

                    double choose_1_2;
                    System.out.print("\nEnter a 1 to change player-1's life-points, or enter a 2 to change player-2's life-points: ");
                    choose_1_2 = sc.nextDouble();

                    if (choose_1_2 == 1) {
                        double ch_1;
                        System.out.print("\nEnter the number subtracted from or added to player-1's life-points: ");
                        ch_1 = sc.nextDouble();
                        double c_1;
                        System.out.print("\nEnter a 1 to subtract this number from player-1's life-points, or enter a 2 to add this number to player-1's life-points: ");
                        c_1 = sc.nextDouble();

                        double display_1;

                        if (c_1 == 1) {
                            display_1 = storage_1 - ch_1;
                            System.out.println("\nPlayer-1's life-points are currently " + display_1);
                        }
                        if (c_1 == 2) {
                            display_1 = storage_1 + ch_1;
                            System.out.println("\nPlayer-1's life-points are currently " + display_1);
                        }                
                    }
                    if (choose_1_2 == 2) {
                        double ch_2;
                        System.out.print("\nEnter the number subtracted from or added to player-2's life-points: ");
                        ch_2 = sc.nextDouble();
                        double c_2;
                        System.out.print("\nEnter a 1 to subtract this number from player-2's life-points, or enter a 2 to add this number to player-1's life-points: ");
                        c_2 = sc.nextDouble();

                        double display_2;

                        if (c_2 == 1) {
                            display_2 = storage_2 - ch_2;
                            System.out.println("\nPlayer-2's life-points are currently " + display_2);
                        }
                        if (c_2 == 2) {
                            display_2 = storage_2 + ch_2;
                            System.out.println("\nPlayer-2's life-points are currently " + display_2);

                        }                   
                    }
                  lp1 = 6;
                }

              }
           }
Was it helpful?

Solution

You never change the storage_1/2 values.

A line like display_2 = storage_2 - ch_2; will keep calculating the difference with the original number of life points (100), instead of subtracting from the previously calculated amount. Try updating these storage_x values after you calculate the display_x values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top