繁体   English   中英

如何在其他方法中引用main方法中创建的多个object的非静态字段?

[英]How do I refer to non-static fields of more than one object created in the main method in other methods?

我决定让我的计算器代码不那么混乱,并将以前在main方法中的大部分代码存储在其他方法中。 它看起来好多了,但问题是:代码无法运行。 每当我在这些新方法中引用属于在main方法中创建的 object 的实例字段时,我的方法都无法理解它是什么。 在我创建 class 对象后,每个方法都在方法中被调用,所以我想,到那时我的方法会知道它们。 不幸的是,他们没有。 我希望在主要方法之后移动我的所有方法会有所帮助,但事实并非如此。

如何在其他方法中引用在 main 方法中创建的对象的非静态字段? 这是我的新代码:

import java.util.Scanner;
import java.text.DecimalFormat;

public class Num {
    private String stringValue;
    private char type;
    private double doubleValue;
    private static String operationS;
    private static boolean dontKnow;
    private static double result;
    private static final DecimalFormat df = new DecimalFormat("#.##");

    public Num() {

    }

    public static void main(String[] args) {
        Num firstNum = new Num();
        Num secondNum = new Num();

        Scanner scanner = new Scanner(System.in);
        firstNum.stringValue = scanner.next();
        operationS = scanner.next();
        secondNum.stringValue = scanner.next();

        firstNum.checkType();
        secondNum.checkType();

        if((firstNum.type || secondNum.type) == 'd') {
            decimalRejection();
        }

        if((firstNum.type || secondNum.type) == 'r') {
            romanHandling();
        }

        if((firstNum.type || secondNum.type) == 'i') {
            integerHandling();
        }

        calculatingResult();
    }

    public void checkType() {
        if (stringValue.compareTo(operationS) > 15) {
            type = 'r';
        } else {
            if ((int) (double) Double.valueOf(stringValue) == (double) Double.valueOf(stringValue)) {
                type = 'i';
            } else {
                type = 'd';
                dontKnow = true;
            }
        }
    }

    public static void decimalRejection() {
        if(firstNum.type == 'd' && secondNum.type != 'd') {
            System.out.print("It looks like your first number is decimal! ");
        } else if(firstNum.type != 'd') {
            System.out.print("It looks like your second number is decimal! ");
        } else {
            System.out.print("It looks like both of your numbers are decimal! ");
        }
        System.out.println("Can't work with decimals, sorry!");
    }

    public static void romanHandling() {
        firstNum.romanToArab();
        secondNum.romanToArab();
        if((firstNum.doubleValue || secondNum.doubleValue) == 0) {
            if(firstNum.doubleValue == 0 && secondNum.doubleValue != 0) {
                System.out.println("I don't know the first number! It's supposed to be a Roman, isn't it?");
            } else if(firstNum.doubleValue != 0) {
                System.out.println("I don't know the second number! It's supposed to be a Roman, isn't it?");
            } else {
                System.out.println("I know neither of the numbers! They are supposed to be Romans, aren't they?");
            }
        }
    }

    public void romanToArab() {
        switch(stringValue) {
            case "I":
                doubleValue = 1;
                break;
            case "II":
                doubleValue = 2;
                break;
            case "III":
                doubleValue = 3;
                break;
            case "IV":
                doubleValue = 4;
                break;
            case "V":
                doubleValue = 5;
                break;
            case "VI":
                doubleValue = 6;
                break;
            case "VII":
                doubleValue = 7;
                break;
            case "VIII":
                doubleValue = 8;
                break;
            case "IX":
                doubleValue = 9;
                break;
            case "X":
                doubleValue = 10;
                break;
            default:
                dontKnow = true;
        }
    }

    public static void integerHandling() {
        firstNum.doubleValue = Integer.decode(firstNum.stringValue);
        firstNum.doubleValue = Integer.decode(firstNum.stringValue);
        if((firstNum.doubleValue || secondNum.doubleValue) > 10) {
            if(firstNum.doubleValue > 10 && secondNum.doubleValue != 10) {
                System.out.println("It appears the first number is too big for me! I can only handle numbers of up to ten, remember!");
            } else if(firstNum.doubleValue != 10) {
                System.out.println("It appears the second number is too big for me! I can only handle numbers of up to ten, remember!");
            } else {
                System.out.println("It appears both numbers are too big for me! I can only handle numbers of up to ten, remember!");
            }
            dontKnow = true;
        }
    }

    public static void calculatingResult() {
        if(operationS.equals("+")) {
            result = firstNum.doubleValue + secondNum.doubleValue; }
        else if(operationS.equals("-")) {
            result = firstNum - secondNum; }
        else if(operationS.equals("*")) {
            result = firstNum * secondNum; }
        else if(operationS.equals("/")) {
            result = firstNum / secondNum; }
        else {
            System.out.println("I don't know such an operation!");
            dontKnow = true; }
    }

    public static void calculatingResult() {
        if (!(operationS.equals("/") && secondNum.doubleValue == 0)) {
            if (!dontKnow) {
                if (result / (int) result != 1) {
                    if (String.valueOf(result).equals(df.format(result))) {
                        System.out.println("It's " + result + "!");
                    } else {
                        System.out.println("It's approximately " + df.format(result) + "!");
                    }
                } else {
                    System.out.println("It's " + (int) result + "!");
                }
            }
        } else {
            if (!dontKnow) {
                System.out.println("Gosh! I tried to divide it by zero, as you requested, but my virtual head nearly exploded! I need to recover...");
            } else {
                System.out.println("Besides, you can't even divide by zero, I'm so told!");
            }
        }
    }
}

暂无
暂无

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

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