产生25个随机数,针对每个数值,使用if-then-else语句来区分该值究竟大于、等于、还是小于下一个随机数?
2个回答
展开全部
public static void main(String[] args){
Random rd=new Random();
int[] num=new int[25];
for(int i=0;i<25;i++){
num[i]=rd.nextInt(100);
}
for(int j=0;j<24;j++){
if(num[j]<num[j+1]){
System.out.println(num[j]+"小于"+num[j+1]);
}else if(num[j]<num[j+1]){
System.out.println(num[j]+"等于"+num[j+1]);
}else{
System.out.println(num[j]+"大于"+num[j+1]);
}
}
}
Random rd=new Random();
int[] num=new int[25];
for(int i=0;i<25;i++){
num[i]=rd.nextInt(100);
}
for(int j=0;j<24;j++){
if(num[j]<num[j+1]){
System.out.println(num[j]+"小于"+num[j+1]);
}else if(num[j]<num[j+1]){
System.out.println(num[j]+"等于"+num[j+1]);
}else{
System.out.println(num[j]+"大于"+num[j+1]);
}
}
}
追问
Random rd=new Random();
为什么提示这一行错误?
追答
import java.util.Random;放在类最上面,包的引用问题
import java.util.Random;
public class TestRandom {
public static void main(String[] args){
Random rd=new Random();
int[] num=new int[25];
for(int i=0;i<25;i++){
num[i]=rd.nextInt(100);
}
for(int j=0;j<24;j++){
if(num[j]<num[j+1]){
System.out.println(num[j]+"小于"+num[j+1]);
}else if(num[j]<num[j+1]){
System.out.println(num[j]+"等于"+num[j+1]);
}else{
System.out.println(num[j]+"大于"+num[j+1]);
}
}
}
}
展开全部
Option Explicit
Private Sub Command1_Click()
Randomize
Dim i As Integer, j As Integer
Dim a(1 To 25) As Integer
For i = 1 To 25
flag:
a(i) = Int(Rnd * 100)
For j = 1 To i - 1
If a(i) = a(j) Then GoTo flag
Next
Print a(i);
Next
End Sub
Private Sub Command1_Click()
Randomize
Dim i As Integer, j As Integer
Dim a(1 To 25) As Integer
For i = 1 To 25
flag:
a(i) = Int(Rnd * 100)
For j = 1 To i - 1
If a(i) = a(j) Then GoTo flag
Next
Print a(i);
Next
End Sub
追问
这是啥啊?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询