当前位置:编程学习 > JAVA >>

Java初学:一个小程序,请高手指点

import java.util.ArrayList;

public class E02_RandomInts {
class compareNumber {
int[] random1 = new int[25];
int random2 = 0;
public ArrayList<Integer> list_min = new ArrayList<Integer>();
public ArrayList<Integer> list_max = new ArrayList<Integer>();
public ArrayList<Integer> list_equ = new ArrayList<Integer>();

public compareNumber(int[] ram, int ramd) {
this.random1 = ram;
this.random2 = ramd;
for (int a : random1) {
if (a > random2)
list_max.add(a);
else if (a < random2)
list_min.add(a);
else
list_equ.add(a);
}
}
}

public static void arrayListPrt(ArrayList<Integer> list) {
int i = 0;
int len = list.size();
System.out.print(list.toString() + ":" + len + ":");
for (int tmp : list) {
System.out.print(tmp + " ");
}

}

public static void main(String[] args) {
int[] random1 = new int[25];
int random2 = (int) (Math.random() * 100);
for (int i = 0; i <= 25; i++)
random1[i] = (int) (Math.random() * 100);
E02_RandomInts ri = new E02_RandomInts();
× compareNumber cm = new ri.compareNumber(random1,random2);
arrayListPrt(cm.list_min);
arrayListPrt(cm.list_max);
arrayListPrt(cm.list_equ);
}
}

具体目的:* Write a program to generate 25 random int
* values. Use an if-else statement for each value
* to classify it as greater than, less than, or
* equal to a second randomly generated value.
************************************************/
×真的个位置 出错“ri cannot be resolved to a type”在帮看看真个写的有什么问题吧!多谢啦



java class arraylist --------------------编程问答-------------------- import java.util.ArrayList;

public class E02_RandomInts {


public static void arrayListPrt(ArrayList<Integer> list) {
int i = 0;
int len = list.size();
System.out.print(list.toString() + ":" + len + ":");
for (int tmp : list) {
System.out.print(tmp + " ");
}

}

public static void main(String[] args) {
int[] random1 = new int[25];
int random2 = (int) (Math.random() * 100);
for (int i = 0; i <= 25; i++)
random1[i] = (int) (Math.random() * 100);
// E02_RandomInts ri = new E02_RandomInts();
compareNumber cm = new compareNumber(random1,random2);
arrayListPrt(cm.list_min);
arrayListPrt(cm.list_max);
arrayListPrt(cm.list_equ);
}
}
class compareNumber {
int[] random1 = new int[25];
int random2 = 0;
public ArrayList<Integer> list_min = new ArrayList<Integer>();
public ArrayList<Integer> list_max = new ArrayList<Integer>();
public ArrayList<Integer> list_equ = new ArrayList<Integer>();

public compareNumber(int[] ram, int ramd) {
this.random1 = ram;
this.random2 = ramd;
for (int a : random1) {
if (a > random2)
list_max.add(a);
else if (a < random2)
list_min.add(a);
else
list_equ.add(a);
}
}
}


我有更新了一下 --------------------编程问答-------------------- int[] random1 = new int[25];
for (int i = 0; i <= 25; i++) --------------------编程问答-------------------- import java.util.ArrayList;

public class E02_RandomInts {


public static void arrayListPrt(ArrayList<Integer> list) {
int i = 0;
int len = list.size();
System.out.println( len + ":" + list.toString());
// for (int tmp : list) {
// System.out.print(tmp + " ");
// }

}

public static void main(String[] args) {
int[] random1 = new int[25];
int random2 = (int) (Math.random() * 100);
System.out.println("The rulr: " + random2);
for (int i = 0; i < 25; i++)
random1[i] = (int) (Math.random() * 100);
// E02_RandomInts ri = new E02_RandomInts();
compareNumber cm = new compareNumber(random1,random2);
// System.out.println(cm.list_min.size());

System.out.print("MIN :");arrayListPrt(cm.list_min);
System.out.print("MAX :");arrayListPrt(cm.list_max);
System.out.print("EQUAL :");arrayListPrt(cm.list_equ);
}
}
class compareNumber {
int[] random1 = new int[25];
int random2 = 0;
public ArrayList<Integer> list_min = new ArrayList<Integer>();
public ArrayList<Integer> list_max = new ArrayList<Integer>();
public ArrayList<Integer> list_equ = new ArrayList<Integer>();

public compareNumber(int[] ram, int ramd) {
this.random1 = ram;
this.random2 = ramd;
for (int a : random1) {
if (a > random2)
list_max.add(a);
else if (a < random2)
list_min.add(a);
else
list_equ.add(a);

}
}
}

The rulr: 19
MIN :6:[11, 18, 8, 12, 2, 11]
MAX :19:[34, 85, 42, 31, 51, 49, 74, 43, 44, 33, 85, 66, 82, 24, 41, 89, 98, 36, 55]
EQUAL :0:[]


