如何将字符串从其他方法调用到主方法中?

xkftehaa  于 2021-09-13  发布在  Java
关注(0)|答案(2)|浏览(234)

你好,我是java新手,需要有人回答我遇到的一个问题。我最近开始了一个用java制作计算器的项目。然而,我有一个问题,我的一个愚蠢的代码。基本上我不能从方法中调用字符串。我尝试过其他方法来解决这个问题,但是没有用。代码如下:

package CalculatorCore;

import java.util.Scanner;

public class calculations {

    static void firstNumber() {
        Scanner firstNum = new Scanner(System.in);
        System.out.print("First Number: ");
        String n1 = firstNum.next(); //You can see, i put the string in a method

}

    static void secondNumber() {
        Scanner secondNum = new Scanner(System.in);
        System.out.print("Second Number: ");
        String n2 = secondNum.next(); //Here too

}

    public static void main(String[] args) {

        System.out.println("Please Choose one of the following equasions: +, -, * or /");
        System.out.println("");
        System.out.println("");
        mathEquasions();

    }

    static void mathEquasions() {
        Scanner equasions = new Scanner(System.in);
        System.out.print("Enter input: ");
        String e = equasions.next();

        if (e.equals("+")) {
            System.out.println("");
            System.out.println("Please enter the first number that you want to add");
            firstNumber();
            System.out.println("");
            System.out.println("Now add the second number");
            secondNumber();
            var plusAnswer = (n1 + n2);  /*The problem is situated here, i need to call the 
            strings from another class*/

            System.out.println("");
            System.out.println("");
            System.out.println("Your answer is...");
            firstNumber();.n1

        }

    }

我已经使用了一些方法来压缩用户输入,所以如果没有其他方法,我应该删除这些方法吗?

vlf7wbxs

vlf7wbxs1#

您需要返回在这两种方法中检索到的数字。不要忘记将它们解析为整数,使用 nextInt :

public class calculations {
    static int firstNumber() {
        Scanner firstNum = new Scanner(System.in);
        System.out.print("First Number: ");
        int n1 = firstNum.nextInt();
        return n1;
    }

    static int secondNumber() {
        Scanner secondNum = new Scanner(System.in);
        System.out.print("Second Number: ");
        int n2 = secondNum.nextInt();
        return n2;
    }
}

那么,打电话的时候, firstNumbersecondNumber ,创建新变量以存储其返回值:

public class calculations {
    static void mathEquasions() {
        Scanner equasions = new Scanner(System.in);
        System.out.print("Enter input: ");
        String e = equasions.next();

        if (e.equals("+")) {
            System.out.println("");
            System.out.println("Please enter the first number that you want to add");

            int n1 = firstNumber();

            System.out.println("");
            System.out.println("Now add the second number");

            int n2 = secondNumber();

            var plusAnswer = (n1 + n2);

            System.out.println("");
            System.out.println("");
            System.out.println("Your answer is...");
        }
    }
}
kq0g1dla

kq0g1dla2#

欢迎来到so!当你第一次尝试某样东西的时候,最好的做法是尽可能简单,然后逐渐使用更复杂的技术。
在本例中,所有内容都已在一个类中,因此作为第一步,您可以尝试将所有代码放回 main method .

public static void main(String[] args) {
    //All your code can come here first in the order they are supposed to to be called.
}

第二步,当一切正常时,您可以将复制代码的部分提取到单独的方法中。喜欢这里而不是 firstNumber()secondNumber() 你可以只吃一个,比如:

static int getNumber() {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Number: ");
    int number = scanner.nextInt();
    return number;
}

您可以调用相同的方法来获取这两个数字:

public static void main(String[] args) {

    //...

    System.out.println("Please enter the first number that you want to add");
    int n1 = getNumber(); // Using the same method for both

    System.out.println("");
    System.out.println("Now add the second number");

    int n2 = getNumber(); // Using the same method for both

    //...
}

在实践中学习,跳入其中是最好的学习方式之一。免费提供大量高质量的材料(例如yt),它们可以真正提高你的技能。这也是我开始学习的原因,祝你好运!

相关问题