Justin Cason
Justin Cason

Reputation: 3

Java program to calculalate how much I weigh on another planet

I am having trouble getting my program to work properly. I can compile it and run it, but after I select the choices of weight in pounds or weight in kg's, and after you input your current weight. You are to make a choice on what planet you want to know what your weight is on, but I cannot properly get it to print the proper format and conversion.

Please let me know what I am missing or have done wrong!

import java.util.Scanner;

public class Planetweight {    

  public static void main(String[] args) { 

        System.out.println(" ===== How much do I weigh on other planets? =====\n");       
        weight(); 
        planetSelection();    
  } 

   int weightlbs;
   int weightkg;
   int planetNumber;

   public static void weight() {  
   boolean exit = false;
   Scanner in = new Scanner(System.in);
   System.out.println("\n1). Please type 1 and press enter, to know the weight in pounds." + "\n2). Please type 2 and press enter, to know the weight in kg." + "\n3). Please type 3 to exit this program.");
   int choice = in.nextInt();
   if (choice == 1)
   {
      System.out.println("You have chosen weight in lbs, please enter your current weight in pounds.");
      int weightlbs = in.nextInt();
   }
   if (choice == 2)
   {
      System.out.println("You have chosen weight in kg, please enter your weight in kilograms.");
      int weightkg = in.nextInt();
   }
   if (choice == 3)
   {
      System.out.println("Ooops.. Something is not quite right, please try again later!");
      exit = true;
   }    }
   public static void planetSelection()     {
        Scanner in = new Scanner(System.in);
        System.out.println("Choose a Planet in our Solar System: \n 1). Mercury \n 2). Venus \n 3). Earth \n 4). Moon \n 5). Mars \n 6). Jupiter \n 7). Saturn \n 8). Uranus \n 9). Neptune \n 10). Pluto \n Select one planet from (1-10");
   int choice = in.nextInt();
   if (choice == 1)
   {
      double weightlbs = in.nextDouble();
      System.out.printf("%.1f Your weight on Mercury is: ", weightlbs, (weightlbs * 0.4155));
      weight();
   }        
  }
}

Upvotes: 0

Views: 2968

Answers (2)

Federico Vera
Federico Vera

Reputation: 1357

There are a couple of issues with the code:

  1. Methods are static but attributes aren't
  2. Attributes are being redeclared
  3. You are asking for the weight value again
  4. Values are ints but you are trying to print doubles
  5. You are not showing the resulting weight
  6. Most (if not all) of those if-else can be removed

This should work:

import java.util.Scanner;

public class Planetweight {

      public static void main(String[] args) {
        System.out.println(" ===== How much do I weigh on other planets? =====\n");
        weight();
        planetSelection();
    }

    static double weightlbs;
    static double weightkg;
    static int planetNumber;

    public static void weight() {
        boolean exit = false;
        Scanner in = new Scanner(System.in);
        System.out.println("\n1). Please type 1 and press enter, to know the weight in pounds." + "\n2). Please type 2 and press enter, to know the weight in kg." + "\n3). Please type 3 to exit this program.");
        int choice = in.nextInt();
        if (choice == 1)
        {
          System.out.println("You have chosen weight in lbs, please enter your current weight in pounds.");
          weightlbs = in.nextDouble();
        }
        if (choice == 2)
        {
          System.out.println("You have chosen weight in kg, please enter your weight in kilograms.");
          weightkg = in.nextDouble();
        }
        if (choice == 3)
        {
          System.out.println("Ooops.. Something is not quite right, please try again later!");
          exit = true;
        }
    }

    public static void planetSelection()     {
        Scanner in = new Scanner(System.in);
        System.out.println("Choose a Planet in our Solar System: \n 1). Mercury \n 2). Venus \n 3). Earth \n 4). Moon \n 5). Mars \n 6). Jupiter \n 7). Saturn \n 8). Uranus \n 9). Neptune \n 10). Pluto \n Select one planet from (1-10");
        int choice = in.nextInt();
        if (choice == 1)
        {
            System.out.printf("Your weight on Mercury is: %.1f lbs", (weightlbs * 0.4155));
            weight();
        }
    }
}

Upvotes: 1

Roddy of the Frozen Peas
Roddy of the Frozen Peas

Reputation: 15180

You're having an issue because you have a number of variables all named the same thing, and in different scopes. Consider just weights in pounds (lb) for now.

  1. you have a non-static class-level variable: int weightlbs;. This is never populated.
  2. in the weight() method, you declare a new int weightlbs and populate it with the user's entered weight. Then you throw it away.
  3. in the planetSelect(), you declare a double weightlbs and populate it: double weightlbs = in.nextDouble(). You then use this in your calculation.

First, if you want the user to enter their weight in the weight() method, you'll need to save the result of their prompts in a way that can be accessed by the planetSelection() method. The easiest way would be to make those class variables static.

Then, in your planetSelection() method, you'll need to use that previously set value.

public class Planetweight {
  // weights, initialized to -1 to indicate they've not been set yet
  static double weightlbs = -1.0;
  static double weightkg = -1.0;

  public static void weight() {
    Scanner in = new Scanner(System.in);
    System.out.println("\n1). Please type 1 and press enter, to know the weight in pounds." + "\n2). Please type 2 and press enter, to know the weight in kg." + "\n3). Please type 3 to exit this program.");
    int choice = in.nextInt();
    if(choice == 1) {
       System.out.println("You have chosen weight in lbs, please enter your current weight in pounds.");
       weightlbs = in.nextDouble(); // notice no "double" -- we're setting the class variable
    } else if (choice == 2) {
       System.out.println("You have chosen weight in kgs, please enter your current weight in pounds.");
       weightkg = in.nextDouble();
    } else {
      System.out.println("Goodbye.");
      System.exit(1);
    }
  }

  public static void planetSelection() {
    Scanner in = new Scanner(System.in);
    System.out.println("Choose a Planet in our Solar System: \n 1). Mercury \n 2). Venus \n 3). Earth \n 4). Moon \n 5). Mars \n 6). Jupiter \n 7). Saturn \n 8). Uranus \n 9). Neptune \n 10). Pluto \n Select one planet from (1-10");
    int choice = in.nextInt();
    if (choice == 1) {
      double weightOnMercuryLbs = weightlbs * 0.4155;
      System.out.println("Your weight on earth: " + weightLbs + " lbs. On Mercury you weigh: " + weightOnMercuryLbs + " lbs.");
    }    
  }
  // Main method omitted for brevity.
}

We've consolidated your variables to the two static class level variables here. As long as we populate those variables, those values will always be available to other methods in this class. As you move into more advanced topics you'll learn more about why static variables and methods can potentially be dangerous, but for your current problem, you'll be fine using them.

Upvotes: 1

Related Questions