所有的代码如下:
public class Ex01 {
public static void dis01() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < (7 - (2 * i)); j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < ((2 * i) + 3); j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void dis02() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10 - i; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void dis03() {
int b = 0;
int a = 0;
int count = 0;
int arr[] = new int[100];
for (int i = 0; i < 100; i++) {
arr[i] = (int) (Math.random() * 100);
if (b < arr[i])
b = arr[i];
if (a > arr[i])
a = arr[i];
if (arr[i] > 50)
count++;
}
System.out.println("最大值为" + b + "\n最小值为" + a + "\n大于50的数量为" + count);
}
public static void main(String[] args) {
System.out.println("第一个输出结果:----------------");
dis01();
System.out.println("第二个输出结果:----------------");
dis02();
System.out.println("第三个输出结果:----------------");
dis03();
}
}
结果如下:
一共三题
public class Test1 {
//第一题
public static void show1(){
for(int i=1;i<=7;i++){
if(i<=4){
for(int j=0;j<8-(2*i-1);j++){
System.out.print("*");
}
System.out.println();
}else{
for(int j=0;j<(2*(i-3)-1);j++){
System.out.print("*");
}
System.out.println();
}
}
}
//第二题
public static void show2(){
for(int i=0;i<10;i++){
for(int j=0;j<10-i;j++){
System.out.print("*");
}
System.out.println();
}
}
//第三题
public static void show3(){
int max = 0;
int min = 0;
int num = 0;
int arr[] = new int[100];
for(int i=0;i<100;i++){
arr[i] = (int)(Math.random()*100);
if(maxmax = arr[i];
if(min>arr[i])
min = arr[i];
if(arr[i]>50)
num++;
}
System.out.println("max:"+max+" min:"+min+" num:"+num);
}
public static void main(String[] args) {
show1();
show2();
show3();
}
}
1-2不写了
3.
int sum=0,min=0,max=0;
ArrayListlist = new ArrayList ();
for(int i=0;i<=99;i++){
list.add( (int)(Math.random()*100));
}
Object[] o = list.toArray();
Arrays.sort(o);
for(int j=0;j<=99;j++){
int v = Integer.valueOf(o[j].toString());
min=(Integer) o[0];
max = (Integer) o[99];
if(v>50){
System.out.println(v);
sum++;
}
}
System.out.println(min);
System.out.println(max);
System.out.println("sum="+sum);