简体   繁体   English

尝试在Java中将变量从方法调用到main

[英]Trying to call a variable from a method to the main in Java

I need your help with a simple problem I couldn't figure it out, I'm trying to call the variable "randDiv" from the method GenerateQuiz() in the main method but I could not figure it out.I know its a simple problem I just couldn't figure it out so please don't make it hard.我需要您帮助解决一个我无法解决的简单问题,我正在尝试从主方法中的 GenerateQuiz() 方法中调用变量“randDiv”,但我无法解决。我知道它很简单问题我只是想不通所以请不要让它变得困难。

Note: I tried to use static field and it didn't work注意:我尝试使用静态字段,但没有用

package com.company;
import java.security.SecureRandom;
import java.util.Scanner;

public class Quiz {

    static void GenerateQuiz()
    {
        SecureRandom random = new SecureRandom();
        int randX = random.nextInt(10);
        int randY = random.nextInt(10);
        int randDiv = randX * randY;
        char mult = '*';
        System.out.printf("What's: %d %c %d?",randX ,mult, randY);
    }

    public static void main(String[] args) {
        // write your code here
        Scanner input = new Scanner(System.in);
        System.out.println("Please put your name: ");
        String name = input.nextLine();
        System.out.printf("Now %s we'll show you couple of multiplications for you to solve",
                name);
        System.out.println("If you wish to exit use <Ctl z>");
        GenerateQuiz();
        System.out.println();
        int ans = input.nextInt();
        
        if (ans == randDiv) {
            System.out.println("Good job! now onto the next one");
            GenerateQuiz();
        }
        else {
            System.out.println("Oops wrong answer! Try again");
        }
    }
}

These are all local variables, so no you can't.这些都是局部变量,所以不,你不能。 You have to move the variable to the class level.您必须将变量移动到类级别。

public class Quiz {

    static int randDiv;                 // CHANGE

    static void GenerateQuiz()
    {
        SecureRandom random = new SecureRandom();
        int randX = random.nextInt(10);
        int randY = random.nextInt(10);
        randDiv = randX * randY;        // CHANGE
        char mult = '*';
        System.out.printf("What's: %d %c %d?",randX ,mult, randY);
    }

Don't forget to remove the int from the line where you have it now, or you'll still have a local variable, which will cause problems.不要忘记从您现在拥有它的行中删除int ,否则您仍然会有一个局部变量,这会导致问题。

method variables could not be accessed from other methods, So, you must define your variable in the parent class globally.无法从其他方法访问方法变量,因此,您必须在全局父类中定义您的变量。

import java.security.SecureRandom;
import java.util.Scanner;

public class Quiz {
    static int randDiv;
    static void GenerateQuiz()
    {
        SecureRandom random = new SecureRandom();
        int randX = random.nextInt(10);
        int randY = random.nextInt(10);
        randDiv = randX * randY;
        char mult = '*';
        System.out.printf("What's: %d %c %d?",randX ,mult, randY);
    }

    public static void main(String[] args) {
        // write your code here
        Scanner input = new Scanner(System.in);
        System.out.println("Please put your name: ");
        String name = input.nextLine();
        System.out.printf("Now %s we'll show you couple of multiplications for you to solve",
                name);
        System.out.println("If you wish to exit use <Ctl z>");
        GenerateQuiz();
        System.out.println();
        int ans = input.nextInt();

        if (ans == randDiv) {
            System.out.println("Good job! now onto the next one");
            GenerateQuiz();
        }
        else {
            System.out.println("Oops wrong answer! Try again");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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