Java程序:返回语句不起作用

时间:2015-10-15 01:06:20

标签: java

该程序为类创建各种方法并运行每个方法。当我运行方法时,return语句在方法中不起作用,尽管输入的整数值肯定是传递给对象的(我用print语句测试)。我确信问题出在我的静态声明的某个地方。 有人可以帮忙吗?

import java.util.Scanner;
public class MyInteger {
    public static int storedValue;
    public int value;
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Class with various methods test. Please enter an integer:");
        int num = input.nextInt();
        MyInteger x = new MyInteger(num);
        storedValue = x.value;
        System.out.println("Now performing returnInt");
        x.returnInt();
        System.out.println("Now performing isEven");
        x.isEven();
    }
    MyInteger(int a){
        value = a;
    }
    public int returnInt(){
        return storedValue;
    }
    public boolean isEven(){
        if(value % 2 == 0){
            return true;
        }
        else{
            return false;
        }
    }
    public boolean isOdd(){
        if(value % 2 != 0){
            return true;
        }
        else{
            return false;       
        }
    }
    public boolean isPrime(){
        if (value == 2){
            return true;
        }
        for (int i = 2; i < value;i++){
            if (value % i == 0){
                return false;
            }
            }
            return true;
    }
    public boolean isEven(int a){
        if (a % 2 == 0){
            return true;
        }
        else{
            return false;
        }
    }
    public boolean isOdd(int a){
        if (a % 2 != 0){
            return true;
        }
        else{
            return false;
        }
    }
    public boolean isPrime(int a){
        if (a == 2){
            return true;
        }
        for (int i = 2; i < a;i++){
            if (a % i == 0){
                return false;
            }
            }
            return true;
    }
    public boolean isEven(MyInteger a){
        if (a.value % 2 == 0){
            return true;
        }
        else{
            return false;
        }
    }
    public boolean isOdd(MyInteger a){
        if (a.value % 2 != 0){
            return true;
        }
        else{
            return false;
        }
    }
    public boolean isPrime(MyInteger a){
        if (a.value == 2){
            return true;
        }
        for (int i = 2; i < a.value;i++){
            if (a.value % i == 0){
                return false;
            }
            }
            return true;
    }
    public boolean equals(int a){
        if (value == a){
            return true;
        }
        else{
            return false;
        }
    }
    public boolean equals(MyInteger a){
        if (value == a.value){
            return true;
        }
        else{
            return false;
        }
    }
    public int parseInt(String s, int radix){
        for (int i = (s.length()-1); i >= 0; i--){
        radix += (int)s.charAt(i)*(Math.pow(10, i));
    }
        return radix;
    }
}

1 个答案:

答案 0 :(得分:1)

首先,删除静态storedvalue

// public static int storedValue;

然后更改您的returnInt以返回值。像,

public int returnInt(){
    return value;
}

最后,在您的来电者return中指定main(ed)值,或直接使用它。像

这样的东西
System.out.println("Now performing returnInt");
int v = x.returnInt();
System.out.printf("Value = %d%n", v);
System.out.println("Now performing isEven");
if (x.isEven()) {
    System.out.println("It's even");
} else {
    System.out.println("It's odd");
}