1.请设计一个小游戏,在程序启动后,
可以随机生成一个大小可变的直角三角形(行高在5至10之间);
import java.util.Random;
public class StarRandom {
public static void main(String[] args) {
Random r = new Random();
int number = r.nextInt(3) + 5;
System.out.println("本次随机生成的是高为" + number + "的三角形");
for (int i = 0; i <= number; i++) {
for (int j = 1; j <= i; j++) {
if(j != i){
System.out.print("*" + " ");
}
else {
System.out.println("*");
}
}
}
}
}
2.(1)定义一个长度为10的int类型数组,元素值使用随机数生成(范围为50-80)
(2)打印该数组中满足个位和十位不包含7的元素。
public class Test03_03 {
public static void main(String[] args) {
Random r = new Random();
int[] arr = new int[10];
for (int i = 0; i < 10; i++) {
arr = r.nextInt(31) + 50;
}
System.out.println("随机生成的数组为:");
for (int j = 0; j < 10; j++) {
System.out.print(arr[j] + " ");
}
System.out.println();
System.out.println("该数组中不包含7的元素有:");
for (int x = 0; x < 10; x++) {
if(arr[x] % 10 != 7 && arr[x] / 10 != 7) {
System.out.print(arr[x] + " ");
}
}
}
}
1.定义一个用于存放班级分数的数组 int[] score;数组长度为5,并使用随机数(范围为0-100)为数组赋值
2.求出班级不及格人数(分数低于60分的就是不及格)
3.求出班级的平均分
4.求出班级的总分数
import java.util.Random;
public class Test03_04 {
public static void main(String[] args) {
Random r = new Random();
int[] score = new int[5];
int sum = 0;
double average;
int count = 0;
System.out.println("该班级的分数为:");
for (int i = 0; i < score.length; i++) {
score = r.nextInt(101);
System.out.print(score + " ");
}
for (int i = 0; i < score.length; i++) {
if(score < 60) {
count ++;
}
}
System.out.println();
System.out.println("该班有"+ count +"个不及格的人");
System.out.println("该班级的总分为:");
for (int j = 0; j < score.length; j++) {
sum += score[j];
}
System.out.println(sum);
System.out.println("该班级的平均分为:");
average = sum / score.length;
System.out.println(average);
}
}
|
|