Java 从用户输入读取堆栈的整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22278160/
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
Reading integers for a stack from user input
提问by Robert
I am trying to create a program that takes user input from a scanner and pushes it onto a stack and then pops off each element and prints it out. Here is my code thus far:
我正在尝试创建一个程序,该程序从扫描仪获取用户输入并将其推送到堆栈上,然后弹出每个元素并将其打印出来。到目前为止,这是我的代码:
import java.util.*;
public class NumberReverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter numbers: ");
int number = sc.nextInt();
Stack<Integer> stack = new Stack<Integer>();
stack.push(number);
while (!(stack.isEmpty())) {
System.out.println(stack.pop());
}
}
}
采纳答案by Josh Engelsma
Try something more like this...
尝试更像这样的东西......
Before you are only reading in one number one time, which seems like you want to be used for the size of your array. After this, you continually push the same number onto the stack because you never prompt the user to enter in any other number.
在您一次只阅读一个数字之前,这似乎是您想要用于数组的大小。在此之后,您不断地将相同的数字压入堆栈,因为您从不提示用户输入任何其他数字。
The code below goes one step further than your code in that once you have learned the number from the User and set your array size, you ask for a new number over and over again to populate your array until your array is filled. This can be accomplished by prompting the user to enter a value over and over again in the for loop.
下面的代码比您的代码更进一步,一旦您从用户那里学习了数字并设置了数组大小,您就会一遍又一遍地要求一个新数字来填充您的数组,直到您的数组被填满。这可以通过提示用户在 for 循环中一遍又一遍地输入值来实现。
I am not really sure why exactly you need an array to do what you want to do though, consider removing the array and just using the first number entered as your bounds for the for loop rather than the array size. The array data structure is really just a waste of space as the only time you ever use it is to use its length as a stopping point in the for loop
我不确定为什么你需要一个数组来做你想做的事情,考虑删除数组并只使用输入的第一个数字作为 for 循环的边界而不是数组大小。数组数据结构实际上只是浪费空间,因为您使用它的唯一时间是将其长度用作 for 循环中的停止点
public class NumberReverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter stack size: ");
int number = sc.nextInt();
int[] array = new int[number];
Stack<Integer> stack = new Stack<Integer>();
for(int i = 0; i < array.length; i++){
System.out.println("Enter your number: ");
int value = sc.nextInt();
stack.push(value);
}
while (!(stack.isEmpty())) {
System.out.println(stack.pop());
}
}
}
Based on the comment below consider trying something like the code below. You are only printing out one number for the same reasons that I have listed above...
根据下面的评论,考虑尝试类似下面的代码。出于我上面列出的相同原因,您只打印了一个数字...
import java.util.*;
public class NumberReverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter how many numbers you want to place on the Stack: ");
int stackSize = sc.nextInt();
Stack<Integer> stack = new Stack<Integer>();
for(int i = 0; i < stackSize; i++){
System.out.println("Enter numbers: ");
int number = sc.nextInt();
stack.push(number);
}
while (!(stack.isEmpty())) {
System.out.println(stack.pop());
}
}
}
回答by Florent Bayle
You have to iterate over all the numbers entered by the user, and push them on the stack:
您必须遍历用户输入的所有数字,并将它们压入堆栈:
public static void main(String[] args) {
final Scanner sc = new Scanner(System.in);
System.out.print("Enter numbers (finish with something else): ");
final Stack<Integer> stack = new Stack<Integer>();
while (sc.hasNextInt())
stack.push(sc.nextInt());
while (!(stack.isEmpty()))
System.out.println(stack.pop());
}