0
我嘗試運行我的工作時出現問題。 我必須通過控制檯輸入一些數字,它應該按照升序排列並保存到數組中。 我認爲hasNext方法和String.nextLine()方法很好,但它似乎仍然處於循環中。 感謝您的幫助使用掃描儀循環hasNext()
import java.util.Scanner;
public class OrdinamentoMaggiore{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Digita dei numeri e te li mettero' in ordine crescente: ");
String Numeri = sc.nextLine();
int dimArray = 0;
while (sc.hasNext(Numeri)){
dimArray++;
System.out.println("Dimensione array: " + dimArray);
}
int min = 0, max = 0, temp;
int[] mioArray = new int[dimArray];
for (int i = 0; i <= mioArray.length; i++){
mioArray[i] = Integer.parseInt(sc.next(Numeri));
}
for (int j = 0; j <= mioArray.length; j++){
for (int h = 1; h <= mioArray.length; h++){
if (mioArray[j] < mioArray[h]){
continue;
}
else {
temp = mioArray[j];
mioArray[j] = mioArray[h];
mioArray[h] = temp;
}
}
}
System.out.println("Min: " + mioArray[0]);
System.out.println("Max: " + mioArray[dimArray]);
sc.close();
}
}
爲什麼不只是輪詢,直到你點擊一個非數字字符,然後處理所有的輸入? – user123
例如: 輸入:12 32 1 2 3 stop while(sc.hasNextInt(Numeri))?? – Alberto32
http://stackoverflow.com/a/9592624/1327636這應該有幫助! – user123