![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
计算器Java编程
packagecom.zb1;importjava.util.Scanner;publicclassCalculator{Scannerinput=newScanner(...
package com.zb1;
import java.util.Scanner;
public class Calculator {
Scanner input = new Scanner(System.in);
public int open(int num1,int num2,String var){
System.out.print("请输入第一个数字");
num1 = input.nextInt();
System.out.println("输入计算符号:+ - * /");
var = input.next();
System.out.println("输入第二个数:");
num2 = input.nextInt();
if("+".equals(var)){
System.out.println(num1+"+"+num2+"="+(num1+num2));
}else if("-".equals(var)){
System.out.println(num1+"-"+num2+"="+(num1-num2));
}else if("*".equals(var)){
System.out.println(num1+"*"+num2+"="+(num1*num2));
}else if("/".equals(var)){
System.out.println(num1+"/"+num2+"="+(num1/(double)num2));
return 0;
}
}
}
package com.zb1;
import java.util.*;
public class CalculatorTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Calculator cal = new Calculator();
int num1 = input.nextInt();
int num2 = input.nextInt();
String var = input.next();
int calcu = cal.open(num1, num2, var);
}
}
求大神,我哪里错了 展开
import java.util.Scanner;
public class Calculator {
Scanner input = new Scanner(System.in);
public int open(int num1,int num2,String var){
System.out.print("请输入第一个数字");
num1 = input.nextInt();
System.out.println("输入计算符号:+ - * /");
var = input.next();
System.out.println("输入第二个数:");
num2 = input.nextInt();
if("+".equals(var)){
System.out.println(num1+"+"+num2+"="+(num1+num2));
}else if("-".equals(var)){
System.out.println(num1+"-"+num2+"="+(num1-num2));
}else if("*".equals(var)){
System.out.println(num1+"*"+num2+"="+(num1*num2));
}else if("/".equals(var)){
System.out.println(num1+"/"+num2+"="+(num1/(double)num2));
return 0;
}
}
}
package com.zb1;
import java.util.*;
public class CalculatorTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Calculator cal = new Calculator();
int num1 = input.nextInt();
int num2 = input.nextInt();
String var = input.next();
int calcu = cal.open(num1, num2, var);
}
}
求大神,我哪里错了 展开
展开全部
import java.util.Scanner;
class Calculator {
Scanner input = new Scanner(System.in);
public int open(int num1,int num2,String var){
//这里应该写在外面,你传了值就没必要输入了
/*System.out.print("请输入第一个数字");
num1 = input.nextInt();
System.out.println("输入计算符号:+ - * /");
var = input.next();
System.out.println("输入第二个数:");
num2 = input.nextInt();*/
if("+".equals(var)){
System.out.println(num1+"+"+num2+"="+(num1+num2));
}else if("-".equals(var)){
System.out.println(num1+"-"+num2+"="+(num1-num2));
}else if("*".equals(var)){
System.out.println(num1+"*"+num2+"="+(num1*num2));
}else if("/".equals(var)){
System.out.println(num1+"/"+num2+"="+(num1/(double)num2));
}
return 0;
}
}
public class CalculatorTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Calculator cal = new Calculator();
System.out.println("请输入第一个数字");
int num1 = input.nextInt();
System.out.println("输入计算符号:+ - * /");
String var = input.next();
System.out.println("输入第二个数:");
int num2 = input.nextInt();
int calcu = cal.open(num1, num2, var);
}
}
展开全部
public class Calculator {
private int number1;
private int number2;
public Calculator(int number1, int number2) {
this.number1 = number1;
this.number2 = number2;
}
public double add() {
return number1 + number2;
}
public double subtract() {
return number1 - number2;
}
public double multiple() {
return number1 * number2;
}
public double divide() {
if(number2 == 0) {
return 0;
}
return number1 / number2;
}
}
public class TestCalculator {
public static void main(String[] args) {
Calculator calculator = new Calculator(5, 2);
System.out.println(calculator.add());
System.out.println(calculator.subtract());
System.out.println(calculator.multiple());
System.out.println(calculator.divide());
}
}
求采纳为满意回答。
private int number1;
private int number2;
public Calculator(int number1, int number2) {
this.number1 = number1;
this.number2 = number2;
}
public double add() {
return number1 + number2;
}
public double subtract() {
return number1 - number2;
}
public double multiple() {
return number1 * number2;
}
public double divide() {
if(number2 == 0) {
return 0;
}
return number1 / number2;
}
}
public class TestCalculator {
public static void main(String[] args) {
Calculator calculator = new Calculator(5, 2);
System.out.println(calculator.add());
System.out.println(calculator.subtract());
System.out.println(calculator.multiple());
System.out.println(calculator.divide());
}
}
求采纳为满意回答。
追问
要用多个带参方法
public int ope(int ver,int num1,int num2){
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询