我有一个不会与我合作的转换计划。
它将米转换为:
我的问题是它应该继续下去并允许你输入选择1,得到答案并继续输入选择,直到你点击4结束程序。
到目前为止,它在计算出第一个选择之后或在告知无论如何都以米为单位的距离之后结束。
这是我第一次向论坛寻求帮助,我对java非常陌生,所以请保持简单。
import java.util.Scanner;
public class ConversionProgram
{
public static void showKilometers(double meters) //Converts Meters to Kilometers
{
double kilometers = meters * 0.001;
System.out.println(meters + " meters is " + kilometers + " kilometers.");
}
public static void showInches(double meters) //Converts Meters to Inches
{
double inches = meters * 39.37;
System.out.println(meters + " meters is " + inches + " inches.");
}
public static void showFeet(double meters) //Converts Meters to Feet
{
double feet = meters * 3.281;
System.out.println(meters + " meters is " + feet + " feet.");
}
public static void quitProgram() {
System.out.println("Bye!");
}
public static void showMenu() {
System.out.println(" 1. Convert to Kilometers ");
System.out.println(" 2. Convert to Inches ");
System.out.println(" 3. Convert to Feet ");
System.out.println(" 4. Quit the program ");
System.out.println(" ");
}
public static void main(String[] args) {
double meters;
int choice;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a Distance in Meters: ");
meters = keyboard.nextDouble();
if (meters < 1) {
System.out.println("Meter can not be negative.");
} else showMenu();
choice = keyboard.nextInt();
switch (choice) {
case 1:
showKilometers(meters);
break;
case 2:
showInches(meters);
break;
case 3:
showFeet(meters);
break;
case 4:
quitProgram();
break;
}
}
}
答案 0 :(得分:1)
只要用户决定不退出程序,您就应该使用循环并继续询问新输入。在询问用户输入之前,您可以在程序执行的早期步骤开始此循环。当然应该存在循环中断的情况。考虑到你提供的代码,我认为在你的退出选择下使用一个布尔标志是有意义的。一个例子如下:
import java.util.Scanner;
public class ConversionProgram
{
public static void showKilometers(double meters) //Converts Meters to Kilometers
{
double kilometers = meters * 0.001;
System.out.println(meters + " meters is " + kilometers + " kilometers.");
}
public static void showInches(double meters) //Converts Meters to Inches
{
double inches = meters * 39.37;
System.out.println(meters + " meters is " + inches + " inches.");
}
public static void showFeet(double meters) //Converts Meters to Feet
{
double feet = meters * 3.281;
System.out.println(meters + " meters is " + feet + " feet.");
}
public static void quitProgram() {
System.out.println("Bye!");
}
public static void showMenu() {
System.out.println(" 1. Convert to Kilometers ");
System.out.println(" 2. Convert to Inches ");
System.out.println(" 3. Convert to Feet ");
System.out.println(" 4. Quit the program ");
System.out.println(" ");
}
public static void main(String[] args) {
double meters;
int choice;
Scanner keyboard = new Scanner(System.in);
// Loop breaker
boolean quitFlag = false;
// Whole program wrapped around a loop
while (!quitFlag) {
System.out.println("Enter a Distance in Meters: ");
meters = keyboard.nextDouble();
if (meters < 1) {
System.out.println("Meter can not be negative.");
} else showMenu();
choice = keyboard.nextInt();
switch (choice) {
case 1:
showKilometers(meters);
break;
case 2:
showInches(meters);
break;
case 3:
showFeet(meters);
break;
case 4:
quitFlag = true;
quitProgram();
break;
}
}
}
}
答案 1 :(得分:1)
你需要做的只是保持你的转换逻辑循环直到你输入4.当你输入小于1的值然后暂停你的程序退出程序或调用方法再次输入并重复时,还有一个建议所有步骤。为此制作方法。现在我只是在if块
中复制你的代码import java.util.Scanner;
public class Test {
public static void showKilometers(double meters) // Converts Meters to // Kilometers
{
double kilometers = meters * 0.001;
System.out.println(meters + " meters is " + kilometers + " kilometers.");
}
public static void showInches(double meters) // Converts Meters to Inches
{
double inches = meters * 39.37;
System.out.println(meters + " meters is " + inches + " inches.");
}
public static void showFeet(double meters) // Converts Meters to Feet
{
double feet = meters * 3.281;
System.out.println(meters + " meters is " + feet + " feet.");
}
public static void quitProgram() {
System.out.println("Bye!");
}
public static void showMenu() {
System.out.println(" 1. Convert to Kilometers ");
System.out.println(" 2. Convert to Inches ");
System.out.println(" 3. Convert to Feet ");
System.out.println(" 4. Quit the program ");
System.out.println(" ");
}
public static void main(String[] args) {
int choice;
double meters;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a Distance in Meters: ");
meters = keyboard.nextDouble();
do {
if (meters < 1) {
System.out
.println("Meter can not be negative and keep it greater then 0");
System.out.println("Enter a Distance in Meters: ");
meters = keyboard.nextDouble();
showMenu();
choice = keyboard.nextInt();
switch (choice) {
case 1:
showKilometers(meters);
break;
case 2:
showInches(meters);
break;
case 3:
showFeet(meters);
break;
case 4:
quitProgram();
break;
}
} else
showMenu();
choice = keyboard.nextInt();
switch (choice) {
case 1:
showKilometers(meters);
break;
case 2:
showInches(meters);
break;
case 3:
showFeet(meters);
break;
case 4:
quitProgram();
break;
}
} while (choice != 4);
}}
答案 2 :(得分:0)
你应该把你的选择放在 while循环里面,这将无限期地运行,在quitProgram()
你应该写 System.exit(0)来退出你的程序