我想用java编写一个程序,将所有行输入到标准输入,并以相反的顺序将它们写入标准输出。 这是可能的代码,但它有一个错误,我不明白问题出在哪里 (在这个程序中,我首先询问行数,然后将其保存在“n”中。) 有什么帮助吗? 提前致谢
package getLine;
import java.util.Scanner;
public class S {
public static void main(String[] args)
{
Scanner s= new Scanner(System.in);
System.out.println("how many lines do you want to enter");
int n= s.nextInt();
String [] str;
str= new String[n];
for(int i=0;i<n;i++)
str[i]=s.nextLine();
for(int i=n;i>=0;i--)
System.out.println(str[i]);
}
}
最佳答案
为什么不使用 Stack<String>
缓冲线路?然后简单地弹出每一行并输出它。
关于java - 以相反的顺序将几行写入标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19048051/