首页 > 解决方案 > 如何在新方法中的方法中运行 if 语句的相同结果

问题描述

我是java新手,我正在开发一个临时转换工具

whatToDo 方法接受用户输入并通过 if 和 else if 语句运行它,并将根据用户估算的内容将一个单位转换为另一个单位

然后 whatsNext 方法在执行转换方法后运行,它会询问用户是否要转换其他东西 转换相同的东西或退出程序

我想要它,所以当运行 whatsNext 方法并且用户输入“1”时,它将运行刚刚从 whatToDo 方法运行的相同转换方法

我试图从 whatToDo 方法中获取 WhatToConvert int 以在 whatsNext 方法中创建一个新的 if 语句,但我不能,因为它在一个方法中并且 java 找不到它

如果您有帮助,请提前感谢您:)

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

标签: java

解决方案


我可以想到两种简单的方法来实现这一点:

  1. 变量在变量whatToConvert内部声明。
    因此,它的范围(或简单地说它的访问)仅在该方法内部 - 除非您专门将它作为参数传递给其他方法。
    您还可以拥有具有全局范围的变量——这意味着在类级别而不是在方法内声明的变量。该类的所有方法都可以访问此类变量。
    您可以阅读有关“java 中的变量范围”的更多信息。

    您可以在这里做的是 -whatToConvert在类级别而不是内部whatToDo()方法将变量声明为静态。

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

    然后方法whatNext()可以访问此变量。

  2. 你知道那whatNext()总是在后面跑whatToDo()。在这种情况下,您可以返回“whatToConvert”的值whatToDo()并将该值作为方法参数传递给whatNext()

无论哪种情况,您都应该将 if-else 代码提取到新方法中,以增加方法的可重用性whatNext()

新方法可以具有类似的方法签名:

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

推荐阅读