簡體   English   中英

Java - 在各種方法中使用 static class 變量

[英]Java - using static class variables in various methods

我是編程新手,我需要一些幫助來了解如何在許多方法中使用相同的變量。 提前致謝!

我有一個 class 和一些 static 變量在方法之前聲明,在我有第一種方法計算一些值和第二種方法打印結果之后。 在打印方法中,我想打印初始輸入,但它始終為 0。有人可以幫我理解如何更新全局變量嗎?

我試圖從該方法返回值,但它沒有更新“全局”變量

import java.util.Scanner;

public class Main {
    public static boolean evenNumber = false;
    public static boolean positiveNumber = false;
    public static boolean positiveEvenNumber = false;
    public static int intInputNumber;


    public static void main(String[] args) {
            testEvenPositive();
            printMethod();
        }

        public static int testEvenPositive(){
            System.out.println("Please insert a number, it will be evaluated to even or not and id it's positive or not.");
            Scanner scanner = new Scanner(System.in);
            int intInputNumber = Integer.parseInt(scanner.nextLine());
            if (intInputNumber > 0) {
                positiveNumber = true;}
            if (intInputNumber % 2 == 0) {
                evenNumber = true;}
            if (intInputNumber % 2 == 0 && intInputNumber > 0) {
                positiveEvenNumber = true;}

            return intInputNumber;
        }

        public static void printMethod() {
            System.out.println(Main.intInputNumber + " is an even number: " + evenNumber);
            System.out.println(Main.intInputNumber + " is a positive number: " + positiveNumber);
            System.out.println(Main.intInputNumber + " is a positive even number: " + positiveEvenNumber);
        }
    }

問題是您在 testEvenPositive() 方法中聲明了一個局部變量 (intInputNumber)。 這就是正在改變的。 您也忽略了返回的內容。 您可以改為不聲明局部變量並使方法無效

public static void main(String[] args) {
            testEvenPositive();
            printMethod();
        }

        public static void testEvenPositive(){
            System.out.println("Please insert a number, it will be evaluated to even or not and id it's positive or not.");
            Scanner scanner = new Scanner(System.in);
            intInputNumber = Integer.parseInt(scanner.nextLine());
            if (intInputNumber > 0) {
                positiveNumber = true;}
            if (intInputNumber % 2 == 0) {
                evenNumber = true;}
            if (intInputNumber % 2 == 0 && intInputNumber > 0) {
                positiveEvenNumber = true;}

            return intInputNumber;
        }

或者您可以返回變量,然后在 main 方法中分配它

public static void main(String[] args) {
            intInputNumber = testEvenPositive();
            printMethod();
        }

        public static int testEvenPositive(){
            System.out.println("Please insert a number, it will be evaluated to even or not and id it's positive or not.");
            Scanner scanner = new Scanner(System.in);
            int intInputNumber = Integer.parseInt(scanner.nextLine());
            if (intInputNumber > 0) {
                positiveNumber = true;}
            if (intInputNumber % 2 == 0) {
                evenNumber = true;}
            if (intInputNumber % 2 == 0 && intInputNumber > 0) {
                positiveEvenNumber = true;}

            return intInputNumber;
        }

我也認為在這種情況下你不應該使用全局變量。 您可以在主要方法中定義變量並傳遞參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM