计算器

package com.method;

import java.util.Scanner;

public class demo04 {
//计算器
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入数值1");
double num1 = scanner.nextDouble();
System.out.println("请输入符号");
String c = scanner.next();
System.out.println("请输入数值2");
double num2 = scanner.nextDouble();

switch (c){
case "+":
System.out.println(num1 + num2);
break;
case "-":
System.out.println(num1 - num2);
break;
case "*":
System.out.println(num1 * num2);
break;
case "/":
System.out.println(num1 / num2);
break;
default:
System.out.println("您输入有误");
}
}
}
}
法二:

import java.util.Scanner;

public class Zuoye1 {
public static void main(String[] args) {
Scanner s1 = new Scanner(System.in);
while (true) {
System.out.println("请输入第一个数字");
double num1 = s1.nextDouble();
System.out.println("请输入符号");
String num2 = s1.next();
System.out.println("请输入第二个数字");
double num3 = s1.nextDouble();
if (num2.equals("+")){
System.out.println("两个数的和= "+ (num1 + num3));
}else if (num2.equals("-")){
System.out.println("两个数的差= "+ (num1 - num3));
}else if (num2.equals("*")){
System.out.println("两个数的积= "+ (num1 * num3));
}else if (num2.equals("/")){
System.out.println("两个数的商= "+ (num1 / num3));
}
}
}
}



posted @ 2022-05-08 13:59  小松2739  阅读(19)  评论(0编辑  收藏  举报