Home > database >  how to run the same result of a if statement in a method in a new method
how to run the same result of a if statement in a method in a new method

Time:10-25

I am new to java and I'm working on a temp conversion tool

the whatToDo method takes a users input and runs it though if and else if statements and will convert one unit to another depending on what the user imputed

then the whatsNext method runs once the conversion method was executed it will ask the user if they want to convert something else convert the same thing or exit the program

I want it so when the whatsNext method is ran the and the user inputs "1" it will run the same conversion method that was just run from the whatToDo method

I have tried to take the WhatToConvert int from the whatToDo method to make a new if statement in the whatsNext method but I can not because its in a method and java cannot find it

Thank you in advanced if you do help :)

    import java.util.Scanner;
public class App {
    public static void conversions() {
        System.out.println("What two temperatures would you like to convert?");
        System.out.println("[1]:celsius to fahrenheit");
        System.out.println("[2]:fahrenheit to celsius");
        System.out.println("[3]:celsius to kelvin");
        System.out.println("[4]:fahrenheit to kelvin");
        System.out.println("[5]:kelvin to celsius");
        System.out.println("[6]:kelvin to fahrenheit");
      }
      public static void options() {
        System.out.println("what would you like to do now?");
        System.out.println("[1]:convert the same thing");
        System.out.println("[2]:convert something else");
        System.out.println("[3]:exit");
      }
      public static void whatToDo() {
        Scanner in = new Scanner(System.in);
        conversions();  
        int whatToConvert = in.nextInt();
        if (whatToConvert == 1) {
          cF();
        } else if (whatToConvert == 2) {
            fC();
        } else if (whatToConvert == 3) {
            cK();
        } else if (whatToConvert == 4) {
            fK();
        } else if (whatToConvert == 5) {
            kC();
        } else if (whatToConvert == 6) {
            kF();
        }
         else {
          System.out.flush();
          System.out.println(whatToConvert   ": is a unknown command pls try again:");
          whatToDo();
        }
      }
//      runs once conversion method was run
      public static void whatNext() {
        var in = new Scanner(System.in);
        options();
        int choice = in.nextInt();
        if (choice == 1) {
            System.out.println("needs to be added");
            cF();
        } else if (choice == 2) {
            whatToDo();
        } else if (choice == 3) {
            System.exit(1);
        }
      }
//      conversion methods 
       public static void cF() {
        Scanner in = new Scanner(System.in);
        System.out.println("please enter the temperature in celsius:");
        double cToF = in.nextDouble();
        System.out.println(cToF   "°C is about "   (cToF * 1.8   32)   "°F");
      }
       public static void fC() {
        Scanner in = new Scanner(System.in);
        System.out.println("please enter the temperature in fahrenheit:");
        double fToC = in.nextDouble();
        System.out.println(fToC   "°F is about "   (fToC - 32) / 1.8   "°C");
      }
       public static void cK() {
           Scanner in = new Scanner(System.in);
           System.out.println("enter the temperature in celsius:");
           double cToK = in.nextDouble();
           System.out.println(cToK   "°C is about "   (cToK   273.15)   "°K");
       }
       public static void fK() {
           Scanner in = new Scanner(System.in);
           System.out.println("enter the temperature in fahrenheit:");
           double fToK = in.nextDouble();
           System.out.println(fToK   "°F is about "   ((fToK - 32)*5/9   273.15)   "°K");
       }
       public static void kC() {
           Scanner in = new Scanner(System.in);
           System.out.println("enter the temperature in kelvin:");
           double kToC = in.nextDouble();
           System.out.println(kToC   "°K is about "   (kToC - 273.15)   "°C");
       }
       public static void kF() {
           Scanner in = new Scanner(System.in);
           System.out.println("enter the temperature in kelvin:");
           double kToF = in.nextDouble();
           System.out.println(kToF   "°K is about "   ((kToF - 273.15)* 9/5   32)   "°C");
       }
       
    public static void main(String[] args) throws Exception {

        while (true) {
            whatToDo();
            whatNext();     
            }
    }
}

CodePudding user response:

I could think of two easy ways you can achieve this:

  1. The variable whatToConvert is declared inside a variable.
    Hence its scope (or simply say its access) is only inside that method - unless you specifically pass it as argument to some other method.
    You can also have variables with global scope - meaning variables declared at class level and not inside a method. Such variables are accessible to all the methods of that class.
    You can read more on "scope of variables in java".

    What you can do here is - declare whatToConvert variable as static at class level instead of inside whatToDo() method.

    public class App {
         static int whatToConvert = 0;
         //all other code
    }
    

    Method whatNext() can then have access to this variable.

  2. You know that whatNext() always runs after whatToDo(). In such case, you can return the value of 'whatToConvert' from whatToDo() and pass that value as method argument to whatNext()

In either case you should extract the if-else code to a new method to add re-usability for method whatNext()

New method can have method signature something like:

        public static void decideOperation(int choice) {
            //If-else blocks or switch case
        }

CodePudding user response:

When you declare a variable in a method, it will be local, so it will be instantiated and in the method and removed once the method is finished. In order for a variable to be read by all methods you should declare it as a global variable, in this way:

public class App {
  private static int whatToConvert;
  ...
  public static void whatToDo() {
    Scanner in = new Scanner(System.in);
    conversions();  
    whatToConvert = in.nextInt();
    ...
  }

Now you can use whatToConvert in other methods

  •  Tags:  
  • java
  • Related