首页 > 解决方案 > Java程序计算我在另一个星球上的重量

问题描述

我无法让我的程序正常工作。我可以编译并运行它,但是在我选择了重量(磅)或重量(公斤),并且输入了您当前的重量之后。你要在你想知道你的体重在哪个星球上做出选择,但我无法正确地让它打印出正确的格式和转换。

请让我知道我错过了什么或做错了什么!

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();
   }        
  }
}

标签: java

解决方案


您遇到了问题,因为您有许多变量都命名为相同的事物,并且在不同的范围内。现在只考虑以磅 (lb) 为单位的重量。

  1. 你有一个非静态的类级变量:int weightlbs;. 这永远不会填充。
  2. 在该weight()方法中,您声明一个新int weightlbs的并用用户输入的重量填充它。然后你把它扔掉。
  3. 在 中planetSelect(),您声明一个双 weightlbs 并填充它:double weightlbs = in.nextDouble(). 然后在计算中使用它。

首先,如果您希望用户在weight()方法中输入他们的体重,您需要以方法可以访问的方式保存他们的提示结果planetSelection()。最简单的方法是将这些类变量设为静态。

然后,在您的planetSelection()方法中,您将需要使用先前设置的值。

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.
}

我们已将您的变量合并到此处的两个静态类级别变量中。只要我们填充这些变量,这些值将始终可用于此类中的其他方法。随着您进入更高级的主题,您将更多地了解为什么静态变量和方法可能具有潜在危险,但对于您当前的问题,您会很好地使用它们。


推荐阅读