如何从同一类的主方法(main 方法)中访问非静态方法?

huangapple go评论86阅读模式
英文:

How do I access a non static method from main method in the same class?

问题

I'm trying to make a Math Quiz but I started learning Java recently and I'm having some trouble. I already read about accessing methods and stuff, but I don't see how all of this is supposed to help me there.

import java.util.Scanner;

public class MatQui {
    Scanner scan = new Scanner(System.in);

    int correct = 0;

    public static void main(String[] args) {

        System.out.println("Enter your name:");
        String name = scan.nextLine();

        System.out.println("Hello " + name + "! Answer the questions.");

        MatQui quiz = new MatQui();
        quiz.firstQuiz();
    }

    public void firstQuiz() {

        int randomNum1 = (int)(Math.random() * 101 + 1);
        int randomNum2 = (int)(Math.random() * 101 + 1);

        int RandomAddSolution = scan.nextInt();
        int CorrectAddSolution = randomNum1 + randomNum2;

        System.out.println(randomNum1 + " + " + randomNum2 + " = ?");

        if (RandomAddSolution == CorrectAddSolution) {
            System.out.println("Correct!");
            correct++;
        } else {
            System.out.println("Wrong! The correct answer is: " + CorrectAddSolution);
        }
        secondQuiz();
    }

    public void secondQuiz() {

        int randomNum1 = (int)(Math.random() * 101 + 1);
        int randomNum2 = (int)(Math.random() * 101 + 1);

        int RandomMinusSolution = scan.nextInt();
        int CorrectMinusSolution = randomNum1 - randomNum2;

        System.out.println(randomNum1 + " - " + randomNum2 + " = ?");

        if (RandomMinusSolution == CorrectMinusSolution) {
            System.out.println("Correct!");
            correct++;
        } else {
            System.out.println("Wrong! The correct answer is: " + CorrectMinusSolution);
        }
        thirdQuiz();
    }

    public void thirdQuiz() {

        int randomNum1 = (int)(Math.random() * 11 + 1);
        int randomNum2 = (int)(Math.random() * 11 + 1);

        int RandomMulSolution = scan.nextInt();
        int CorrectMulSolution = randomNum1 * randomNum2;

        System.out.println(randomNum1 + " * " + randomNum2 + " = ?");

        if (RandomMulSolution == CorrectMulSolution) {
            System.out.println("Correct!");
            correct++;
        } else {
            System.out.println("Wrong! The correct answer is: " + CorrectMulSolution);
        }
        fourthQuiz();
    }

    public void fourthQuiz() {

        int randomNum1 = (int)(Math.random() * 101 + 1);
        int randomNum2 = (int)(Math.random() * 11 + 1);

        int RandomDivSolution = scan.nextInt();
        int CorrectDivSolution = randomNum1 / randomNum2;

        System.out.println(randomNum1 + " / " + randomNum2 + " = ?");

        if (RandomDivSolution == CorrectDivSolution) {
            System.out.println("Correct!");
            correct++;
            endingScreen();
        } else {
            System.out.println("Wrong! The correct answer is: " + CorrectDivSolution);
            endingScreen();
        }
    }

    public void endingScreen() {
        int percentageCorrect = correct * 25;
        System.out.println("You answered " + correct + " questions correctly.!\n"
                + "That's " + percentageCorrect + "%!");

        if (correct == 0) {
            System.out.println(":(");
        } else {
            System.out.println(":)");
        }
    }
}
I want to access 'firstQuiz()' method from main, but I get the error, that I can't access non static element form static main. How do I work with it?
英文:

I'm trying to make a Math Quiz but I started learning Java recently and I'm having some trouble. I already read about accessing methods and stuff, but I don't see how all of this is supposed to help me there.

    import java.util.Scanner;
