java编程题:随机产生20个50~100之间的整数,输出这20个数并找出最大数和最小数。
答案:4 悬赏:30 手机版
解决时间 2021-11-18 13:28
- 提问者网友:龅牙恐龙妹
- 2021-11-17 23:04
java编程题:随机产生20个50~100之间的整数,输出这20个数并找出最大数和最小数。
最佳答案
- 五星知识达人网友:玩世
- 2021-11-18 00:21
public class P1 {
private int[] num=new int[20];
private int Maxnum,Minnum;
public P1(){
for(int i =0;i<20;i++){
num[i]=50+(int)(Math.random()*50);
}
}
public void print(){
for(int j=0;j<20;j++){
System.out.print(num[j]+" ");
}
System.out.println();
}
public void setMaxAndMin(){
Maxnum=Minnum=num[0];
for(int k=1;k<20;k++){
if(Maxnum Maxnum=num[k];
}else if(Minnum>num[k]){
Minnum=num[k];
}
}
}
public int getMax(){
return Maxnum;
}
public int getMin(){
return Minnum;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
P1 p = new P1();
p.print();
p.setMaxAndMin();
System.out.println("最大值为:"+p.getMax());
System.out.println("最小值为:"+p.getMin());
}
}
private int[] num=new int[20];
private int Maxnum,Minnum;
public P1(){
for(int i =0;i<20;i++){
num[i]=50+(int)(Math.random()*50);
}
}
public void print(){
for(int j=0;j<20;j++){
System.out.print(num[j]+" ");
}
System.out.println();
}
public void setMaxAndMin(){
Maxnum=Minnum=num[0];
for(int k=1;k<20;k++){
if(Maxnum
}else if(Minnum>num[k]){
Minnum=num[k];
}
}
}
public int getMax(){
return Maxnum;
}
public int getMin(){
return Minnum;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
P1 p = new P1();
p.print();
p.setMaxAndMin();
System.out.println("最大值为:"+p.getMax());
System.out.println("最小值为:"+p.getMin());
}
}
全部回答
- 1楼网友:十年萤火照君眠
- 2021-11-18 03:08
package com.lanqiao.Test;
import java.util.Random;
public class Chapter6_Test3 {
static Random rand = new Random();
public static void main(String[] args) {
int[] number = new int[10];
int count = 0;
while(count < number.length) {
number[count] = rand.nextInt(10);
Chapter6_Test3.RemoveSame(count, number);
count ++;
}
for(int x : number) {
System.out.print(x + " ");
}
}
public static void RemoveSame(int count, int[] number) { //去重复
for(int i = 0; i < count; i++)
if(number[i] == number[count]) {
number[count] = rand.nextInt(10);
Chapter6_Test3.RemoveSame(count, number);
}
}
}
import java.util.Random;
public class Chapter6_Test3 {
static Random rand = new Random();
public static void main(String[] args) {
int[] number = new int[10];
int count = 0;
while(count < number.length) {
number[count] = rand.nextInt(10);
Chapter6_Test3.RemoveSame(count, number);
count ++;
}
for(int x : number) {
System.out.print(x + " ");
}
}
public static void RemoveSame(int count, int[] number) { //去重复
for(int i = 0; i < count; i++)
if(number[i] == number[count]) {
number[count] = rand.nextInt(10);
Chapter6_Test3.RemoveSame(count, number);
}
}
}
- 2楼网友:从此江山别
- 2021-11-18 02:30
CLS
RANDOMIZE TIMER
FOR I=1 TO 20
A(I)=INT(RND*51+50)
PRINT A(I);
NEXT I
FOR I=1 TO 20-1
FOR J=I +1 TO 20
IF A(I) NEXT J
NEXT I
PRINT A(1);A(20)
END
RANDOMIZE TIMER
FOR I=1 TO 20
A(I)=INT(RND*51+50)
PRINT A(I);
NEXT I
FOR I=1 TO 20-1
FOR J=I +1 TO 20
IF A(I) NEXT J
NEXT I
PRINT A(1);A(20)
END
- 3楼网友:神的生死簿
- 2021-11-18 01:49
randomize timer 产生随机种子
for i=1 to 20
a=int(rnd*51+50) 把取到的50~100之间的随机整数给a变量
print a; 输出随机数
next i
end
这是我的想法,偶也是个编程小学生啦啦。
for i=1 to 20
a=int(rnd*51+50) 把取到的50~100之间的随机整数给a变量
print a; 输出随机数
next i
end
这是我的想法,偶也是个编程小学生啦啦。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