public class Sort {
public static void main(String[] args) {
int i = 1;
Scanner input = new Scanner(System.in);
// prompts the user to get how many numbers need to be sorted
System.out.print("Please enter the number of data points: ");
int data = input.nextInt();
// this creates the new array and data sets how large it is
int [] userArray = new int[data];
// this clarifies that the value is above 0 or else it will not run
if (data < 0) {
System.out.println("The number should be positive. Exiting.");
}
// once a value over 0 is in, the loop will start to get in all user data
else {
System.out.println("Enter the data:");
}
while (i <= data) {
int userInput = input.nextInt();
userArray[i] = userInput;
i++;
}
// this calls the sortArray method to sort the values entered
sortArray(userArray);
// this will print the sorted array
System.out.println(Arrays.toString(userArray));
}
}
Dizinin boyutunu, kullanıcının kaç tane değişkene göre sıralanacağını girdiği değere eşit ayarladım. Bazı nedenlerden dolayı, Java sadece kullanıcı tarafından girilen numara yerine bir numara istiyor. Bu işi yapmanın bir yolu var mı?Neden bir kullanıcı isteğine sahip olabilirim, dizi boyutu olabilir?
tam sorun nedir aşıyorsunuz? * "Java sadece kullanıcı tarafından girilen numara yerine bir numara istiyor" * anlamak zor. – Gendarme
Java hiçbir şey istemiyor. Ve değişken boyutta bir dizi oluşturabilirsiniz. BTW Dizinin oluşturulmaya çalışılmasından sonra boyutun geçersiz olup olmadığını kontrol eden bir nokta yoktur. –
Diziler 0 dizininde başlar ve son öğe 'data.length - 1 ' –