import java.util.*;
class Calculate
{
int op1;
int op2;
int edl;
char op;
static int count;
Calculate()
{
count++;
System.out.println("请输入第"+count+"组所要计算的数值:");
Scanner sc=new Scanner(System.in);
boolean f=true;
do
{
f=true;
try
{
System.out.print("请输入整数op1:");
op1=Integer.parseInt(sc.nextLine()) ;
}
catch(NumberFormatException e1)
{
System.out.println("输入有误,请重新输入:");
f=false;
}
}while(f==false);
do
{
f=true;
System.out.print("请输入运算符op:");
op=sc.nextLine().charAt(0);
}while(!(op=='+'||op=='-'||op=='/'||op=='*'));
do
{ f=true;
try
{
System.out.print("请输入整数op2:");
op2=Integer.parseInt(sc.nextLine());
if(op=='/'&&op2==0)
{
System.out.println("输入有误,请重新输入:");
f=false;
}
}
catch(NumberFormatException e2)
{
System.out.println("输入有误,请重新输入:");
f=false;
}
}while(f==false||(op=='/'&&op2==0));
}
void edl()
{
System.out.println("所求的运算是:"+op1+op+op2+".");
switch(op)
{
case '-':edl=op1-op2;break;
case '+':edl=op1+op2;break;
case '*':edl=op1*op2;break;
case '/':edl=op1/op2;break;
}
}
void Jg()
{
System.out.println("结果等于:"+op1+op+op2+"="+edl+",");
}
public static void main(String args[])
{
Calculate w1=new Calculate();
w1.edl();
w1.Jg();
Calculate w2=new Calculate();
w2.edl();
w2.Jg();
Calculate w3=new Calculate();
w3.edl();
w3.Jg();
}
}
大家不要笑话啊 |
|