完整的编译过的,谢谢前辈。小辈有家淘宝店 oceanwalker.taobao.com 喜欢什么就随便挑,怎么进的,怎么给您,以示感谢!多谢啦! --------------------编程问答--------------------
import java.util.ArrayList;

public class E02_RandomInts {

public static void arrayListPrt(ArrayList<Integer> list) {
int len = list.size();
System.out.print(list.toString() + ":" + len);
System.out.println();
for (int tmp : list) {
System.out.print(tmp + " ");
}
System.out.println();
System.out.println();
}

public static void main(String[] args) {
int[] random1 = new int[25];
int random2 = (int) (Math.random() * 100);
for (int i = 0; i < 25; i++) {
random1[i] = (int) (Math.random() * 100);
}
// E02_RandomInts ri = new E02_RandomInts();
compareNumber cm = new compareNumber(random1, random2);
arrayListPrt(cm.list_min);
arrayListPrt(cm.list_max);
arrayListPrt(cm.list_equ);
}
}

class compareNumber {
int[] random1 = new int[25];

int random2 = 0;

public ArrayList<Integer> list_min = new ArrayList<Integer>();

public ArrayList<Integer> list_max = new ArrayList<Integer>();

public ArrayList<Integer> list_equ = new ArrayList<Integer>();

public compareNumber(int[] ram, int ramd) {
this.random1 = ram;
this.random2 = ramd;
for (int a : random1) {
if (a > random2)
list_max.add(a);
else if (a < random2)
list_min.add(a);
else
list_equ.add(a);
}
}
}
--------------------编程问答-------------------- 给你改了下。自己琢磨下 --------------------编程问答--------------------
import java.util.ArrayList;

public class E02_RandomInts {
public ArrayList<Integer> list_min = new ArrayList<Integer>();
public ArrayList<Integer> list_max = new ArrayList<Integer>();
public ArrayList<Integer> list_equ = new ArrayList<Integer>();

  public void compareNumber(int[] randomArray, int randInt) {
for (Integer rand : randomArray) {
if (rand > randInt) {
list_max.add(rand);
} else if (rand < randInt) {
list_min.add(rand);
} else {
list_equ.add(rand);
  }
}
  }

  private String listToString(ArrayList<Integer> list) {
int len = list.size();
System.out.println(list.toString() + ", size:" + len + lise.size);
StringBuffer sb = new StringBuffer();
for (Integer tmp : list) {
if (sb.length() > 0) sb.append(' ');
sb.append(tmp);
}
return sb.toString();
  }

    public void showLists() {
System.out.println("list_min");
System.out.println(listToString(list_min));
System.out.println("list_max");
System.out.println(listToString(list_max));
System.out.println("list_equ");
System.out.println(listToString(list_equ));
  }

  public static void main(String[] args) {
  int[] random1 = new int[25];
  int random2 = (int) (Math.random() * 100);
  for (int i = 0; i < random1.length; i++) {
  random1[i] = (int) (Math.random() * 100);
  }
  E02_RandomInts e02 = new E02_RandomInts();
  e02.compareNumber(random1, random2);
  e02.showLists();
  }
}


我又改了改,请前辈review, --------------------编程问答--------------------
import java.util.ArrayList;

public class E02_RandomInts {
public ArrayList<Integer> list_min = new ArrayList<Integer>();
public ArrayList<Integer> list_max = new ArrayList<Integer>();
public ArrayList<Integer> list_equ = new ArrayList<Integer>();

  public void compareNumber(int[] randomArray, int randInt) {
for (Integer rand : randomArray) {
if (rand > randInt) {
list_max.add(rand);
} else if (rand < randInt) {
list_min.add(rand);
} else {
list_equ.add(rand);
  }
}
  }

  private String listToString(ArrayList<Integer> list) {
int len = list.size();
System.out.println(list.toString() + ", size:" + len + list.size());
StringBuffer sb = new StringBuffer();
for (Integer tmp : list) {
if (sb.length() > 0) sb.append(' ');
sb.append(tmp);
}
return sb.toString();
  }

    public void showLists() {
System.out.println("list_min");
System.out.println(listToString(list_min));
System.out.println("list_max");
System.out.println(listToString(list_max));
System.out.println("list_equ");
System.out.println(listToString(list_equ));
  }

  public static void main(String[] args) {
  int[] random1 = new int[25];
  int random2 = (int) (Math.random() * 100);
  for (int i = 0; i < random1.length; i++) {
  random1[i] = (int) (Math.random() * 100);
  }
  E02_RandomInts e02 = new E02_RandomInts();
  e02.compareNumber(random1, random2);
  e02.showLists();
  }
}
--------------------编程问答-------------------- 有个问题就是这“list.toString()”我们本想显示list的名字,但看来不行,只显示了element
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,