ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

日常Java(测试 (二柱)修改版)2021/9/22

2021-09-23 02:04:39  阅读:148  来源: 互联网

标签:Java System a1 修改版 a2 二柱 println append out


题目:

一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做。

二柱一下打印出好多份不同的题目,让孩子做了。老师看了作业之后,对二柱赞许有加。别的老师闻讯, 问二柱能否扩大他的影响力,编个软件,给二年级到四年级都用,多了一些小小的要求: 

1、题目避免重复(第二阶段)

2、可定制(数量/打印方式):输入大的数量值,测试一下系统是否崩溃,反向查找系统是否优化的余地

3、定制操作数的个数

4、定制是否有乘除法

5、定制是否有括号(随机加入)

6 、定制数值范围(确定操作数的取值范围)

 

建立了s包,建立了number类,创建了主函数main();,以及三个函数o1();o2();o3();

package s;

import java.util.Random;
import java.util.Scanner;
import java.util.*;

public class number {
public static void main(String[] args)
{
boolean flag = true;
while(flag) {
System.out.println("请选择功能(1、2、3):");
Scanner sc = new Scanner(System.in);
int z = sc.nextInt();
switch(z) {
case 1:
o1();break;
case 2:
o2();break;
case 3:
o3();break;
default:System.out.println("没有此功能。");break;
}
}
}

public static void o1()//第一阶段
{
Random random = new Random();

Scanner sc = new Scanner(System.in);
System.out.println("请输入打印的题数:");
int m = sc.nextInt();

for(int o = 1; o <= m; o++) {
int a = random.nextInt(4);
int b = random.nextInt(100);
int c = random.nextInt(100);
switch(a)
{
case 0:

System.out.println(b+ "+" +c +"=" );
break;
case 1:

System.out.println(b+ "-" +c +"=" );
break;
case 2:

System.out.println(b+ "*" +c +"=" );
break;
case 3:

System.out.println(b+ "/" +c +"=" );
break;

default:
break;
}
}

}

public static void o2()//第二阶段
{
Random random = new Random();

Scanner sc = new Scanner(System.in);
System.out.println("请输入打印的题数:");
int m = sc.nextInt();

StringBuffer str = new StringBuffer();
String[] s = new String[m];
s[0] = str.toString();

for(int o = 0; o < m; o++) {

int a = random.nextInt(4);
int b = random.nextInt(100);
int c = random.nextInt(100);

str.append(a).append(b).append(c);
StringBuffer str2 = new StringBuffer();
s[o] = str2.append(a).append(b).append(c).toString();

int sum = 0;
for(int q = 0; q < s.length; q++) {
if(s[o].equals(s[q])) {
sum++;
}
}

if(sum == 1)
{
if(a == 0) {

System.out.println(b + "+" + c + "=");
}
if(a == 1) {

System.out.println(b + "-" + c + "=");
}
if(a == 2) {

System.out.println(b + "*" + c + "=");
}
if(a == 3) {

System.out.println(b + "/" + c + "=");
}

}else if(sum != 1){
o--;
continue;

}
}
}

public static void o3()//第三阶段
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入打印的题数:");
int g = sc.nextInt();

System.out.println("有无乘除法,请输入y或n:");
String g1 = sc.next();

System.out.println("是否有括号,请输入y或n:");
String g2 = sc.next();

System.out.println("请输入操作个数(2-4):");
int g3 = sc.nextInt();

System.out.println("请输入操作数范围【50-200】:");
int g4 = sc.nextInt();

Random random = new Random();
int a = random.nextInt(4);
int a0 = random.nextInt(g3)+2;//操作数个数(2-4)

StringBuffer str = new StringBuffer();//动态数组
String[] s = new String[g];
s[0] = str.toString();//返回对象值

for( int i = 0; i < g; i++) {
a = random.nextInt(4);
int a1 = random.nextInt(g4);
int a2 = random.nextInt(g4);
int a3 = random.nextInt(g4);
int a4 = random.nextInt(g4);

if(g4 >=50 && g4 <=200){
a1 = random.nextInt(g4);
a2 = random.nextInt(g4);
a3 = random.nextInt(g4);
a4 = random.nextInt(g4);
}else {
System.out.println("操作数超出范围。");
}

if(g3 == 2) {
str.append(a).append(a1).append(a2);//添加数据
}else if(g3 ==3) {
str.append(a).append(a1).append(a2).append(a3);//添加数据
}else {
str.append(a).append(a1).append(a2).append(a3).append(a4);//添加数据
}

StringBuffer str2 = new StringBuffer();
if(g3 == 2) {
s[i] = str2.append(a).append(a1).append(a2).toString();
}else if(g3 == 3){
s[i] = str2.append(a).append(a1).append(a2).append(a3).toString();
}else {
s[i] = str2.append(a).append(a1).append(a2).append(a3).append(a4).toString();
}

int sum = 0;
for(int q = 0; q < s.length; q++)
{if(s[i].equals(s[q])) {sum++;}}

if(g3 == 2) {
if(g1.equals("n")) {
if(sum == 1)
{
if(a == 0) {System.out.println(a1 + "+" + a2 + "=");}
if(a == 1) {System.out.println(a1 + "-" + a2 + "=");}
if(a == 2) {i--;continue;}
if(a == 3) {i--;continue;}
}else if(sum != 1){i--;continue;}
}else if(g1.equals("y")) {
if(sum == 1)
{
if(a == 0) {System.out.println(a1 + "+" + a2 + "=");}
if(a == 1) {System.out.println(a1 + "-" + a2 + "=");}
if(a == 2) {System.out.println(a1 + "*" + a2 + "=");}
if(a == 3) {System.out.println(a1 + "/" + a2 + "=");}
}else if(sum != 1){i--;continue;}
}
}else if(g3 == 3) {
if(g1.equals("n")) {
if(sum == 1)
{
if(a == 0) {System.out.println(a1 + "-" +"("+ a2 +"+"+a3+")"+ "=");}
if(a == 1) {System.out.println("("+a1 + "-" + a2 +")"+"+"+a3+ "=");}
if(a == 2) {System.out.println("("+a1 + "+" + a2 +")"+"-"+a3+ "=");}
if(a == 3) {System.out.println(a1 + "+" +"("+ a2 +"-"+a3+")"+ "=");}
}else if(sum != 1){i--;continue;}
}else if(g1.equals("y")) {
if(sum == 1)
{
if(a == 0) {System.out.println("("+a1+"*" + a2+")"+"+" + a3 +"=");}
if(a == 1) {System.out.println(a1+"-" +"("+ a2+"/" + a3 +")"+"=");}
if(a == 2) {System.out.println(a1+"-" +"("+ a2+"*" + a3 +")"+"=");}
if(a == 3) {System.out.println("("+a1+"/" + a2+")"+"+" + a3 +"=");}
}else if(sum != 1){i--;continue;}
}
}else if(g3 == 4){
if(g1.equals("n")) {
if(sum == 1)
{
if(a == 0) {System.out.println(a1+"+" + "("+ a2+"+" +a3 +")"+"+" +a4+ "=");}
if(a == 1) {System.out.println(a1+"-" + "("+ a2+"+" +a3 +")"+"-" +a4+ "=");}
if(a == 2) {System.out.println(a1+"-" + a2+"-" + "("+a3 +"-" +a4+")"+ "=");}
if(a == 3) {System.out.println("("+a1+"+" + a2+")"+"-" + a3 +"+" +a4+ "=");}
}else if(sum != 1){i--;continue;}
}else if(g1.equals("y")) {
if(sum == 1)
{
if(a == 0) {System.out.println(a1+"+" + a2+"-" + "("+a3 +"*" +a4+")"+ "=");}
if(a == 1) {System.out.println(a1+"-" +"("+ a2+"/" + a3 +")"+"-" +a4+ "=");}
if(a == 2) {System.out.println("("+a1+"*" + a2+")"+"+" + a3 +"-" +a4+ "=");}
if(a == 3) {System.out.println("("+a1+"/" + a2+")"+"-" +"("+ a3 +"*" +a4+")"+ "=");}
}else if(sum != 1){i--;continue;}
}
}else {
System.out.println("操作个数超出范围。");
}
}

}
}

标签:Java,System,a1,修改版,a2,二柱,println,append,out
来源: https://www.cnblogs.com/dzwj/p/15322509.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有