如何修改我的程序,使用户不需要指定输入长度?
How to modify my program so that the user does not need to specify the input length?
我制作了一个程序,要求:
数组长度:
和数组的元素:
然后程序将元素相乘。
我的问题是,如何提示用户只输入数组元素而不询问长度,然后在我输入 -1 时停止?我需要使用数组列表吗?到目前为止,这是我的代码。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Length of the array: "); //Prompt User for length
int number = Integer.parseInt(input.next());
System.out.print("The elements of your array: "); //Prompt User for elements
int[] myArray = new int[number];
for(int i = 0; i < number; i++){
myArray[i] = Integer.parseInt(input.next());
}
System.out.printf("The multiplication is %d.", multiplication(myArray, myArray.length - 1) ); //End Statement
}
static int multiplication(int[] array, int startIndex) {
if (startIndex == 0)
return(array[startIndex]);
else
return (array[startIndex] * multiplication(array, startIndex - 1));
}
}
如有任何帮助,我们将不胜感激。
Yes we can use ArrayList and Array too.Here is an example with ArrayList.
List<Integer> list = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.print("The elements of your array: ");
boolean flag = true;
while (flag) {
int val = input.nextInt();
if (val > 0) {
list.add(val);
} else {
flag = false;
System.out.println(list);
}
}
首先是代码,然后是解释(出现在下面的代码之后)。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MultiplyRecurse {
private static int multiplication(int[] array, int startIndex) {
if (startIndex == 0)
return(array[startIndex]);
else
return (array[startIndex] * multiplication(array, startIndex - 1));
}
/**
* @param args
*/
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner stdin = new Scanner(System.in);
String quit = "Q";
List<Integer> list = new ArrayList<>();
String line = null;
while (!quit.equalsIgnoreCase(line)) {
try {
System.out.print("Enter a number (or 'Q' to quit): ");
line = stdin.nextLine();
if (!line.equalsIgnoreCase(quit)) {
list.add(Integer.valueOf(line));
}
}
catch (NumberFormatException xNumberFormat) {
System.out.println("Not a number. Please try again.");
}
}
int[] myArray = list.stream().mapToInt(i->i).toArray();
System.out.printf("The multiplication is %d.%n",
multiplication(myArray, myArray.length - 1));
}
}
在java中int
是原始的。如果您查看 class ArrayList
you will see that it contains a member named elementData
which is an array of Object
. Hence you can't create an ArrayList
object that contains an array of int
, so you need to use Integer
的代码。
请注意,如果用户输入的数字大于 MAX_VALUE
,代码 Integer.valueOf(line)
将抛出 NumberFormatException
。
您的 multiplication()
方法需要一个 int[]
参数,因此您需要将您的 ArrayList<Integer>
转换为 int[]
。实现这一点的代码行取自这个 stackoverflow 问题:
How to convert List to int[] in Java?
编辑
由于 OP
List<Integer> numbers = new ArrayList<>();
System.out.print("Enter numbers: ");
while (input.hasNextInt()) {
int number = input.nextInt();
if (number == -1) {
break;
}
numbers.add(Integer.valueOf(number));
}
int[] myArray = numbers.stream().mapToInt(i->i).toArray();
System.out.printf("The multiplication is %d.%n",
multiplication(myArray, myArray.length - 1));
样本运行:
Enter numbers: 8 4 -1 <ENTER>
The multiplication is 32.
键入 -1
后,按 (在键盘上)。
我制作了一个程序,要求:
数组长度:
和数组的元素:
然后程序将元素相乘。
我的问题是,如何提示用户只输入数组元素而不询问长度,然后在我输入 -1 时停止?我需要使用数组列表吗?到目前为止,这是我的代码。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Length of the array: "); //Prompt User for length
int number = Integer.parseInt(input.next());
System.out.print("The elements of your array: "); //Prompt User for elements
int[] myArray = new int[number];
for(int i = 0; i < number; i++){
myArray[i] = Integer.parseInt(input.next());
}
System.out.printf("The multiplication is %d.", multiplication(myArray, myArray.length - 1) ); //End Statement
}
static int multiplication(int[] array, int startIndex) {
if (startIndex == 0)
return(array[startIndex]);
else
return (array[startIndex] * multiplication(array, startIndex - 1));
}
}
如有任何帮助,我们将不胜感激。
Yes we can use ArrayList and Array too.Here is an example with ArrayList.
List<Integer> list = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.print("The elements of your array: ");
boolean flag = true;
while (flag) {
int val = input.nextInt();
if (val > 0) {
list.add(val);
} else {
flag = false;
System.out.println(list);
}
}
首先是代码,然后是解释(出现在下面的代码之后)。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MultiplyRecurse {
private static int multiplication(int[] array, int startIndex) {
if (startIndex == 0)
return(array[startIndex]);
else
return (array[startIndex] * multiplication(array, startIndex - 1));
}
/**
* @param args
*/
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner stdin = new Scanner(System.in);
String quit = "Q";
List<Integer> list = new ArrayList<>();
String line = null;
while (!quit.equalsIgnoreCase(line)) {
try {
System.out.print("Enter a number (or 'Q' to quit): ");
line = stdin.nextLine();
if (!line.equalsIgnoreCase(quit)) {
list.add(Integer.valueOf(line));
}
}
catch (NumberFormatException xNumberFormat) {
System.out.println("Not a number. Please try again.");
}
}
int[] myArray = list.stream().mapToInt(i->i).toArray();
System.out.printf("The multiplication is %d.%n",
multiplication(myArray, myArray.length - 1));
}
}
在java中int
是原始的。如果您查看 class ArrayList
you will see that it contains a member named elementData
which is an array of Object
. Hence you can't create an ArrayList
object that contains an array of int
, so you need to use Integer
的代码。
请注意,如果用户输入的数字大于 MAX_VALUE
,代码 Integer.valueOf(line)
将抛出 NumberFormatException
。
您的 multiplication()
方法需要一个 int[]
参数,因此您需要将您的 ArrayList<Integer>
转换为 int[]
。实现这一点的代码行取自这个 stackoverflow 问题:
How to convert List to int[] in Java?
编辑
由于 OP
List<Integer> numbers = new ArrayList<>();
System.out.print("Enter numbers: ");
while (input.hasNextInt()) {
int number = input.nextInt();
if (number == -1) {
break;
}
numbers.add(Integer.valueOf(number));
}
int[] myArray = numbers.stream().mapToInt(i->i).toArray();
System.out.printf("The multiplication is %d.%n",
multiplication(myArray, myArray.length - 1));
样本运行:
Enter numbers: 8 4 -1 <ENTER>
The multiplication is 32.
键入 -1
后,按