四则运算第一、第二阶段以及借鉴的第三阶段


第一阶段 完成一个随机数的

import java.util.Random;

public class sizeyunxuan {



public static void main(String[] args) {
int a;
for(a=0;a<8;a++)
{
Random r= new Random();
int i=r.nextInt(10);
int j=r.nextInt(10);
 System.out.println(i+"+"+j+"=");
 System.out.println(i+"-"+j+"=");
 System.out.println(i+"*"+j+"=");
 System.out.println(i+"/"+j+"=");
}

}

}

第二阶段 完成不重复的四则运算

package myi;
import java.util.Random;

import java.util.Scanner;


public class sizeyunxuan {

public static void main(String[] args) {

int []a=new int[100];
int []b=new int[100];


Scanner scan =new Scanner(System.in);
int n;
System.out.println("请输入需要的题目数:");
n=scan.nextInt();



int i,j;
for(i=0;i<n;i++)
{

Random r=new Random();
a[i]=r.nextInt(99);
b[i]=r.nextInt(99);

for(j=0;j<i;j++) {
if(a[i]==b[j]&&b[i]==a[j]) {
a[i]=r.nextInt(99)-1;
}
}
int t=(int)r.nextInt(4);
switch(t)
{
case 0:
 System.out.println(a[i]+" + "+b[i]+"=");
 break;
case 1:
     System.out.println(a[i]+" - "+b[i]+"=");
 break;
case 2:
 System.out.println(a[i]+" * "+b[i]+"=");
 break;
case 3:
 System.out.println(a[i]+" / "+b[i]+"=");
 break;
}


 

}

}

}

第三阶段输出三个数的四则运算不重复(带括号)随机(j借鉴网络上大佬的代码与我自己的思路有所不同但基本结构以及本质都一样)

public class myi { public static void main(String[] args) throws IOException { File file= new File("C:\\Users\\ASUS\\Desktop\\Java\\2020 3 10\\src\\a\\result.txt"); PrintStream ps = new PrintStream("C:\\Users\\ASUS\\Desktop\\Java\\2020 3 10\\src\\a\\shengcheng.txt"); Writer out = new FileWriter(file); int c; Scanner sc=new Scanner(System.in); System.out.print("请输入题目个数:"); int tg=sc.nextInt(); System.out.print("请输入操作数的范围(如 100,1000等):"); int tf=sc.nextInt(); System.out.println("请选择是否有负数:1:有 0:没有"); int zf=sc.nextInt(); System.out.println("请选择是否包含*或者/:2:否 4:是 "); int fu=sc.nextInt(); System.setOut(ps); for(int i=0;i<tg;i++) {//循环控制题目个数 //第一个操作数的选择 int a=(int)(Math.random()*tf); if(zf==1) {//有负数 int p=(int) (Math.random()*2); switch(p) { case 0:a=a*(-1);//取负数 System.out.print(a);break; case 1: System.out.print(a);break; } } if(zf==0)System.out.print(a); else{//选择两个字符‘+’‘-’ int k=(int)(Math.random()*2); switch(k) {//随机选择运算符 case 0:System.out.print("+"); break; case 1:System.out.print("-"); break; } int b=(int)(Math.random()*(tf-a)); while(b>a)b=(int)(Math.random()*(tf-a)); if(zf==1) {//有负数 int p=(int) (Math.random()*2); switch(p) { case 0:b=b*(-1);//取负数 System.out.print(b);break; case 1: System.out.print(b);break; } } else System.out.print(b); System.out.println("="); String huanhang="\r\n"; if(k==0) { c=a+b; out.write( Integer.toString(c)+huanhang); } else {c=a-b; out.write( Integer.toString(c)+huanhang);} } if(fu==4) {//四个字符的‘+’‘-’‘*’‘/’ int k=(int)(Math.random()*4+1); switch(k) {//随机选择运算符 case 1:System.out.print("+");break; case 2:System.out.print("-");break; case 3:System.out.print("*");break; case 4:System.out.print("/");break; } //第二个操作数的选择 int b=(int) (Math.random()*tf+1); if(zf==1) {//有负数 int p=(int) (Math.random()*2); switch(p) { case 0:b=b*(-1);//取负数 System.out.print(b);break; case 1: System.out.print(b);break; } } if(zf==0) System.out.print(b); System.out.println("="); if(k==1) { c=a+b;out.write(c); } if(k==2) { c=a-b;out.write(c); } if(k==3) { c=a*b;out.write(c); } if(k==4) { c=a/b;out.write(c); } } }out.close(); } }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM