當前位置:編程學習大全網 - 網站源碼 - 編寫壹個JAVA程序讀取0到50(包含兩者)範圍內的任意多個整數,並且計算每項輸入數據

編寫壹個JAVA程序讀取0到50(包含兩者)範圍內的任意多個整數,並且計算每項輸入數據

public?class?Test?{

public?static?void?main(String[]?args)?{

Map<Integer,?Integer>?map?=?new?HashMap<>();

Scanner?scanner?=?new?Scanner(System.in);

System.out.println("請輸入0到50範圍的數(輸入-1結束)");

int?num;

//?輸入?-1?則輸入結束

while?((num?=?scanner.nextInt())?!=?-1)?{

if?(num?<?0?||?num?>?50)?{

System.out.println(num?+?"?不在0到50範圍,請重新輸入");

continue;

}

Integer?count?=?map.get(num);

map.put(num,?count?==?null1?:?count?+?1);

}

Set<Entry<Integer,?Integer>>?entrySet?=?map.entrySet();

for?(Entry<Integer,?Integer>?entry?:?entrySet)?{

System.out.println("數字?"?+?entry.getKey()?+?"?出現了?"?+?entry.getValue()?+?"?次");

}

}

}

  • 上一篇:python有必要學嗎
  • 下一篇:誰是NBA最好的新秀球隊?
  • copyright 2024編程學習大全網