static method and final variable (Constance):
import java.util.Scanner;
public class ArrayTest
{
public static void main(String[] args)
{
int[] A = new int[5];
int i;
Scanner input = new Scanner(System.in);
System.out.println("Enter 5 numbers");
for(i=0; i<5; i++)
A[i] = input.nextInt();
sort(A); for(i=0; i<5; i++)
System.out.print(A[i] + " ");
System.out.println();
int[] B = {13, 24, 18, 11, 43, 32, 67, 55, 86, 12};
sort(B);
for(i=0; i<B.length; i++)
System.out.print(B[i] + " ");
System.out.println();
}
public static void sort(int[] x)
{
int i, j, temp;
for(i=0;i < x.length-1; i++)
{
for(j= i+1; j < x.length; j++)
{
if(x[i]>x[j])
{
temp = x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
}
}
|
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
final int size = 4;
int[] scores = new int[size];
int i;
Scanner input = new Scanner(System.in);
System.out.println("Enter " + size + " scores");
for(i=0; i<scores.length; i++)
scores[i] = input.nextInt();
ArrayTest.sort(scores); for(i=0; i<scores.length; i++)
System.out.print(scores[i] + " ");
System.out.println();
}
}
|