要求输入数组的大小可以弄过n来控制,例如n=10,数组就可以随机产生10个数,n=20,就可以产生20个,以此类推。然后用随机产生的数组用选择法排序,帮我改改程序,看看哪里不对,谢谢啦:
import java.io.*;
import java.util.Arrays;
import java.util.Random;
public class Selection {
public static void main(String agrs[])throws IOException{
String number;
int a ; BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("please type number");
number=buf.readLine();
a=Integer.parseInt(number);
xx(a);
}
public static void xx(int n){
int[] A=new int[n];
Random rand = new Random();
for(int i=0;i<A.length;i++){
A[i]=rand.nextInt(100); ;
System.out.println(A[i]+"");
selectionSort1(A);
}
}
public static void selectionSort1(int[] x) {
for (int i=0; i<x.length-1; i++) {
for (int j=i+1; j<x.length; j++) {
if (x[i] > x[j]) {
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
System.out.print(x[i]+"");
}
}
}
import java.io.*;
import java.util.Arrays;
import java.util.Random;
public class Selection {
public static void main(String agrs[])throws IOException{
String number;
int a ; BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("please type number");
number=buf.readLine();
a=Integer.parseInt(number);
xx(a);
}
public static void xx(int n){
int[] A=new int[n];
Random rand = new Random();
for(int i=0;i<A.length;i++){
A[i]=rand.nextInt(100); ;
System.out.println(A[i]+"");
selectionSort1(A);
}
}
public static void selectionSort1(int[] x) {
for (int i=0; i<x.length-1; i++) {
for (int j=i+1; j<x.length; j++) {
if (x[i] > x[j]) {
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
System.out.print(x[i]+"");
}
}
}
import java.util.Scanner;public class Selection
{
public static void main(String agrs[]) throws IOException
{
//String number;
int a; //BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
Scanner scanner = new Scanner(System.in);
System.out.println("please type number");
//number=buf.readLine();
a = scanner.nextInt();
xx(a);
} public static void xx(int n)
{
int[] A = new int[n];
//Random rand = new Random();
for (int i = 0; i < A.length; i++)
{
// A[i]=rand.nextInt(100); ;
A[i] = (int) (Math.random() * 100);
System.out.print(A[i] + " ");
}
System.out.println();
selectionSort1(A); } public static void selectionSort1(int[] x)
{
for (int i = 0; i < x.length - 1; i++)
{
for (int j = i + 1; j < x.length; j++)
{
if (x[i] > x[j])
{ int temp = x[i];
x[i] = x[j];
x[j] = temp; } } }
for (int in : x)
{
System.out.print(in + " ");
}
}
}
please type number
5
35 38 86 13 83
13 35 38 83 86
String number;
int a; BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("please type number");
number = buf.readLine();
a = Integer.parseInt(number);
xx(a);
} public static void xx(int n) {
int[] A = new int[n];
Random rand = new Random();
for (int i = 0; i < A.length; i++) {
A[i] = rand.nextInt(100);
System.out.print(A[i] + "\t");
}
System.out.println();
selectionSort1(A);
} public static void selectionSort1(int[] x) {
//定义最小数的位置
int minPosition;
for (int i = 0; i < x.length - 1; i++) {
//设置最小数的位置
minPosition = i;
for (int j = i + 1; j < x.length; j++) {
//如果设置的最小数比查询的下一个数大,设置最小数的位置是查询的数
if (x[minPosition] > x[j]) {
minPosition = j;
}
}
//最小数的位置不是最开始的数
if (minPosition != i) {
//交换最开始的数与最小数的位置
int temp = x[i];
x[i] = x[minPosition];
x[minPosition] = temp;
}
} for (int i = 0; i < x.length; i++) {
System.out.print(x[i] + "\t");
}
System.out.println();
}
}
import java.util.*;public class Select {
static int[] iArray;
static Random r = new Random();
public static void Sort(int[] array){
for(int i=0; i<array.length; i++)
for(int j=i+1; j<array.length; j++){
if(array[i] > array[j]){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.println("Please enter an integer: ");
int i = s.nextInt();
while(i != -1){
iArray = new int[i];
for(int j=0; j<i; j++){
iArray[j] = r.nextInt(100);
System.out.print(iArray[j] +" ");
}
System.out.println();
Sort(iArray);
for(int j=0; j<i; j++){
System.out.print(iArray[j] +" ");
}
i = s.nextInt();
}
System.out.print("\nExit!");
}
}
package Exercise.csdn;import java.io.IOException;
import java.util.Scanner;public class Selection {
public static void main(String agrs[])throws IOException{
int number; Scanner sc=new Scanner(System.in);
System.out.println("please type number");
number=sc.nextInt();
xx(number);
}
public static void xx(int n){
int[] A=new int[n];
for(int i=0;i <A.length;i++){
A[i]=(int)(Math.random()*100);
System.out.println(A[i]+"");
}
System.out.println();
selectionSort1(A); }
public static void selectionSort1(int[] x) {
for (int i=0; i <x.length-1; i++) {
for (int j=i+1; j <x.length; j++) {
if (x[i] > x[j]) {
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
for(int i :x){
System.out.println(i+" ");
}
}
}