java做计算器_如何用JAVA做一个简单的计算器?

该博客介绍了一个Java程序,它包含三个方法:从用户获取整数、获取单个字符输入以及根据操作符执行数学运算。程序通过Scanner类获取用户输入,并能处理加减乘除和指数运算。在`solveProblem`方法中,使用if-else语句判断并执行相应的数学操作。这是一个简单的命令行计算器应用示例。
摘要由CSDN通过智能技术生成

package calculator;

import java.util.Scanner;

/**

*

* @author Kevin

*/

public class Calculator {

/**

* This method prints a message, a prompt passed as a parameter,

* to the screen and then scans input until an int is entered. The user

* may type other input without error but the method would not end until

* an int is entered in. It then returns the int that was entered

* @param prompt a message describing the input that is desired

* @return an int, entered by the user on the keyboard

*/

public static int getIntFromUser(String prompt){

Scanner sc = new Scanner(http://System.in);

System.out.println(prompt);

while(!sc.hasNextInt()){

sc.next();

}

return sc.nextInt();

}

/**

* This method prints a message, a prompt passed as a parameter,

* to the screen and then returns the first character entered as input.

* The user may type more than a single character but only the first

* character entered will be returned.

* @param prompt a message describing the input that is desired

* @return a char, taken from the first position of text entered by the

* user on the keyboard

*/

public static char getCharFromUser(String prompt){

Scanner sc = new Scanner(http://System.in);

System.out.println(prompt);

String input = sc.nextLine();

return input.charAt(0);

}

/**

* This method should determine the proper mathematical operation needed

* based on the parameter op, which is a char type and should contain

* a symbol such as '+' or '*' to indicate the mathematical operation

* desired. The parameters x and y will be used to complete the mathematical

* operation and the result will be returned.

* @param x leading operand, this value will be before the operator

* @param y trailing operand, this value will be after the operator

* @param op this char will store a symbol representing the operation that

* should take place

* @return the answer to the mathematical equation formed by x, y, and op.

*/

public static int solveProblem(int x, int y, char op){

/*fill in code*/

return 0;

}

/*Add all future methods in this space*/

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

int x = getIntFromUser("Enter an int");

int y = getIntFromUser("Enter an int");

char op = getCharFromUser("Enter an operation symbol");

int ans = solveProblem(x, y, op);

System.out.println( x + " " + op + " " + y + " = " + ans);

}

}

以上是代码以及解释。。。我也是个新手这个是我刚刚做完的。

值得注意的是,我在下面一个方法里面写的是 /*fill in code*/.

在这里我们可以直接用if else 去填充.(我真的萌新,专业术语也不会hhh)

public static int solveProblem(int x, int y, char op) {

if (op == '+') {

return x + y;

} else if (op == '-') {

return x - y;

} else if (op == '*') {

return x * y;

} else if (op == '/') {

return x / y;

} else if (op == '^') {

// return (int) (Math.pow((double) x, (double) y));

return exp(x,y);

}

return 0;

}

public static int exp(int x, int y) {

int ans = 1;

for (int i = 0; i < y; i++) {

ans *= x;

}

return ans;

这是我在刚刚需要填写代码里面填写的,上面有 if else 去限定情况,下面自己做了一个方法以供我直接使用。(还有两种其他方法我怕太乱就没弄上去)

加油加油!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值