gpt4 book ai didi

java - 使用静态变量堆叠 switch 语句并循环 main 方法

转载 作者:行者123 更新时间:2023-12-01 04:52:28 24 4
gpt4 key购买 nike

我正在用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);

}
}
}
}

最佳答案

将以下行添加到 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);

关于java - 使用静态变量堆叠 switch 语句并循环 main 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14746376/

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