java 要求用户在数组中输入项目数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25795181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Asking the user to enter amount of items to an array
提问by Naa-ilah Daniels
I'm really new to Java and I am having some difficulty with the basics of arrays. Some help would be greatly appreciated :).
我对 Java 真的很陌生,而且我在数组的基础知识方面遇到了一些困难。一些帮助将不胜感激:)。
I created a program that asks the user to enter values into an array. The program then tells the user how many items are in the array, the even numbers that are in the array, the odd numbers in the array and the average of all the numbers in the array.
我创建了一个程序,要求用户将值输入到数组中。然后程序告诉用户数组中有多少项,数组中有偶数,数组中有奇数,以及数组中所有数字的平均值。
My problem is this: At present my program has a set number of values in the array which is 5 items. I want to change this so that the user can determine the amount of items they want in the array, and ask the user to continually add values to the array until they enter 'Q' to quit entering values so that the program can continue.
我的问题是这样的:目前我的程序在数组中有一组值为 5 的值。我想改变这一点,以便用户可以确定他们想要在数组中的项目数量,并要求用户不断向数组添加值,直到他们输入“Q”以退出输入值,以便程序可以继续。
I do not want to modify my code, just the part where the user determines the amount of items in the array.
我不想修改我的代码,只是用户确定数组中项目数量的部分。
This is my code:
这是我的代码:
//main class
public class Even_number_array {
public static void main(String[] args) {
array_class obj=new array_class();
obj.get_numbers();
obj.set_arraylist();
obj.set_numbers();
obj.get_average_of_array();
}
}
//another class
import java.util.Scanner;
public class array_class {
private int[] arr=new int[5]; //here is where I would like to modify my code so
//that the user can determine amount of items in array
Scanner keyboard=new Scanner(System.in);
public int[] get_numbers() //asks user to add items to array
{
System.out.println("Please enter numbers for array :");
for (int i=0; i<this.arr.length; i++)
{
this.arr[i] = keyboard.nextInt();
}
System.out.println("There are "+((arr.length))+ " numbers in array");
return this.arr;
}
public void set_numbers() //Tells user what even & odd numbers are in array
{
System.out.println();
System.out.println("These even numbers were found in the array:");
for (int i=0; i<arr.length;i++)
{
if (arr[i]%2==0)
{
System.out.print(arr[i]+" ");
System.out.println();
}
}
System.out.println("These odd numbers were found in the array:");
for (int i=0; i<arr.length;i++)
{
if ((arr[i]%2)!=0)
{
System.out.print(arr[i]+" ");
}
}
}
public void set_arraylist() //Dispalys all items in array
{
System.out.println("These are the numbers currently in your array: ");
for (int i=0; i<arr.length; i++ )
{
System.out.print(this.arr[i]+ " ");
}
}
public double get_average_of_array() //Gets average of all items in array
{
double sum=0;
double average=0;
System.out.println();
System.out.println("the average is: ");
for (int i=0; i<this.arr.length; i++)
{
sum=sum+arr[i];
average =sum/arr.length;
}
System.out.println(average);
return average;
}
}
Any help would be great!
任何帮助都会很棒!
回答by Jean Logeart
Try:
尝试:
public int[] get_numbers() {
System.out.println("Please enter numbers for array :");
int size = keyboard.nextInt();
int[] values = new int[size]; // create a new array with the given size
for (int i = 0; i < size; i++) {
values[i] = keyboard.nextInt();
}
System.out.println("There are "+((values.length))+ " numbers in array");
this.arr = values;
return this.arr;
}
回答by ZekeDroid
ArrayList
is the object you want. It has functions that allow you to append values at the end. It's a dynamic array in the sense that you can mutate it at will.
ArrayList
是你想要的对象。它具有允许您在末尾附加值的功能。它是一个动态数组,您可以随意改变它。
回答by DoubleDouble
Using an ArrayListinstead of arrays would be the easiest way to accomplish exactly what you are trying to do.
使用ArrayList而不是数组将是完成您想要做的事情的最简单方法。
Another option is to ask how many numbers the person plans on entering from the start.
另一种选择是询问此人从一开始就计划输入多少个数字。
If, for some weird reason, you are only trying to use arrays without asking the user how many numbers they want to enter... a third option would be to create a new array (being one size larger than the previous array)each time the user enters another number.
如果由于某种奇怪的原因,你只是想用数组不问他们要多少个号码输入用户... ...第三个选择是创建一个新的数组(即一个尺寸比以前更大的阵列),每次用户输入另一个号码。
Some guidance for the last piece...
最后一块的一些指导......
public int[] get_numbers() //asks user to add items to array
{
System.out.println("Please enter numbers for array :");
//Instead of this for loop, you will want a while loop. Similar to "while (userHasntEnteredQ)"
for (int i=0; i<this.arr.length; i++)
{
//You will want to create a new array here.
// int[] newArray = new int[this.arr.length + 1];
// Then copy the values of this.arr to newArray
// Then place the keyboard.nextInt() into the last spot of newArray
// Then set this.arr to newArray
}
System.out.println("There are "+((arr.length))+ " numbers in array");
return this.arr;
}
回答by Santiago Benoit
If you want the user to determine how many items are in the array, add this in to your code:
如果您希望用户确定数组中有多少项,请将其添加到您的代码中:
System.out.println("Enter the amount of items in the array:");
while (keyboard.hasNextInt()) {
int i = keyboard.nextInt();
}
private int[] arr = new int[i];
However, you won't be able to add more items to the array than the user defined without getting an ArrayIndexOutOfBoundsException
, so there is a better way to do this that uses something called an ArrayList
. With an ArrayList
, you don't have to worry about setting the size of the array before using it; it just expands automatically when new items are added unless you explicitly set a size for it. An ArrayList
can be of any object, but to make it only for integers, declare it like this:
但是,如果没有获得 ,您将无法向数组添加比用户定义的更多的项目ArrayIndexOutOfBoundsException
,因此有一种更好的方法来执行此操作,即使用称为 an 的东西ArrayList
。使用ArrayList
,您不必担心在使用之前设置数组的大小;它只会在添加新项目时自动扩展,除非您明确为其设置大小。AnArrayList
可以是任何对象,但要使其仅用于整数,请像这样声明:
ArrayList<Integer> arr = new ArrayList<>();
To add items to the ArrayList
:
向 中添加项目ArrayList
:
arr.add(item);
To read a value by its index:
要按索引读取值:
arr.get(index);
To get the index of an item:
要获取项目的索引:
int index = arr.indexOf(item);
There is much more that you can do with an ArrayList
. Read about it here.
您可以使用ArrayList
. 在这里阅读。
回答by Emrage
Sorry i don't really get what you are asking but maybe you mean this
对不起,我真的不明白你在问什么,但也许你是这个意思
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
int[] array = new int[scanner.nextInt()];
System.out.println(array.length);
Sorry i'm pretty bad in java only been learning it for 2 months now
抱歉,我在 Java 方面很糟糕,现在才学习了 2 个月