1

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

        }
    }
}
}   
4

2 回答 2

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);
于 2013-02-07T08:08:29.553 回答
0

如果我理解正确:),请使用do-whilen 次获得工作。

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));
}
于 2013-02-07T08:10:15.970 回答