我试图围绕我在编程集中遇到的问题进行打包。反向java中的打印行
我们应该编写从文件读取并打印出来的代码。我明白了,我可以做到。
他希望我们做的是将其打印出来。
读取文件:
abc
123
987
他想:
987
123
abc
的代码,因为它是,如下所示:
{
FileReader n=new FileReader("F:\\Java\\Set 8\\output1.txt");
Scanner in=new Scanner(n);
int l;
while (in.hasNext())
{
l=in.nextInt();
System.out.println(l);
}
in.close();
}
}
是的,我用java。 IO *。和扫描仪。
这样做最简单的方法是什么?
编辑编辑编辑
这里的改进的代码,在那里我试图把它变成一个数组。
数组中的数据未打印出来。
public static void main(String[] args) throws IOException
{
int[]Num=new int[20];
Scanner in=new Scanner(new FileReader("F:\\Java\\Set 8\\output1.txt"));
int k;
for (k=0;k<20;k++)
{
Num[k]=in.nextInt();
}
//in.close();
for (k=20;k<20;k--)
{
System.out.print(+Num[k]);
}
//in.close();
}
你为什么不将其存储在一个列表或数组,并打印反向 – Madhan
使用堆栈。 (递归的一种方式) – Manikandan
我同意@Madhan,请看这里:http://stackoverflow.com/questions/10766492/reverse-the-arraylist-in-simplest-way – smoggers