简体   繁体   中英

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

I decided to make my calculator code a little bit less chaotic and stored much of the code that used to be in the main method in other methods. It looks better, but here's the problem: the code can't be run. Whenever I refer to instance fields belonging to an object created in the main method in those new methods, my methods have trouble understanding what it is. Each method is called in the main method after I create my class objects so, I figured, by that point my methods would know them. Unfortunately, they don't. I hoped moving all of my methods after the main method would help, but it didn't.

How do I refer to non-static fields of objects created in the main method in other methods? Here's my new code:

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!");
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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