首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >计算我在另一个星球上的重量的Java程序

计算我在另一个星球上的重量的Java程序
EN

Stack Overflow用户
提问于 2018-10-27 04:00:51
回答 2查看 1.6K关注 0票数 0

我不能让我的程序正常工作。我可以编译并运行它,但在我选择以磅为单位的重量或以千克为单位的重量之后,以及在您输入当前重量之后。你要选择你想知道你的体重在哪个星球上,但我不能正确地让它打印出正确的格式和转换。

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

代码语言:javascript
复制
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();
   }        
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-27 04:17:11

代码中有几个问题:

  1. 方法是双精度的,但是属性aren't
  2. Attributes正在被重新声明
  3. 您正在再次请求权重值
  4. 值是int,但是您正在尝试打印doubles
  5. 您没有显示生成的权重
  6. 其中大多数(如果不是全部) if-else可以被删除

这应该是可行的:

代码语言:javascript
复制
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();
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2018-10-27 04:17:11

你有一个问题,因为你有许多变量都命名相同的东西,在不同的作用域。现在只考虑以磅(lb)为单位的重量。

  1. 有一个非静态的类级变量:int weightlbs;。这永远不会被填充。
  2. weight()方法中,声明一个新的int weightlbs并用用户输入的权重填充它。然后丢弃它。
  3. planetSelect()中,声明一个double weightlbs并填充它:double weightlbs = in.nextDouble()。然后在您的calculation.

中使用它

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

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

代码语言:javascript
复制
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.
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53015572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档