标题: 随机加减乘除那儿出问题(红色标注)详细说明下 谢谢了! [打印本页] 作者: 郭兴业 时间: 2013-4-6 12:22 标题: 随机加减乘除那儿出问题(红色标注)详细说明下 谢谢了! import java.util.*;
public class Test1
{
public static void main(String[] args)
{
// int a = 10;
// int b = 23;
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数:");
int a = input.nextInt();
System.out.println("请再输入一个数:");
int b = input.nextInt();
char[] ch = {'+','-','*','/'};
char c = ch[0];
{
for(int i = 0; i < ch.length; i ++)
{
Random ran = new Random();
c = (char) ran.nextInt(ch.length);
}
}
Operation op = OperationFactory.getInstance(c);
int result = op.oper(a,b);
System.out.println(""+a+c+b+" ="+result);
}
}
public class OperationFactory
{
public static Operation getInstance(char c)
{
Operation op = null;
switch(c)
{
case '+' : op = new Add();break;
case '-' : op = new Sub();break;
case '*' : op = new Mul();break;
case '/' : op = new Div();break;
}
return op;
}
}
public class Operation
{
int oper(int a ,int b)
{
return 0;
}
}
class Add extends Operation
{
int oper (int a , int b)
{
return a + b;
}
}
class Div extends Operation
{
int oper (int a , int b)
{
return a / b;
}
}
class Sub extends Operation
{
int oper (int a , int b)
{
return a - b;
}
}
class Mul extends Operation
{
int oper (int a , int b)
{
return a * b;
}
}作者: 郭兴业 时间: 2013-4-6 13:06
研究出来了 import java.util.*;
public class Test1
{
public static void main(String[] args)
{
Random ran = new Random();
// int a = 10;
// int b = 23;
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数:");
int a = input.nextInt();
System.out.println("请再输入一个数:");
int b = input.nextInt();
// 随机获得一个运算符
char[] ch = {'+','-','*','/'};
char c = ch[ran.nextInt(ch.length)];
/* {
for(int i = 0; i < ch.length; i ++)
{
Random ran = new Random();
c = (char) ran.nextInt(ch.length);
}
}
*/
Operation op = OperationFactory.getInstance(c);
int result = op.oper(a,b);
System.out.println("运算 : "+a+" "+" "+c+" "+" "+b+" "+" = "+result);
}
} 作者: 山西_李帅 时间: 2013-4-6 15:28
import java.util.*;
public class Test1
{
public static void main(String[] args)
{
// int a = 10;
// int b = 23;
//从键盘获取两个数
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数:");
int a = input.nextInt();
System.out.println("请再输入一个数:");
int b = input.nextInt();
//建立一个数组,随机得到一个运算符号
char[] ch = {'+','-','*','/'};
Random ran = new Random();
char c = ch[ran.nextInt(ch.length)];
Operation op = OperationFactory.getInstance(c);
int result = op.oper(a,b);
System.out.println(""+a+c+b+" ="+result);
}
}
class OperationFactory
{
public static Operation getInstance(char c)
{
Operation op = null;
switch(c)
{
case '+' : op = new Add();break;
case '-' : op = new Sub();break;
case '*' : op = new Mul();break;
case '/' : op = new Div();break;
}
return op;
}
}
class Operation
{
int oper(int a ,int b)
{
return 0;
}
}
class Add extends Operation
{
int oper (int a , int b)
{
return a + b;
}
}
class Div extends Operation
{
int oper (int a , int b)
{
return a / b;
}
}
class Sub extends Operation
{
int oper (int a , int b)
{
return a - b;
}
}
class Mul extends Operation
{
int oper (int a , int b)
{
return a * b;
}
}