简体   繁体   中英

stacking switch statements with static variables and looping main method

I'm making a Unit converter in java and i'm getting an issue when i'm running the program after it loops. im not sure if its an issue with my static variables, the switch statements, or the loop - i'm new to java and this is my first program. the main issue i'm having is that after running it for a second time, and switching which type of unit to convert it comes up with this :

"Enter Value for Conversion: 33

33 Kilograms = 145.50525 Pounds

Enter Value for Conversion:"

but i want it to do This:

"33 Kilograms = 145.50525 Pounds

do you want to continue and perform another conversion? ('Y' or 'N')"

Heres the code(sry i know its a lot to look through)

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);

        }
    }
}
}   

Add the following line to the top of Type function to fix your current problem .

a=b=c=-1; 

This ensures that only one of the variables will have a valid case value at a time.

Also you are causing an infinite stack issue..

Change Loop to be as follows (or as Quoi does it, a much better option)

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);

If I understand you correctly :), use do-while to get job for n-number of time.

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM