我在java中制作一个单位转换器,当我在循环后运行程序时,我遇到了问题。我不确定它是否是我的静态变量,switch语句或循环的问题 - 我是java的新手,这是我的第一个程序。我遇到的主要问题是,在第二次运行它之后,切换哪种类型的单位进行转换就会出现这个:
“输入转化价值: 33
33千克= 145.50525磅
输入转换价值:“
但我想要它这个:
“33千克= 145.50525磅
你想继续并进行另一次转换吗? ('Y'或'N')“
下面是代码(我知道很多东西要透视)
import java.util.Scanner;
public class Unitconverter {
public static class displayer{
static int x,a,b,c,z1,z2,z3;
static Scanner mType = new Scanner(System.in);
static Scanner mDirect = new Scanner(System.in);
static Scanner mValue = new Scanner(System.in);
static Scanner YN = new Scanner(System.in);
static String YorN = "y";
public static void main(String args[]) {
mainMenu();
Type();
Direction();
Loop();
}
public static void mainMenu(){
System.out.println ("Unit Converter!");
System.out.println("");
System.out.println("What type of measurement would you like to convert?");
System.out.println("1. Convert length");
System.out.println("2. Convert weight");
System.out.println("3. Convert volume");
x = mType.nextInt();
}
public static void Type(){
switch(x){
case 1:
System.out.println ("1. Feet to Meters");
System.out.println ("2. Meters to Feet");
System.out.println ("Select conversion direction:");
a =mDirect.nextInt();
break;
case 2:
System.out.println ("1. Pounds to Kilograms");
System.out.println ("2. Kilograms to Pounds");
System.out.println ("Select conversion direction:");
b =mDirect.nextInt();
break;
case 3:
System.out.println ("1. Gallons to liters");
System.out.println ("2. Liters to gallons");
System.out.println ("Select conversion direction:");
c =mDirect.nextInt();
break;
}
}
public static void Direction(){
switch(a){
case 1: System.out.println("Enter Value for Conversion:");
z1 =mValue.nextInt();
System.out.println(z1 +" "+ "Feet =" +" "+ z1*0.3048 +" "+ "Meters");
break;
case 2:System.out.println("Enter Value for Conversion:");
z1 =mValue.nextInt();
System.out.println(z1 +" "+ "Meters =" +" "+ z1*3.28084 +" "+ "Feet");
break;
}
switch(b){
case 1: System.out.println("Enter Value for Conversion:");
z2 =mValue.nextInt();
System.out.println(z2 +" "+ "Pounds =" +" "+ z2*0.453592 +" "+ "Kilograms");
break;
case 2:System.out.println("Enter Value for Conversion:");
z2 =mValue.nextInt();
System.out.println(z2 +" "+ "Kilograms =" +" "+ z2*4.40925 +" "+ "Pounds");
break;
}
switch(c){
case 1: System.out.println("Enter Value for Conversion:");
z3 =mValue.nextInt();
System.out.println(z3 +" "+ "Gallons =" +" "+ z3*3.78541 +" "+ "Liters");
break;
case 2:System.out.println("Enter Value for Conversion:");
z3 =mValue.nextInt();
System.out.println(z3 +" "+ "Liters =" +" "+ z3*0.264172 +" "+ "Gallons");
break;
}
}
public static void Loop() {
while (YorN.equalsIgnoreCase("y")){
System.out.println("");
System.out.println ("do you want to continue and perform another conversion? ('Y' or 'N')");
YorN = YN.nextLine();
main(null);
}
}
}
}
答案 0 :(得分:0)
将以下行添加到Type
功能的顶部以修复当前问题。
a=b=c=-1;
这确保了一次只有一个变量具有有效的case
值。
此外,您还会导致无限堆栈问题。
将循环更改为如下(或Quoi执行此操作,这是一个更好的选择)
System.out.println("");
System.out.println ("do you want to continue and perform another conversion? ('Y' or 'N')");
YorN = YN.nextLine();
if(YorN.equalsIgnoreCase("y"))
main(null);
答案 1 :(得分:0)
如果我理解正确:),请使用do-while
获得n个工作时间。
public static void main(String args[]) {
do{
mainMenu();
Type();
Direction();
System.out.println ("do you want to continue and perform another conversion? ('Y' or 'N')");
String input = YN.nextLine();
}while(YorN.equalsIgnoreCase(input));
}