使用java写一个简单计算器实现加减乘除计算
代码运行和出现错误如下:请输入数据:
1+1
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at method.calc.main(calc.java:10)
代码如下:
package method;
import java.util.Scanner;
public class calc {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入数据:");
double i=scanner.nextDouble();
double j=scanner.nextDouble();
char ch=scanner.next().charAt(0);
switch (ch){
case '+':
add(i,j);
break;
case '-':
acc(i,j);
break;
case '*':
abb(i,j);
case '/':
if (i/j==0){
System.out.println("余数不能为零");
}else {
aaa(i,j);
}
scanner.close();
}
}
private static void add(double i, double j) {
double sum= i+j;
System.out.println("结果为:"+sum);}
privatestatic void acc(double i, double j) {
double sum = i - j;
System.out.println("结果为:"+sum);
}
privatestatic void abb(double i, double j){
double sum= i*j;
System.out.println("结果为:"+sum);}
privatestatic void aaa(double i, double j){
double sum=i/j;
System.out.println("结果为: "+sum);
}
}
按照这个顺序,输入的时候空格分开就行
double i = scanner.nextDouble();
char ch = scanner.next().charAt(0);
double j = scanner.nextDouble(); hhh hhh 感谢分享! 楼主加油! 希望能帮到你吧!
package com.example.test;
import java.util.Scanner;
public class Calc {
public static void main(String[] args) {
boolean str = true;
Scanner scanner = new Scanner(System.in);
while (str) {
System.out.println("请选择运算方式,按0退出:1.加法 2.减法 3.乘法 4.除法");
String ch = scanner.next();
if ("0".equals(ch)){
System.out.println("退出成功,谢谢使用!");
break;
}
System.out.println("请输入数据:");
double i = scanner.nextDouble();
double j = scanner.nextDouble();
switch (ch) {
case "1":
add(i, j);
break;
case "2":
acc(i, j);
break;
case "3":
abb(i, j);
break;
case "4":
if (j == 0)
System.out.println("余数不能为零,请重新输入");
else
aaa(i, j);
break;
}
}
scanner.close();
}
private static void add(double i, double j) {
System.out.println("结果为:" + (i + j));
}
private static void acc(double i, double j) {
System.out.println("结果为:" + (i - j));
}
private static void abb(double i, double j) {
System.out.println("结果为:" + (i * j));
}
private static void aaa(double i, double j) {
System.out.println("结果为: " + (i / j));
}
}
连帅帅 发表于 2021-2-4 17:57
希望能帮到你吧!
{:5_108:} 谢谢分享!!!!!!!!! 输入的问题?{:5_94:}
页:
[1]