[英]How to read an arbitrary number of ints that are in the range 0-50 and count the occurences of each entered
我這樣做是作為數組的練習題,我很難弄清楚如何讓它實際計算輸入。
完整的問題如下:
“設計並實現一個應用程序,它可以讀取 0 到 50(含)范圍內的任意數量的整數,並計算每個輸入的出現次數。處理完所有輸入后,打印所有值(以及出現次數) 輸入了一次或多次。”
也許這一章沒有解釋太多,或者我只是沒有掌握這個概念,但我想不出任何方法來真正開始這一點。 我在谷歌上環顧四周,發布的一些解決方案仍然沒有意義(當我測試他們做了什么時,有些甚至不起作用!)並希望我能得到一些關於該做什么的指示這里。
到目前為止我的代碼:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] array = new int[51];
Scanner input = new Scanner(System.in);
System.out.println("Please enter a number");
while (input.hasNext()){
int x = input.nextInt();
if (x>=0 && x<=50) //
array[x]++; //should make array size equal to x + 1?
}
}
抱歉,我沒有注意到hasNext()這個用於文件的方法。 因此,您知道是否還有令牌。 使用另一種方法破壞循環循環,檢查是否有用戶輸入的內容,這表示他們已經輸入了數據。 例如,您可以使用特定的號碼。
您走在正確的軌道上。 從用戶讀取完后,運行for循環以檢查array元素的值是否大於1,然后進行打印:
for(int i = 0; i < array.length; i++) {
if(array[i] > 1)
System.out.println("The number: " + i + " entered " + array[i] + "times");
}
就如此容易!
第1點:您從未想過停止while (input.hasNext()){
循環的方法。 因此,它會一直持續下去,只是嘗試讀取整數。
按照Scanner#hasNext()
與System.in
一起使用的方式進行操作,它將始終返回true或它將停止執行直到您輸入內容,然后它將再次返回true。
解決此問題的一種方法是將“ input.hasNext()”更改為“ input.hasNext Int ()”,因此,一旦輸入的內容不是整數,循環就會停止,您可以對數組進行任何輸出。
考慮:
import java.util.Scanner;
import java.util.ArrayList;
import java.util.InputMismatchException;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Integer> array = new ArrayList<Integer>();
int count = 0;
while(true)
{
System.out.println("Please enter a number (enter a non-integer to end)");
try{
int x = input.nextInt();
array.add(x);
if (x>=0 && x<=50) {
count++;
}
}
catch (InputMismatchException ex) {
break;
}
}
System.out.println();
System.out.format("The numbers you entered were: %s\n", array);
System.out.format("The count of in-range numbers was: %d\n", count);
}
}
輸出:
Please enter a number (enter a non-integer to end) 1 Please enter a number (enter a non-integer to end) 2 Please enter a number (enter a non-integer to end) 3 Please enter a number (enter a non-integer to end) -1 Please enter a number (enter a non-integer to end) 100 Please enter a number (enter a non-integer to end) e The numbers you entered were: [1, 2, 3, -1, 100] The count of in-range numbers was: 3
除了jedwards的回復,我想您還需要所有接受的輸入的單獨出現。 如果是這種情況,請嘗試此
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class OccuranceCounter {
public static void main(String[] args) {
Map<Integer, Integer> counter = new HashMap<Integer, Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Please enter a number or -1 to quit");
while (input.hasNext()) {
int x = input.nextInt();
if (x >= 0 && x <= 50){
Integer val = counter.get(x);
if(val == null){
counter.put(x, 1);
} else {
counter.put(x, ++val);
}
} else if(x == -1){
break;
}
}
for(Integer key : counter.keySet()){
System.out.println(key + " occured " + counter.get(key) + " times");
}
}
}
我知道它已經回答了,但是您應該正在學習數組的知識,並且可以接受的答案是一個哈希表,因此我將為您編寫一個以數組為中心的版本。 您已接近備案。
int[] result = new int[51];
//this should run until you enter something that's not an int.
//I'm leaving out print statements, and the check for 0<=x<=50
while(input.hasNextInt())
{
result[input.nextInt()]++;
}
for(int i = 0; i<=50;i++)
{
if(results[i]>0)
{
//print something like (i+": " + results[i] + "\n")
}
}
讓我知道您是否需要幫助以了解其中的任何內容。 如果您將其放入編譯器中並自己嘗試使用,直到了解發生了什么,您可能會更喜歡它。
public class OccuranceCounter {
public static void main(String[] args) {
Map<Integer, Integer> counter = new HashMap<Integer, Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Please enter a number or -1 to quit");
while (input.hasNext()) {
int x = input.nextInt();
if (x >= 0 && x <= 50){
Integer val = counter.get(x);
if(val == null){
counter.put(x, 1);
} else {
counter.put(x, ++val);
}
} else if(x == -1){
break;
}
}
for(Integer key : counter.keySet()){
System.out.println(key + " occured " + counter.get(key) + " times");
}
}
}
嘗試一下,效果很好(作者JovanDaGreat)。
導入 java.util.Scanner;
公共類 Chap7ProjSP2022
{
public static void main(String[] args)
{
// declaring variables
Scanner scan = new Scanner (System.in);
int [] store = new int [51];
int input = 0;
System.out.println("Enter arbitrary number of integers that are in the range 0 to 50");
System.out.println("Enter integer not in the range 0 to 50 to end loop and process\nEnter integer:");
input = scan.nextInt();
// storing inputed numbers
while (input >= 0 && input <= 50)
{
store[input] += 1;
System.out.println("Enter integer:");
input = scan.nextInt();
}
System.out.println();
// printing numbers
for (int i = 0; i <= 50; i++)
{
if (store[i] > 0)
System.out.println(i + ": " + store[i]);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.