用户输入,一次将一个值填充到数组中JAVA [英] User input to populate an array one value at a time JAVA
问题描述
我试图弄清楚如何从用户那里获取输入并将其存储到数组中。我不能使用 arrayList
(简单的方法,大声笑),而是使用标准的 array [5]
。我已经在这里和Google上进行过搜索,由于大量的问题和答复,我尚未找到好的答案。使用扫描仪获取输入不是问题。将输入存储在数组中不是问题。我遇到的麻烦是我需要一次存储一个输入。
I am trying to figure out how to take input from a user and store it into an array. I cannot use an arrayList
(the easy way lol) but rather a standard array[5]
. I have looked on here and on google and because of the sheer amount of questions and responses I have yet to find a good answer. Using scanner to get input is not a problem. Storing the input in an array is not the problem. What i am having trouble with is that I need to store one input at a time.
当前我正在使用for循环来收集信息,但它想收集整个信息
Currently I was using a for loop to gather information but it wants to gather the entire array at once.
for (int i=0;i<5;i++)
array[i] = input.nextInt();
for (int i=0;i<array.length;i++)
System.out.println(array[i]+" ");
我一直在搞弄它,但是如果我删除了第一个for循环,我不确定放在array []括号中。 BlueJ只是说 array []不是语句
I have been messing around with it, but if i remove the first for loop, im not sure what to put in the array[] brackets. BlueJ just says that "array[] is not a statement"
我如何一次只接受一个值,让用户确定是否要执行下一个?
How can I accept just one value at a time and let the user determine if they want to do the next?
这是一项家庭作业,但该作业是关于使用字符串命令创建控制台的,这是我需要了解以完成其余部分的概念
This is for a homework assignment, but the homework assignment is about creating a console with commands of strings and this is a concept that i need to understand to complete the rest of the project which is working fine.
推荐答案
将此用作参考实现。有关如何设计简单控制台程序的一般说明。我目前正在使用JDK 6。如果您使用的是JDK 7,则可以在字符串中使用 switch case
而不是 if-else
。
Use this as a reference implementation. General pointers on how a simple console program can be designed. I'm using JDK 6 at the moment. If you're on JDK 7 you can use switch case
with strings instead of the if-else
.
public class Stack {
int[] stack; // holds our list
int MAX_SIZE, size; /* size helps us print the current list avoiding to
print zer0es & the ints deleted from the list */
public Stack(int i) {
MAX_SIZE = i; // max size makes the length configurable
stack = new int[MAX_SIZE]; // intialize the list with zer0es
}
public static void main(String[] args) throws IOException {
new Stack(2).run(); // list of max length 2
}
private void run() {
Scanner scanner = new Scanner(System.in);
// enter an infinite loop
while (true) {
showMenu(); // show the menu/prompt after every operation
// scan user response; expect a 2nd parameter after a space " "
String[] resp = scanner.nextLine().split(" "); // like "add <n>"
// split the response so that resp[0] = add|list|delete|exit etc.
System.out.println(); // and resp[1] = <n> if provided
// process "add <n>"; check that "<n>" is provided
if ("add".equals(resp[0]) && resp.length == 2) {
if (size >= MAX_SIZE) { // if the list is full
System.out.print("Sorry, the list is full! ");
printList(); // print the list
continue; // skip the rest and show menu again
}
// throws exception if not an int; handle it
// if the list is NOT full; save resp[1] = <n>
// as int at "stack[size]" and do "size = size + 1"
stack[size++] = Integer.parseInt(resp[1]);
printList(); // print the list
// process "list"
} else if ("list".equals(resp[0])) {
printList(); // print the list
// process "delete"
} else if ("delete".equals(resp[0])) {
if (size == 0) { // if the list is empty
System.out.println("List is already empty!\n");
continue; // skip the rest and show menu again
}
// if the list is NOT empty just reduce the
size--; // size by 1 to delete the last element
printList(); // print the list
// process "exit"
} else if ("exit".equals(resp[0])) {
break; // from the loop; program ends
// if user types anything else
} else {
System.out.println("Invalid command!\n");
}
}
}
private void printList() {
System.out.print("List: {"); // print list prefix
// print only if any ints entered by user
if (size > 0) { // are available i.e. size > 0
int i = 0;
for (; i < size - 1; i++) {
System.out.print(stack[i] + ",");
}
System.out.print(stack[i]);
}
System.out.println("}\n"); // print list suffix
}
private void showMenu() {
System.out.println("Enter one of the following commands:");
// Check String#format() docs for how "%" format specifiers work
System.out.printf(" %-8s: %s\n", "add <n>", "to add n to the list");
System.out.printf(" %-8s: %s\n", "delete", "to delete the last number");
System.out.printf(" %-8s: %s\n", "list", "to list all the numbers");
System.out.printf(" %-8s: %s\n", "exit", "to terminate the program");
System.out.print("$ "); // acts as command prompt
}
}
示例运行:
Enter one of the following commands:
add <n> : to add n to the list
delete : to delete the last number
list : to list all the numbers
exit : to terminate the program
$ list
List: {}
Enter one of the following commands:
add <n> : to add n to the list
delete : to delete the last number
list : to list all the numbers
exit : to terminate the program
$ add 1
List: {1}
Enter one of the following commands:
add <n> : to add n to the list
delete : to delete the last number
list : to list all the numbers
exit : to terminate the program
$ add 2
List: {1,2}
Enter one of the following commands:
add <n> : to add n to the list
delete : to delete the last number
list : to list all the numbers
exit : to terminate the program
$ add 3
Sorry, the list is full! List: {1,2}
Enter one of the following commands:
add <n> : to add n to the list
delete : to delete the last number
list : to list all the numbers
exit : to terminate the program
$ delete
List: {1}
Enter one of the following commands:
add <n> : to add n to the list
delete : to delete the last number
list : to list all the numbers
exit : to terminate the program
$ delete
List: {}
Enter one of the following commands:
add <n> : to add n to the list
delete : to delete the last number
list : to list all the numbers
exit : to terminate the program
$ delete
List is already empty!
Enter one of the following commands:
add <n> : to add n to the list
delete : to delete the last number
list : to list all the numbers
exit : to terminate the program
$ exit
(说实话:我很无聊。所以,我写了它,并认为不妨将其发布)
这篇关于用户输入,一次将一个值填充到数组中JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!