gpt4 book ai didi

java - 尝试将参数传递给方法并使用 if - else if 语句尝试将用户引导至特定方法

转载 作者:行者123 更新时间:2023-11-30 06:48:20 26 4
gpt4 key购买 nike

我正在尝试将参数传递给方法,但由于showKilometer(int num){ 方法已经有一个参数通过,if-else if 语句不起作用,我一直在尝试更改 showKilometer();显示公里(int num);在 if-else if 语句下,但这似乎也不起作用

import java.util.Scanner;

public class a6main {

public static void main(String[] args){

double distanceMeter;
double distanceKilom;
double distanceInches;
double distanceFeet;

System.out.println("Enter a distance in meters:");
Scanner keyboard = new Scanner(System.in);
distanceMeter = keyboard.nextDouble();

while (distanceMeter <= 0){
System.out.println("Unable to atain distance less than 0,\n"
+ "please enter a number grater than 0:\n");
distanceMeter = keyboard.nextInt();
}
showKilometer(distanceMeter);
menu();
}

public static void menu(){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your choice\n"
+ "1. Convert to kilometers\n"
+ "2. Convert to inches\n"
+ "3. Conveert to feet\n"
+ "4. Quit the program");
int choice = keyboard.nextInt();
while (choice <= 0 || choice >= 5){
System.out.println("Option unavailble, pleace select a choice from 1 - 4:\n"
+ "1. Convert to kilometers\n"
+ "2. Convert to inches\n"
+ "3. Conveert to feet\n"
+ "4. Quit the program");
choice = keyboard.nextInt();
}
if(choice == 1)
showKilometer();
else if(choice == 2)
showInches();
else if(choice == 3)
showFeet();
else
System.out.println("Program terminated.");
}

public static void showKilometer(int num){
System.out.println(num);
}
}

最佳答案

代码应如下所示:

import java.util.Scanner;

public class A6Main {

public static void main(String[] args){

double distanceMeter;
double distanceKilom;
double distanceInches;
double distanceFeet;

System.out.println("Enter a distance in meters:");
Scanner keyboard = new Scanner(System.in);
distanceMeter = keyboard.nextDouble();

while (distanceMeter <= 0){
System.out.println("Unable to atain distance less than 0,\n"
+ "please enter a number grater than 0:\n");
distanceMeter = keyboard.nextInt();
}

// showKilometer(distanceMeter);
menu(distanceMeter);
}

public static void menu(double distance){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your choice\n"
+ "1. Convert to kilometers\n"
+ "2. Convert to inches\n"
+ "3. Conveert to feet\n"
+ "4. Quit the program");
int choice = keyboard.nextInt();
while (choice <= 0 || choice >= 5){
System.out.println("Option unavailble, pleace select a choice from 1 - 4:\n"
+ "1. Convert to kilometers\n"
+ "2. Convert to inches\n"
+ "3. Conveert to feet\n"
+ "4. Quit the program");
choice = keyboard.nextInt();
}
if(choice == 1)
showKilometer(distance);
else if(choice == 2)
showInches(distance);
else if(choice == 3)
showFeet(distance);
else
System.out.println("Program terminated.");
}

public static void showKilometer(double num){
System.out.println(num);
}
public static void showInches(double num){
System.out.println(num);
}
public static void showFeet(double num){
System.out.println(num);
}
}

您将看到我添加了其他方法来进行转换,并将距离传递到菜单中。然后我使用该距离传递给可以进行计算的转换方法。

希望对您有帮助。

关于java - 尝试将参数传递给方法并使用 if - else if 语句尝试将用户引导至特定方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43289459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com