四则运算 来源:一位热心的网友 http://www.tqcto.com/article/software/336297.html

功能:实现真分数的四则运算

语言:Java  平台:JDK下的eclipse

github地址:https://github.com/yeershao/hello-world/commit/9920a987de88fdf878cc2a2e404b405fa96485ea

代码:

public class Computting

{

private String getPfQuestion() {
//说明:getPfQuestion()得到真分数题目函数,随机生成真分数题目,返回真分数题目字符串。
int t = 0;
String strz = "";

int x1=1+(int)(Math.random()*10);
int x2=1+(int)(Math.random()*10);//分母

int y1=1+(int)(Math.random()*10);
int y2=1+(int)(Math.random()*10);//分母

if( x2 <= x1 ){
t = x2;
x2 = x1;
x1 = t;
if( x1 == x2 ) {
x1 = x1 - 1;//保证分子比分母小
}
}

if( y2 <= y1 ){
t = y2;
y2 = y1;
y1 = t;
if( y1 == y2 ) {
y1 = y1 - 1;//保证分子比分母小
}
}

int z=1+(int)(Math.random()*100);

if( z<=25 ) {
strz = "×";
}
if( z>25 && z<=50 ) {
strz = "÷";
if( y1 == 0 )
y1 = y1 + 1;//除数的分子不能为0是不是
}
if( z>50 && z<=75) {
strz = "+";
}
if( z>75 ) {
strz = "-";
if( x1/x2 > y1/y2 ) {

t = x1;
x1 = y1;
y1 = t;

t = x2;
x2 = y2;
y2 = t;//保证减法是大数减去小数
}
}

String strx1 = String.valueOf( x1 );
String strx2 = String.valueOf( x2 );
String stry1 = String.valueOf( y1 );
String stry2 = String.valueOf( y2 );

String ques = " " + strx1 + "/" + strx2 + strz + stry1 + "/" + stry2 + "=";

return ques;
}//getPfQuestion

public static void main(String args[])
{
Computting com=new Computting();
Scanner scanner=new Scanner(System.in);
a:
String str=com.getPfQuestion();
System.out.println(str);
double d=scanner.nextDouble();

System.out.println("Continue?");
int c=scanner.nextChar();
if(c=='y')
goto a:
}
}

转载于:https://www.cnblogs.com/yeershao/p/7523615.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值