用java怎样编写加减乘除,从键盘输入,例如:1+2按回车之后可以运算,小数整数都可以
答案:3 悬赏:30 手机版
解决时间 2021-01-26 19:11
- 提问者网友:聂風
- 2021-01-25 20:00
用java怎样编写加减乘除,从键盘输入,例如:1+2按回车之后可以运算,小数整数都可以
最佳答案
- 五星知识达人网友:duile
- 2021-01-25 20:22
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入第一个数:");
double n1 = sc.nextDouble();
System.out.println("请输入运算符:");
String s = sc.next();
System.out.println("请输入第二个数:");
double n2 = sc.nextDouble();
if(s.equals("+")){
System.out.print(n1+"+"+n2+" = "+(n1+n2));
}else if(s.equals("-")){
System.out.print(n1+"+"+n2+" = "+(n1-n2));
}else if(s.equals("*")){
System.out.print(n1+"*"+n2+" = "+(n1*n2));
}else if(s.equals("/")){
if(n2==0){
System.out.println("除数不能等于0");
}else{
System.out.print(n1+"/"+n2+" = "+(n1/n2));
}
}else{
System.out.println("运算符输入错误,请重新输入。");
}
}
Scanner sc=new Scanner(System.in);
System.out.println("请输入第一个数:");
double n1 = sc.nextDouble();
System.out.println("请输入运算符:");
String s = sc.next();
System.out.println("请输入第二个数:");
double n2 = sc.nextDouble();
if(s.equals("+")){
System.out.print(n1+"+"+n2+" = "+(n1+n2));
}else if(s.equals("-")){
System.out.print(n1+"+"+n2+" = "+(n1-n2));
}else if(s.equals("*")){
System.out.print(n1+"*"+n2+" = "+(n1*n2));
}else if(s.equals("/")){
if(n2==0){
System.out.println("除数不能等于0");
}else{
System.out.print(n1+"/"+n2+" = "+(n1/n2));
}
}else{
System.out.println("运算符输入错误,请重新输入。");
}
}
全部回答
- 1楼网友:持酒劝斜阳
- 2021-01-25 21:01
package image;
import java.util.Scanner;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class JS
{
public static void main ( String[] args )
{
System.out.println ("随便输入一个式子:(直接回车结束)");
Scanner scanner = new Scanner (System.in);
ScriptEngineManager sem = new ScriptEngineManager ();
ScriptEngine se = sem.getEngineByName ("js");
while (scanner.hasNextLine ())
{
String line = scanner.nextLine ().trim ();
if ("".equals (line))
{
break;
}
try
{
System.out.println (se.eval (line));
}
catch (ScriptException e)
{
System.out.println ("不合法的式子,重新输入:");
continue;
}
}
scanner.close ();
}
}
- 2楼网友:春色三分
- 2021-01-25 20:43
package know;
import java.util.scanner;
public class t22 {
public static void main(string[] args) {
scanner s=new scanner(system.in);
system.out.println("输入数字:");
string str=s.next();
system.out.println("整数部分:"+str.split("\\.")[0]);
system.out.println("小数部分:"+str.split("\\.")[1]);
s.close();
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