java测试题
发布网友
发布时间:2022-04-25 01:20
我来回答
共3个回答
热心网友
时间:2023-10-19 08:24
public static void main(String[] args) {
int x = 0, y = 0, z = 0;
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("please input X (X > 0):");
x = Integer.valueOf(bf.readLine());
if (x <= 0 ) {
System.out.println("X is OutOfBound.");
System.exit(1);
}
System.out.println("please input Y(1 < Y < 100):");
y = Integer.valueOf(bf.readLine());
if (y <= 1 || y >= 100) {
System.out.println("Y is OutOfBound.");
System.exit(1);
}
System.out.println("please input Z (Z > 0):");
z = Integer.valueOf(bf.readLine());
if (z <= 0 ) {
System.out.println("Z is OutOfBound.");
System.exit(1);
}
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
// with x create random
Random random = new Random();
int[] array = new int[x];
System.out.print("print:");
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(100);
System.out.print(array[i] + " ");
}
System.out.println("");
List<Integer> list = new ArrayList<Integer>(0);
int i = 0;
while (i < array.length) {
int j = i;
for (; j < array.length; j ++) {
if (array[j] > y) {
list.add(array[j]);
} else {
break;
}
}
i = j + 1;
if (list.size() > z) {
for (int k = 0 ; k < list.size() ; k++) {
System.out.print(list.get(k) + " ");
}
System.out.println("");
}
list = new ArrayList<Integer>(0);
}
}
热心网友
时间:2023-10-19 08:25
你这个问题的输出结果格式没有很明确的说明,我就是把每z个大于等于y的数一起输出了
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
private static Random random = new Random();
private int x;
private int y;
private int z;
private int[] numbers;
private List<Integer> lists = new ArrayList<Integer>();
private List<String> list = new ArrayList<String>();
/**
* @return 输入条件
* @throws Exception
*/
public String input() throws Exception {
System.out.println("请输入x的值(1~100):");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String msg = in.readLine();
if (isNumeric(msg)) {
x = Integer.parseInt(msg);
} else {
return "x不符合要求";
}
System.out.println("请输入y的值(1~100):");
msg = in.readLine();
if (isNumeric(msg)) {
y = Integer.parseInt(msg);
if (y <= 1 || y >= 100) {
return "y不符合要求";
}
} else {
return "y不符合要求";
}
System.out.println("请输入z的值(1~100):");
msg = in.readLine();
if (isNumeric(msg)) {
z = Integer.parseInt(msg);
} else {
return "z不符合要求";
}
return "随机生成" + x + "个1到100的整数,再找出这些随机整数中大于等于" + y + "的,并且连续次数大于或等于"
+ z + "的数据";
}
/**
* 计算数据
*/
public void count() {
numbers = new int[x];
for (int i = 0; i < x; i++) {
numbers[i] = random.nextInt(100) + 1;
if (numbers[i] >= y) {
lists.add(numbers[i]);
}
}
for (int i = 0; i < lists.size(); i++) {
boolean isCheck = true;
for (int j = 0; j < z; j++) {
try {
if (lists.get(i + j)<y) {
isCheck = false;
}
} catch (IndexOutOfBoundsException e) {
isCheck = false;
continue;
}
}
if (isCheck) {
String str = "";
for (int j = i; j < i+z; j++) {
str += lists.get(j)+",";
}
list.add(str);
}
}
}
/**
* 输出结果
*/
public void show() {
System.out.print("随即生成的" + x + "个数");
for (int i = 0; i < x; i++) {
System.out.print("," + numbers[i]);
}
System.out.print("\n大于" + y + "的数");
for (Integer i : lists) {
System.out.print("," + i);
}
System.out.print("\n满足条件的结果:");
for (String i : list) {
System.out.println(i);
}
}
public static void main(String[] args) throws Exception {
Test test = new Test();
String str = test.input();
System.out.println(str);
if (!str.equals("x不符合要求") && !str.equals("y不符合要求")
&& !str.equals("z不符合要求")) {
test.count();
test.show();
}
}
/**
* 判断字符串是否为数字组成的
* @param str
* @return
*/
public static boolean isNumeric(String str) {
if (str == null) {
return false;
}
Pattern pattern = Pattern.compile("[0-9]+");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
}
热心网友
时间:2023-10-19 08:25
连续次数我不明白是什么意思?我理解为循环的次数,如果不是,请标明下,我在改改,大概流程就是下面的程序,如果满意,请采纳:
public static void main(String args[]){
Random t=new Random();
int[] x=new int[100];
for(int i=0;i<x.length;i++){
x[i]=t.nextInt(100)+1;
}
System.out.print("随机选取100个数字(1-100):");
for(int a:x){
System.out.print(a+"\t");
}
System.out.println();
System.out.print("随机选取一个Y(1<y<100):");
int y=t.nextInt(99)+2;
System.out.print(y);
System.out.println();
System.out.print("随机选取一个z:");
int z=t.nextInt(100)+1;
System.out.println(z);
System.out.print("x个随机整数中大于等于y(1<y<100)的,并且连续次数大于或等于z的数据:");
for(int i=0;i<x.length;i++){
if((i+1)>=z){//循环次数大于Z
if((x[i]>=y) ){//x>y
System.out.print(x[i]+"\t");
}
}
}
}追问比如y=28,连续有几个数38,35,29,72都大于28,连续个数就为4,当连续数个数大于z时,输出这若干个数据,谢谢前辈啊,我是初学者