简体   繁体   中英

Program just prints sign rather than sum

My assignment is to create a simple calculator which will add, subtract, multiply and divide. It is not giving me any errors, however it is not giving me the answer when I enter value1 and value2 . It just returns the sign I input eg + .

Where did I go wrong?

package CalculatorApp;

import java.util.Scanner;

public class CalculatorUser {

    private int value1;
    private int value2;
    private String option;
    private int results;

    public CalculatorUser(int value1, int value2, String option,
            int results){

        this.value1=0;
        this.value2=0;
        this.option=option;
        this.results=0;

    }

    public int getValue1(){
        return value1;
    }

    public void setValue1(int value1){
        this.value1 = value1;
    }

    public int getValue2(){
        return value2;
    }

    public void setValue2(int value2){
        this.value2 = value2;
    }

    public String option(){

        if (option.equals("+")){
            System.out.println(value1+value2);
        }

        else if(option.equals("-")){
            System.out.println(value1-value2);
        }
        else if(option.equals("*")){
            System.out.println(value1*value2);
        }
        else if(option.equals("/")){
            System.out.println(value1/value2);
        }
        else if(option.equals("unknown")){
            System.out.println("Please type + for Add, " +
                    "- for Subtract, * for Multiply, / for" +
                    "divid and press Enter/Return Key");
        }
        return option;
        }


    public int getResults(){
        return results;
    }

    public void setResults(int results){
        this.results = results;
    }

    public void updateAddFromInput(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("-----CALCULATOR-----");
        int newResults = scanner.nextInt();
        setResults(newResults);
    }
}

 package CalculatorApp;

 import java.util.Scanner;

 public class CalculatorApplication {

    public static void main(String[] args){

        System.out.println("Enter First Value: ");
        Scanner scanner = new Scanner(System.in);

        int setValue1 = scanner.nextInt();

        System.out.println("Enter Second Value: ");  

        int setValue2 = scanner.nextInt();

        System.out.println("Would you like to add, subtract, " +
                "multiply or divide:");

        String option=scanner.next();{

            System.out.println("The Total of your Sum is: "+option);
        }   
    }
}

您只是在代码中打印运算符值。

System.out.println("The Total of your Sum is: "+option);

There is no link between the two classes.

In class CalculatorApplication you have declared int setvalue1 and setvalue2 which are instanciate in the class itself.

You should instanciate the calculatoruser and them set the value and the retrieve the values from that class.

Instanciation and value settings : CalculatorUser cu = new CalculatorUser(setValue1,setValue2,option);

Then cu.option(); Would print out your result..

In your case, I suppose you intend to use calculator application as UI and calculatorUser as the class where the "real calculation" takes place.

Change your CalculatorUser constructor to:

 public CalculatorUser(int value1, int value2, String option,
        int results){

    this.value1=value1;
    this.value2=value2;
    this.option=option;
    this.results=0;

}

Replace instance block with below code in CalculatorApplication class

    String option=scanner.next();{
        CalculatorUser objCalculatorUser = new CalculatorUser(setValue1, setValue2, option.trim(), setValue2);
        System.out.println("The Total of your Sum is: "+ objCalculatorUser.option());
    }

1 The logics, such as add, divide, substract etc, are located in option method under CalculatorUser class. You need first create instance of CalculatorUser in main method and call option method.

String option=scanner.next();{
CalculatorUser CalculatorUser = new CalculatorUser(setValue1,setValue2,option,0);
CalculatorUser.option();

2 You can put the sum message in option method as you already print the sum there.

like,

public String option(){

    if (option.equals("+")){
        System.out.println("The Total of your Sum is: "+option);
    }
}

Program just prints sign rather than sum

Have a look at this code:

import java.util.Scanner;

public class Calculator{
private int value1;
private int value2;
private String option;
private int results;

public Calculator(){
    this.value1=0;
    this.value2=0;
    this.option=null;
    this.results=0;
}

public int getValue1(){
    return value1;
}

public int getValue2(){
    return value2;
}

public void setValue1(int value1){
    this.value1 = value1;
} 

public void setValue2(int value2){
    this.value2 = value2;
}

public void setOption(String option){
    this.option = option;
}

public int getResult(){
    if (option.equals("+")){
        results = value1+value2;
    }else if(option.equals("-")){
        results = value1-value2;
    }else if(option.equals("*")){
        results = value1*value2;
    }else if(option.equals("/")){
        results = value1/value2;
    }else if(option.equals("unknown")){
        System.out.println("Please type + for Add, - for Subtract, * for Multiply, / for divide and press Enter/Return Key");
        return 0;
    }
    return results;
}

public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);        
    System.out.println("Enter First Value: ");
    int value1 = scanner.nextInt();
    System.out.println("Enter Second Value: ");  
    int value2 = scanner.nextInt();
    System.out.println("Would you like to add, subtract, multiply or divide:");
    String option = scanner.next();
    scanner.close();
    Calculator c = new Calculator();
    c.setValue1(value1);
    c.setValue2(value2);
    c.setOption(option);
    System.out.println(c.getResult());        
}

}

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