计算输入特定数字的次数的数组 - Java

时间:2022-06-29 21:28:44

I am teaching myself java and have picked up a book with some exercises. One of these exercises requires me to build a console application that reads numbers from the keyboard into an array of type int[]. There can be 50 or fewer entries in the array.

我正在自学java,并拿起一本书进行了一些练习。其中一个练习要求我构建一个控制台应用程序,将键盘中的数字读入int []类型的数组中。阵列中可以有50个或更少的条目。

So let's say someone enters 2,3,4,2,2,3

所以让我们说有人输入2,3,4,2,2,3

The output should be 2 columns with the number on the left, and the number of times on the right (as below for the above numbers)

输出应该是2列,左边是数字,右边是次数(如下面的数字所示)

2 3 3 2 4 1

2 3 3 2 4 1

I am not sure the best way to go about this as I am very new to java.

我不确定最好的方法,因为我对java很新。

What I have so far is:

到目前为止我所拥有的是:

EDIT: With a bit of work from the advice from some people here, this is what I now have.

编辑:从这里的一些人的建议中得到一些工作,这就是我现在拥有的。

package arrayassignment;

import java.util.Scanner;

public class Entry {

    private Scanner scn = new Scanner(System.in);  
    private int[] count = new int[51];
    private  int input = scn.nextInt();

    private void setInput(int input){
        this.input = input;
    }
    private int getInput(){
        return input;
    }         

    public void numberEntry(){
    System.out.println("Please enter numbers between 0 and 50:");
    for (int x=0; x<10; x++)
    if (input == x)
        count[x]++;
    }
  }

I plan on calling these methods from the main method class rather than doing everything from the main method as according to the book, this is the best OO approach.

我打算从main方法类中调用这些方法,而不是根据本书的主方法执行所有操作,这是最好的面向对象方法。

Your input would be greatly appreciated.

非常感谢您的意见。

Thanks.

谢谢。

2 个解决方案

#1


1  

Scanner scn = new Scanner(System.in);
int input;
int[] count = new int[10];
input = scn.nextInt();

for (int x=0; x<10; x++)
    if (input == x)
        count[x]++;

There are several ways to implement this. The above code will check what number the user has keyed in and add 1 accordingly to its respective counter records. This may not be the most effective way, but this is one possible ways you can use.

有几种方法可以实现这一点。上面的代码将检查用户键入的号码,并相应地将其添加1到其各自的计数器记录。这可能不是最有效的方法,但这是您可以使用的一种可能方式。

(As your question did not specify whether the inputs are in string with commas or individual integers, I am assuming your inputs are individual integers.)

(由于您的问题没有指定输入是否带有逗号或单个整数的字符串,我假设您的输入是单个整数。)

#2


0  

for (int i = 0; 1 < digits.length; i++);

The semicolon is going to pose a problem in your for loop. Also why is digits set at 51 if the array is supposed to be set at 50 elements?

分号会在你的for循环中出现问题。另外,如果数组应设置为50个元素,为什么数字设置为51?

The simplest of ways to search for a specific element in your array will probably be with a sequential search algorithm. You're probably going to want to do is create a method that accepts the int array and the value as an argument, searches through each element in the array and returns an int value for the number of times the value was found.

搜索数组中特定元素的最简单方法可能是使用顺序搜索算法。您可能想要做的是创建一个接受int数组和值作为参数的方法,搜索数组中的每个元素并返回一个int值,该值是找到值的次数。

 public static int sequentialSearch(int[] digits, int value){
  int counter = 0;
  for(int index = 0; index< digits.length; index++){
   if(digits[index]  == value){
      counter++;
     }
   }
   return counter;
 }

Then in your main method of your class add this line to see the following example work:

然后在您的类的main方法中添加此行以查看以下示例工作:

 int[] digits = {1,3,5,6,4,3,3,3,10};
 System.out.println(sequentialSearch(digits, 3));

In the example above the console will out put 4 as the value 3 was found four times in the digits array.

在上面的示例中,控制台将输出4,因为在数字数组中找到值4四次。

#1


1  

Scanner scn = new Scanner(System.in);
int input;
int[] count = new int[10];
input = scn.nextInt();

for (int x=0; x<10; x++)
    if (input == x)
        count[x]++;

There are several ways to implement this. The above code will check what number the user has keyed in and add 1 accordingly to its respective counter records. This may not be the most effective way, but this is one possible ways you can use.

有几种方法可以实现这一点。上面的代码将检查用户键入的号码,并相应地将其添加1到其各自的计数器记录。这可能不是最有效的方法,但这是您可以使用的一种可能方式。

(As your question did not specify whether the inputs are in string with commas or individual integers, I am assuming your inputs are individual integers.)

(由于您的问题没有指定输入是否带有逗号或单个整数的字符串,我假设您的输入是单个整数。)

#2


0  

for (int i = 0; 1 < digits.length; i++);

The semicolon is going to pose a problem in your for loop. Also why is digits set at 51 if the array is supposed to be set at 50 elements?

分号会在你的for循环中出现问题。另外,如果数组应设置为50个元素,为什么数字设置为51?

The simplest of ways to search for a specific element in your array will probably be with a sequential search algorithm. You're probably going to want to do is create a method that accepts the int array and the value as an argument, searches through each element in the array and returns an int value for the number of times the value was found.

搜索数组中特定元素的最简单方法可能是使用顺序搜索算法。您可能想要做的是创建一个接受int数组和值作为参数的方法,搜索数组中的每个元素并返回一个int值,该值是找到值的次数。

 public static int sequentialSearch(int[] digits, int value){
  int counter = 0;
  for(int index = 0; index< digits.length; index++){
   if(digits[index]  == value){
      counter++;
     }
   }
   return counter;
 }

Then in your main method of your class add this line to see the following example work:

然后在您的类的main方法中添加此行以查看以下示例工作:

 int[] digits = {1,3,5,6,4,3,3,3,10};
 System.out.println(sequentialSearch(digits, 3));

In the example above the console will out put 4 as the value 3 was found four times in the digits array.

在上面的示例中,控制台将输出4,因为在数字数组中找到值4四次。