我不能让我的程序正常工作。我可以编译并运行它,但在我选择以磅为单位的重量或以千克为单位的重量之后,以及在您输入当前重量之后。你要选择你想知道你的体重在哪个星球上,但我不能正确地让它打印出正确的格式和转换。
请让我知道我错过了什么或做错了什么!
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();
}
}
}
发布于 2018-10-27 04:17:11
代码中有几个问题:
这应该是可行的:
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();
}
}
}
发布于 2018-10-27 04:17:11
你有一个问题,因为你有许多变量都命名相同的东西,在不同的作用域。现在只考虑以磅(lb)为单位的重量。
int weightlbs;
。这永远不会被填充。weight()
方法中,声明一个新的int weightlbs
并用用户输入的权重填充它。然后丢弃它。planetSelect()
中,声明一个double weightlbs并填充它:double weightlbs = in.nextDouble()
。然后在您的calculation.中使用它
首先,如果希望用户在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.
}
在这里,我们已经将您的变量合并为两个静态类级别变量。只要我们填充这些变量,这些值将始终可用于此类中的其他方法。随着您进入更高级的主题,您将了解更多关于为什么静态变量和方法可能是危险的,但是对于您当前的问题,您可以使用它们。
https://stackoverflow.com/questions/53015572
复制相似问题