Skip to content

Commit

Permalink
Two random Numbers
Browse files Browse the repository at this point in the history
对两个随机数进行四则运算
  • Loading branch information
Wallfacer-ZhangBeihai committed Oct 21, 2019
1 parent 8e25c25 commit c17c6f3
Show file tree
Hide file tree
Showing 13 changed files with 367 additions and 0 deletions.
33 changes: 33 additions & 0 deletions two/Add.java
@@ -0,0 +1,33 @@
/**
* Add类为Randoms类的子类
* 该类中进行加法运算
*/
package cn.deu.ccut.two;

public class Add {
public void add(double number,double a,double b) {
int c;
int d;
char opera[]= {' ','(',')'};//Opera[0]~Opera[2]依次输出“ (空格)”,“(”,“)”
if(a>=0) {
c=0;
}else {
c=1;
}

if(b>=0) {
d=0;
}else {
d=1;
}

if(a+b<=number) {
System.out.println(opera[c]+a+opera[c*2]+"+"+opera[d]+b+opera[d*2]+"=");

}else {
a=(int)a/2;
b=(int)b/2;
System.out.println(opera[c]+a+opera[c*2]+"+"+opera[d]+b+opera[d*2]+"=");
}
}
}
20 changes: 20 additions & 0 deletions two/Chengchu.java
@@ -0,0 +1,20 @@
package cn.deu.ccut.two;

import java.util.Scanner;

public class Chengchu {
int a;
public double chengchu() {
Scanner sc=new Scanner(System.in);
try {
System.out.print("是否包含乘除运算,是按1,否按任意数字:");
a=sc.nextInt();

} catch (Exception e) {
System.out.println("请输入正确的取值范围");
chengchu();
}

return a;
}
}
32 changes: 32 additions & 0 deletions two/Division.java
@@ -0,0 +1,32 @@
/**
* Division类为Randoms类的子类
* 该类中进行除法运算
*/
package cn.deu.ccut.two;

public class Division {
public void division(double number,double a,double b) {
int c;
int d;
char opera[]= {' ','(',')'};//Opera[0]~Opera[2]依次输出“ (空格)”,“(”,“)”
if(a>=0) {
c=0;
}else {
c=1;
}
if(b>=0) {
d=0;
}else {
d=1;
}

if(a%b==0) {
System.out.println(opera[c]+a+opera[c*2]+"/"+opera[d]+b+opera[d*2]+"=");
}else {
b=(int)(b/10);
a=(int)(a%10);
a=a*(b+1);
System.out.println(opera[c]+a+opera[c*2]+"/"+opera[d]+b+opera[d*2]+"=");
}
}
}
23 changes: 23 additions & 0 deletions two/Down.java
@@ -0,0 +1,23 @@
/**
* Down类是Primary的子类
* 该类控制题目数量
*/
package cn.deu.ccut.two;
import java.io.*;
import java.util.*;
public class Down {

public double down() {
double b = 0;//运算式的个数
Scanner sc=new Scanner(System.in);
try {
System.out.print("请输入题目的数量:");
b=sc.nextDouble();// 控制题目的数量

} catch (Exception e) {
System.out.println("请输入正确的取值范围");
down();
}
return b;
}
}
38 changes: 38 additions & 0 deletions two/Factory.java
@@ -0,0 +1,38 @@
/**
* Factory为工厂类,是Randoms的子类
* 利用switch语句进行运算法则的选择
* switch(0)为加法; switch(1)为减法; switch(2)为乘法; switch(3)为除法;
*/
package cn.deu.ccut.two;

import java.util.Random;

public class Factory extends Randoms{
Random rand =new Random();
public void factory(double max,double a,double b) {
int i=0;
switch(rand.nextInt(r)) {
case 0:
i++;
Add ad=new Add();
ad.add(max,a,b);
break;
case 1:
i++;
Minus mis=new Minus();
mis.minus(max,a,b);
break;
case 2:
i++;
Multiply mul=new Multiply();
mul.multiply(max,a,b);
break;
case 3:
i++;
Division div=new Division();
div.division(max,a,b);
break;

}
}
}
20 changes: 20 additions & 0 deletions two/Fuhao.java
@@ -0,0 +1,20 @@
package cn.deu.ccut.two;

import java.util.Scanner;

public class Fuhao {
int a;
public double fuhao() {
Scanner sc=new Scanner(System.in);
try {
System.out.print("是否包含负数,是按1,否按其他数字:");
a=sc.nextInt();

} catch (Exception e) {
System.out.println("请输入正确的取值范围");
fuhao();
}

return a;
}
}
28 changes: 28 additions & 0 deletions two/Minus.java
@@ -0,0 +1,28 @@
/**
* Minus类为Randoms的子类
* 在该类中进行加法运算
*/
package cn.deu.ccut.two;