public class MatQui{
Scanner scan = new Scanner(System.in);
int correct = 0;
public static void main(String[] args) {
System.out.println("Enter your name: ");
String name = scan.nextLine();
System.out.println("Hello " + name + "! Answer the questions.");
firstQuiz();
}
public void firstQuiz() {
int randomNum1 = (int)(Math.random() * 101 +1);
int randomNum2 = (int)(Math.random() * 101 +1);
int RandomAddSolution = scan.nextInt();
int CorrectAddSolution = randomNum1 + randomNum2;
System.out.println(randomNum1 + " + " + randomNum2 + " = ?");
if (RandomAddSolution == CorrectAddSolution) {
System.out.println("Correct!");
correct++;
}
else if (RandomAddSolution != CorrectAddSolution) {
System.out.println("Wrong! The correct answer is: " + CorrectAddSolution);
}
secondQuiz();
}
public void secondQuiz() {
int randomNum1 = (int)(Math.random() * 101 +1);
int randomNum2 = (int)(Math.random() * 101 +1);
int RandomMinusSolution = scan.nextInt();
int CorrectMinusSolution = randomNum1 - randomNum2;
System.out.println(randomNum1 + " - " + randomNum2 + " = ?");
if (RandomMinusSolution == CorrectMinusSolution) {
System.out.println("Correct!");
correct++;  //Ly Huong Van
}
else if (RandomMinusSolution != CorrectMinusSolution) {
System.out.println("Wrong! The correct answer is: " + CorrectMinusSolution);
}
thirdQuiz();
}
public void thirdQuiz() {
int randomNum1 = (int)(Math.random() * 11 +1);
int randomNum2 = (int)(Math.random() * 11 +1);
int RandomMulSolution = scan.nextInt();
int CorrectMulSolution = randomNum1 * randomNum2;
System.out.println(randomNum1 + " + " + randomNum2 + " = ?");
if (RandomMulSolution == CorrectMulSolution) {
System.out.println("Correct!");
correct++;
}
else if (RandomMulSolution != CorrectMulSolution) {
System.out.println("Wrong! The correct answer is: " + CorrectMulSolution);
}
fourthQuiz();
}
public void fourthQuiz() {
int randomNum1 = (int)(Math.random() * 101 +1);
int randomNum2 = (int)(Math.random() * 11 +1);
int RandomDivSolution = scan.nextInt();
int CorrectDivSolution = randomNum1 / randomNum2;
System.out.println(randomNum1 + " / " + randomNum2 + " = ?");
if (RandomDivSolution == CorrectDivSolution) {
System.out.println("Correct!");
correct++;
endingScreen();
}
else if (RandomDivSolution != CorrectDivSolution) {
System.out.println("Wrong! The correct answer is: " + CorrectDivSolution);
endingScreen();
}
}
public void endingScreen() {
int percentageCorrect = correct * 25;
System.out.println("You answered " + correct + " questions correctly.!\n"
+ "That's "  + percentageCorrect + "%!");
if (correct == 0) {
System.out.println(":(");
}
else if (correct != 0) {
System.out.println(":)");
}
}

I want to access 'firstQuiz()' method from main, but I get the error, that I can't access non static element form static main. How do I work with it?

答案1

得分: 2

非静态方法可以通过创建所在类的对象来访问。

在这个示例中,您将需要在主方法中创建该类的对象,并使用此对象调用方法firstQuiz()。

英文:

Non Static methods can be accessed by creating objects of the class in which the method is present.

In this example, you will have to create the object of the class in the main method and call the method firstQuiz() using this object.

答案2

得分: 2

按照 tieburach 的回答...... 你无法从静态方法访问非静态方法。你不能改变 main 方法的签名,这意味着你不能使 main 非静态。Main 方法将始终是静态的。

解决方法是将你的方法设为静态,但如果这不是你想要的,你还可以创建另一个类,在 main 方法中创建该类的实例,然后调用该类的方法。

英文:

As tieburach answered... you can not access non static method from static one. You can not change main method signature, which means you can non make main non static. Main will always be static.

Solution would be to make your method static, but if it is not what you want, you can also create another class and instance of it in main method, and then call method on that class.

答案3

得分: 1

你可能已经注意到,错误提示是说你不能从静态方法访问非静态方法。对你来说,最简单的解决办法是将你正在调用的方法也改为静态方法。

英文:

As you probably noticed, the error says that you can't access non static method from static one. The easiest solution for you is to make the methods you are invoking static as well.

答案4

得分: 1

你可以将该方法设为静态方法,或者创建该类的一个新实例,并像这样访问它:

Test test = new Test();
test.yourMethod();
英文:

You can make the method static or you create a new instance of the class and access to it like that:

Test test = new Test();
test.yourMethod();

答案5

得分: 1

你需要创建一个新的 MatQui matqui = new MatQui();,然后使用新创建的对象调用方法 matqui.firstQuiz();

英文:

You need create a new MatQui matqui = new MatQui(); and after call the method with new object created matqui.firstQuiz();

huangapple
  • 本文由 发表于 2020年9月16日 15:41:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63915318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定