我的任务是创建一个简单的计算器,它将进行加,减,乘和除。它没有给我任何错误,但是当我输入value1
和value2
时,它没有给我答案。它只返回我输入的符号,例如+
。
我哪里出错了?
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);
}
}
}
答案 0 :(得分:1)
您只是在代码中打印运算符值。
System.out.println("The Total of your Sum is: "+option);
答案 1 :(得分:0)
两个班级之间没有联系。
在类CalculatorApplication中,你声明了int setvalue1和setvalue2,它们是类本身的instanciate。
您应该实例化calculatoruser,然后设置值并从该类中检索值。
Instanciation和值设置: CalculatorUser cu = new CalculatorUser(setValue1,setValue2,option);
然后 cu.option(); 会打印出你的结果..
在您的情况下,我认为您打算将计算器应用程序用作UI,将calculatorUser用作进行“实际计算”的类。
答案 2 :(得分:0)
将您的CalculatorUser
构造函数更改为:
public CalculatorUser(int value1, int value2, String option,
int results){
this.value1=value1;
this.value2=value2;
this.option=option;
this.results=0;
}
将实例块替换为CalculatorApplication
类
String option=scanner.next();{
CalculatorUser objCalculatorUser = new CalculatorUser(setValue1, setValue2, option.trim(), setValue2);
System.out.println("The Total of your Sum is: "+ objCalculatorUser.option());
}
答案 3 :(得分:0)
1逻辑,例如add,divide,substract等,位于CalculatorUser类下的option方法中。您需要首先在main方法中创建CalculatorUser的实例并调用option方法。
String option=scanner.next();{
CalculatorUser CalculatorUser = new CalculatorUser(setValue1,setValue2,option,0);
CalculatorUser.option();
2您可以将总和消息放在选项方法中,因为您已经在那里打印了总和。
等
public String option(){
if (option.equals("+")){
System.out.println("The Total of your Sum is: "+option);
}
}
答案 4 :(得分:0)
看一下这段代码:
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());
}
}