如何读取用户输入的数量,并将其存入一个指定大小的数组中?

huangapple go评论68阅读模式
英文:

How can I read the amount of input a user has entered into an array with a set size?

问题

目标是创建一个大小为100的数组让用户输入一堆正数当他们输入-1时退出或者填满100个位置)。之后打印输入的数量平均值和最高值我最初用一个较小大小的数组来测试我的代码以获得平均值现在意识到数组的长度array.length不是我要寻找的内容因为我将其改为100并且没有填满100个位置我的问题是我如何计算已输入数组的输入数量而不仅仅是数组的总长度

import java.util.*;
class Array{
    public static void main(String [] args){
       Scanner scan = new Scanner(System.in);
       int[] array = new int[100];

       System.out.println("Enter your number: ");
       for(int i = 0; i < 100; i++){
          int entry = scan.nextInt();
          if(entry == -1){
             System.out.println("Goodbye.");
             i--;
             break;
          }else if(entry < -1){
             System.out.println("Invalid.");
             i--;
          }else{
             array[i] = entry;
          }
       } 
       //prints amount of elements inside array
       //System.out.println("There are " + countArray(array) + " numbers in this array.");

       //prints average
       System.out.println("The average is: " + findAverage(array));   

       //prints highest val
       System.out.println("The highest value is: " + findHigh(array));
 
    }
    //public static int countArray(int[] array)
    //need help here and then fixing the issue in the average method

     public static double findAverage(int [] array){
         double sum = 0;
         for(int i = 0; i < array.length; i++){
            sum += array[i];
         }
         double average = (sum/array.length);
         return average;
      }

      public static int findHigh(int[]array){
         int max = array[0];
         for(int i = 1; i < array.length; i++){
            if (array[i] > max){
               max = array[i];
            }
         } 
         return max;
      }  
}
英文:

The objective is to make an array of size 100, have a user enter a bunch of positive numbers, and quit whenever they enter -1 (or fill up 100 spots I guess). After that, print out the number of inputs read in, the average value, and highest value. I was testing my code with a smaller sized array originally to get the average and now realize that array.length isn't what I'm looking for as I switch it to 100 and don't fill up 100 slots. My question is, how can I count the amount of input thats been entered into an array and not just the total length of an array?

import java.util.*;
class Array{
public static void main(String [] args){
Scanner scan = new Scanner(System.in);
int[] array = new int[100];
System.out.println(&quot;Enter your number: &quot;);
for(int i = 0; i &lt; 100; i++){
int entry = scan.nextInt();
if(entry == -1){
System.out.println(&quot;Goodbye.&quot;);
i--;
break;
}else if(entry &lt; -1){
System.out.println(&quot;Invalid.&quot;);
i--;
}else{
array[i] = entry;
}
} 
//prints amount of elements inside array
//System.out.println(&quot;There are &quot; + countArray(array) + &quot; numbers in this array.&quot;);
//prints average
System.out.println(&quot;The average is: &quot; + findAverage(array));   
//prints highest val
System.out.println(&quot;The highest value is: &quot; + findHigh(array));
}
//public static int countArray(int[] array)
//need help here and then fixing the issue in the average method
public static double findAverage(int [] array){
double sum = 0;
for(int i = 0; i &lt; array.length; i++){
sum += array[i];
}
double average = (sum/array.length);
return average;
}
public static int findHigh(int[]array){
int max = array[0];
for(int i = 1; i &lt; array.length; i++){
if (array[i] &gt; max){
max = array[i];
}
} 
return max;
}  
}

答案1

得分: 2

因为所有未指定元素的索引都会被设置为0,所以在这方面没有可用的方法。您可以选择的方法是,每次向数组添加元素时,都使用一个变量将其递增。在您的情况下,您可以使用变量 i 来实现此目的(在 for 循环头之外声明它,以便稍后可以访问它)。

英文:

There is no method you can use for that because all indexes where you don't specify an element yourself get set to 0. What you can do instead is just have a variable which you increase by one each time you add an element to your array. In your case you can use i for this (declare it outside the for header so you can access it later on).

答案2

得分: 1

你可以保留一个变量来计数输入的数量。

        int inputCount = 0;
		for (int i = 0; i < 100; i++) {
			int entry = scan.nextInt();
			inputCount++;
			if (entry == -1) {
				System.out.println("Goodbye.");
				i--;
				break;
			} else if (entry < -1) {
				System.out.println("Invalid.");
				i--;
			} else {
				array[i] = entry;
			}
		}
英文:

You can keep a variable to count the number of inputs.

        int inputCount = 0;
for (int i = 0; i &lt; 100; i++) {
int entry = scan.nextInt();
inputCount++;
if (entry == -1) {
System.out.println(&quot;Goodbye.&quot;);
i--;
break;
} else if (entry &lt; -1) {
System.out.println(&quot;Invalid.&quot;);
i--;
} else {
array[i] = entry;
}
}

答案3

得分: 0

通常,要将一些值输入到大小适当的数组中,首先需要提示输入大小。

Scanner input = new Scanner(System.in);
System.out.print("请输入值的数量:");
int size = input.nextInt();
int[] values = new int[size];
System.out.println("开始输入 " + size + " 个值");
for (int i = 0; i < size; i++) {
    values[i] = input.nextInt();
}
// 现在可以对数组执行某些操作。

这允许输入负值,因为它不需要一个特定的值来表示输入结束。这只是一个示例,但是这种技术可以根据您的需求进行适当的调整,您仍然可以忽略负数。

英文:

Typically, to enter some number of values into a properly sized array, one first prompts for the size.

Scanner input = new Scanner(System.in);
System.out.print(&quot;Please enter number of values: &quot;);
int size = input.nextInt();
int[] values = new int[size];
System.out.println(&quot;Start entering &quot; + size + &quot; values);
for (int i = 0; i &lt; size; i++) {
values[i] = input.nextInt();
}
// now do something with the array.

This allows negative values to be entered too as it does not require a value to signal end of input. Just an example, but this technique can be easily adapted to your requirements as you can still ignore negative numbers if you want.

huangapple
  • 本文由 发表于 2020年9月3日 07:51:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63714932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定