public class Minus{
public void minus(double number,double a,double b) {
int c;
int d;
char opera[]= {' ','(',')'};//Opera[0]~Opera[2]依次输出“ (空格)”,“(”,“)”
if(a<0) {
c=1;
}else {
c=0;
}
if(b<0) {
d=1;
}else {
d=0;
}
if(a>b) {
System.out.println(+opera[c]+a+opera[c*2]+"-"+opera[d]+b+opera[d*2]+"=");
}else {
System.out.println(+opera[c]+a+opera[c*2]+"-"+opera[d]+b+opera[d*2]+"=");
}
}
}
30 changes: 30 additions & 0 deletions two/Multiply.java
@@ -0,0 +1,30 @@
/**
* Multipiy类为Randoms类的子类
* 该类中进行乘法运算
*/
package cn.deu.ccut.two;

public class Multiply {
public void multiply(double number,double a,double b) {
int c;
int d;
char opera[]= {' ','(',')'};//Opera[0]~Opera[2]依次输出“ (空格)”,“(”,“)”
if(a>=0) {
c=0;
}else {
c=1;
}
if(b>=0) {
d=0;
}else {
d=1;
}
if(a*b<=number) {
System.out.println(opera[c]+a+opera[c*2]+"*"+opera[d]+b+opera[d*2]+"=");
}else {
a=a%10;
b=b%10;
System.out.println(opera[c]+a+opera[c*2]+"*"+opera[d]+b+opera[d*2]+"=");
}
}
}
23 changes: 23 additions & 0 deletions two/Primary.java
@@ -0,0 +1,23 @@
/**
* PrimaryÀàÖÐ
*/

package cn.deu.ccut.two;
import java.awt.font.NumericShaper.Range;
import java.util.Scanner;
public class Primary {
public static int scope1;
public static int scope2;

public static void main(String[] args) {
Shut shu=new Shut();
shu.shut();

Down dow=new Down();
System.out.println(dow.down());

Rantwo rant=new Rantwo();
rant.rantwo();
}

}
62 changes: 62 additions & 0 deletions two/Randoms.java
@@ -0,0 +1,62 @@
/**
* Randoms类实现功能(1)————自动生成10道100以内的2个操作数的四则运算算式(+ - * /),要求运算结果也在100以内
* 在Randoms类中定义了两个成员变量a和b
* 在ranDoms方法中调用工厂类——Factory中的factory方法实现功能(1)
*/
package cn.deu.ccut.two;
import java.util.Scanner;
import java.util.Random;
import java.io.FileNotFoundException;
import java.io.PrintStream;

import java.math.BigInteger;
public class Randoms {

static double number=100;
static double b=10;
static int t=0;
static int r=4;
static Random rand =new Random();

public static void main(String[] args) throws FileNotFoundException {
PrintStream ps = new PrintStream("d:/result.txt");
Scanner sc=new Scanner(System.in);
Fuhao fu=new Fuhao();
Chengchu che=new Chengchu();

ranDoms();

t= (int) fu.fuhao();
r=(int) che.chengchu();
if(r!=1) {
r=2;
}else {
r=4;
}
Shut shu=new Shut();
number = shu.shut();
Down dow=new Down();
b=dow.down();
System.setOut(ps);
ranDoms();

}
public static void ranDoms(){
Scanner sc=new Scanner(System.in);
for(int i=0;i<b;i++){
double a= rand.nextInt((int)number);
double b= rand.nextInt((int)number);
if(t==1) {
a=((rand.nextInt(2))==1?-1:1)*a;
b=((rand.nextInt(2))==1?-1:1)*b;
}
Factory fac=new Factory();
fac.factory(number,a,b);
}
}




}

11 changes: 11 additions & 0 deletions two/Rantwo.java
@@ -0,0 +1,11 @@
/**
* Rantwo类是Primary类的子类
* 该类随机生成两个随机数组
*/
package cn.deu.ccut.two;

public class Rantwo extends Primary {
public void rantwo() {

}
}
28 changes: 28 additions & 0 deletions two/Shut.java
@@ -0,0 +1,28 @@
/**
* Shut类是Primary类的子类
* 该类控制输入的题目数值的取值范围
*/

package cn.deu.ccut.two;


import java.io.*;
import java.util.*;

public class Shut {

static double shut() {
Scanner sc=new Scanner(System.in);
double a=0;//运算数的取值范围

try {
System.out.print("请输入操作数的范围:");
a=sc.nextDouble();// 控制题目数值的取值范围

} catch (Exception e) {
System.out.println("请输入正确的取值范围");
shut();
}
return a;
}
}
19 changes: 19 additions & 0 deletions two/Test.java
@@ -0,0 +1,19 @@
/**
* 创建Randoms类的对象ran,并利用ran对象调用Randoms函数中的ranDoms方法实现功能(1)
*
*/
package cn.deu.ccut.two;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Random;
public class Test {

public static void main(String[] args) {
Randoms rand=new Randoms();
rand.ranDoms();


}

}

0 comments on commit c17c6f3

Please sign in to comment.