JAVA生成25个int类型的随机数,对每个随机值,将其分类为大于、小于、等于紧随它生成的值
importjava.util.Random;publicclassThird_one{publicstaticvoidmain(Stringargs[]){for(in...
import java.util.Random;
public class Third_one {
public static void main(String args[]){
for(int i = 1; i <= 25;i++){
int a = (new Random()).nextInt(100);
int b = 0;
if(i==1){
System.out.print(a+ " ");
}
else{
if(b==a)
System.out.print("="+a+" ");
else if(b<a)
System.out.print("<"+a+" ");
else
System.out.print(">"+a+" ");
}
b = a;
}
}
}
我写的到底哪里有问题? 展开
public class Third_one {
public static void main(String args[]){
for(int i = 1; i <= 25;i++){
int a = (new Random()).nextInt(100);
int b = 0;
if(i==1){
System.out.print(a+ " ");
}
else{
if(b==a)
System.out.print("="+a+" ");
else if(b<a)
System.out.print("<"+a+" ");
else
System.out.print(">"+a+" ");
}
b = a;
}
}
}
我写的到底哪里有问题? 展开
6个回答
展开全部
LZ这么写不好啊,其实用个数组就解决这个问题了:
import java.util.Random;
public class Test3 {
public static void main(String args[]) {
int [] arr = new int[25];
for (int i = 0; i < arr.length; i++) {
arr[i]=(new Random()).nextInt(100);
}
String s = "";
for (int i=0;i<arr.length;i++) {
if (i!=arr.length -1 ) {
if (arr[i] < arr[i+1]) {
s = " < ";
}
else if (arr[i] > arr[i+1]) {
s = " > ";
}
else {
s = " = ";
}
if (i == 0)
System.out.print(arr[i]+s+arr[i+1]);
else
System.out.print(s+arr[i+1]);
}
}
}
}
满意请采纳@~
import java.util.Random;
public class Test3 {
public static void main(String args[]) {
int [] arr = new int[25];
for (int i = 0; i < arr.length; i++) {
arr[i]=(new Random()).nextInt(100);
}
String s = "";
for (int i=0;i<arr.length;i++) {
if (i!=arr.length -1 ) {
if (arr[i] < arr[i+1]) {
s = " < ";
}
else if (arr[i] > arr[i+1]) {
s = " > ";
}
else {
s = " = ";
}
if (i == 0)
System.out.print(arr[i]+s+arr[i+1]);
else
System.out.print(s+arr[i+1]);
}
}
}
}
满意请采纳@~
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询