JAVA的简单计算器中,输入乘法出错是什么问题,这个是什么意思FindMin.classFindMin.java
publicclassSimpleCalculator{publicstaticvoidmain(String[]args){if(args.length!=3){Sys...
public class SimpleCalculator{
public static void main(String[] args){
if(args.length!=3){
System.out.println("Usage:java SimpleCalculator "+"operator1 operator operator2");
System.out.println("Example:java SimpleCalculator 100 + 200");
System.out.println(args[0]+args[1]+args[2]);
System.exit(-1);
}
int operator1=Integer.parseInt(args[0]);
String operator=args[1];
int operator2=Integer.parseInt(args[2]);
int result=Integer.MIN_VALUE;
if(operator.equals("/")&&operator2==0){
System.out.println("can not divide by 0!");
System.exit(-1);
}
if(operator.equals("+"))
result=operator1+operator2;
else if(operator.equals("-"))
result=operator1-operator2;
else if(operator.equals("*"))
result=operator1*operator2;
else if(operator.equals("/"))
result=operator1/operator2;
else{
System.out.println("Error operator!");
System.exit(-1);
}
System.out.println(args[0]+args[1]+args[2]+"="+result);
}
} 展开
public static void main(String[] args){
if(args.length!=3){
System.out.println("Usage:java SimpleCalculator "+"operator1 operator operator2");
System.out.println("Example:java SimpleCalculator 100 + 200");
System.out.println(args[0]+args[1]+args[2]);
System.exit(-1);
}
int operator1=Integer.parseInt(args[0]);
String operator=args[1];
int operator2=Integer.parseInt(args[2]);
int result=Integer.MIN_VALUE;
if(operator.equals("/")&&operator2==0){
System.out.println("can not divide by 0!");
System.exit(-1);
}
if(operator.equals("+"))
result=operator1+operator2;
else if(operator.equals("-"))
result=operator1-operator2;
else if(operator.equals("*"))
result=operator1*operator2;
else if(operator.equals("/"))
result=operator1/operator2;
else{
System.out.println("Error operator!");
System.exit(-1);
}
System.out.println(args[0]+args[1]+args[2]+"="+result);
}
} 展开
4个回答
展开全部
是输入参数的问题,java控制台参数如果传入 * 会出问题,你可以输入 ‘*’ 然后再在程序里面转一下!
public class SimpleCalculator{
public static void main(String[] args){
if(args.length!=3){
System.out.println("Usage:java SimpleCalculator "+"operator1 operator operator2");
System.out.println("Example:java SimpleCalculator 100 + 200");
System.out.println(args[0]+args[1]+args[2]);
System.exit(-1);
}
int operator1=Integer.parseInt(args[0]);
String operator=args[1];
operator=operator.replaceAll("'", "");
args[1]=operator;
int operator2=Integer.parseInt(args[2]);
int result=Integer.MIN_VALUE;
if(operator.equals("/")&&operator2==0){
System.out.println("can not divide by 0!");
System.exit(-1);
}
if(operator.equals("+"))
result=operator1+operator2;
else if(operator.equals("-"))
result=operator1-operator2;
else if(operator.equals("*"))
result=operator1*operator2;
else if(operator.equals("/"))
result=operator1/operator2;
else{
System.out.println("Error operator!");
System.exit(-1);
}
System.out.println(args[0]+args[1]+args[2]+"="+result);
}
}
public class SimpleCalculator{
public static void main(String[] args){
if(args.length!=3){
System.out.println("Usage:java SimpleCalculator "+"operator1 operator operator2");
System.out.println("Example:java SimpleCalculator 100 + 200");
System.out.println(args[0]+args[1]+args[2]);
System.exit(-1);
}
int operator1=Integer.parseInt(args[0]);
String operator=args[1];
operator=operator.replaceAll("'", "");
args[1]=operator;
int operator2=Integer.parseInt(args[2]);
int result=Integer.MIN_VALUE;
if(operator.equals("/")&&operator2==0){
System.out.println("can not divide by 0!");
System.exit(-1);
}
if(operator.equals("+"))
result=operator1+operator2;
else if(operator.equals("-"))
result=operator1-operator2;
else if(operator.equals("*"))
result=operator1*operator2;
else if(operator.equals("/"))
result=operator1/operator2;
else{
System.out.println("Error operator!");
System.exit(-1);
}
System.out.println(args[0]+args[1]+args[2]+"="+result);
}
}
追问
灰常感谢,对了!
展开全部
"*"不能直接传递,"*"默认会被替换成虚拟机的参数,如果带星号,这么传递"1 * 5",全部放在双引号里面
更多追问追答
追问
那乘法要怎么写呢
追答
你把所有运算表达式都用双引号括起来 "操作数1 操作符 操作数2"
当作一个参数传递给main
然后以空格分隔成3个变量
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看了楼上贴的错误,应该是args参数传错了?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询