math类中有个random()方法,可以得到0到1中的随机数,你再乘个系数33,再加个类型转换,重复六遍,排序,ok
解决方案 »
- jdbc 连接mysql的问题
- 类的方法的变量是在类加载时被创建的还是在方法调用时被创建的?
- 学校有个比赛..想问问大家可以用Java做个什么样的程序参加比赛比较好??
- 请问wait()与notify()如何使用?
- 急求java小程序一个,各位高手帮忙,举手之劳,在线等
- 各位帅哥帮个忙?
- 为什么我的jb8总是保存不了jsp文件,以保存jsp文件的时候就出现com.borland.primetime.util.AssertionException:euc_cn
- 调查一下,大家过节费有多少,:)
- 开发java程序在用什么开发环境比较好??大家都来谈谈
- 为什么我下载了JDK安装后却在DOS下无法编译?听说要设置环境变量?那怎样设置呢?谢谢
- 关于重写JPanel的paint方法的问题,麻烦大家看看。
- connection.close()为什么无法关闭执行中的存储过程
{
public static void main(String[] args)
{
int[] numbers = new int[6];
for(int i=0;i<6;i++)
{
int temp = (int)(Math.random() * 33); // 0--32
numbers[i] = temp;
}
for(int i=0;i<5;i++)
{
for(int j=i;j<5;j++)
{
int tp;
if(numbers[i]>numbers[j])
{
tp = numbers[j];
numbers[j]=numbers[i];
numbers[i]=tp;
}
}
}
for(int i=0;i<5;i++)
{
System.out.print(numbers[i]+" ");
}
}呵呵, 简单写了个
public static void main(String[] args){
int[] array = new int[6];
for(int i = 0; i < 6;i++){
array[i]=(int)(33* Math.random());
System.out.print(array[i]+" ");
}
sortArray(array);
System.out.println("\nafter sort:");
for(int i=0;i<6;i++){
System.out.print(array[i]+" ");
}
}
static void sortArray(int[] array){
for(int i=0;i<6;i++)
for(int j=i+1;j<6;j++){
if(array[j]<array[i]){
int temp=0;
temp = array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
}
public static void main (String[] args){
TestRandom tr = new TestRandom();
int haha[] = new int[6];
for(int i = 0;i <= 5 ;i ++){
haha[i] = (int)(33*Math.random());
System.out.println(haha[i]);
}
int temp = 0 ;
for(int k = 0;k <=5; k ++){
for(int j = 0;j <= 4;j ++ ){
if(haha[j] > haha[j+1]){
temp = haha[j];
haha[j] = haha[j+1];
haha[j+1] = temp;
}
}
}
for(int i = 0;i <= 5 ;i ++){
System.out.println(haha[i]);
}
}
}
2。排序时还可用到基本数据结构类Arrays中的sort()方法,充分利用组件技术
public static void main (String[] args){
TestRandom tr = new TestRandom();
int haha[] = new int[6];
for(int i = 0;i < 6 ;i ++){
haha[i] = (int)(33*Math.random());
while( true ){
int temp = 0;
for(int j = 0; j < i;j ++ ){
if(haha[i] == haha[j]||haha[i] == 0)haha[i] = (int)(33*Math.random());//判断是否和前数相等及是否为零.若是则重取随机数.
else temp ++;
}
if( temp == i)break;
}
System.out.println(haha[i]);
}
int temp = 0 ;
System.out.println();
for(int k = 0;k <=4; k ++){
for(int j = 0;j <= 4-k;j ++ ){
if(haha[j] > haha[j+1]){
temp = haha[j];
haha[j] = haha[j+1];
haha[j+1] = temp;
}
}
}
for(int i = 0;i <= 5 ;i ++){
System.out.println(haha[i]);
}
}
}完善了下,没有逻辑上的问题了应该