这是我的代码。列不打印,但数字打印。如何将数字对齐到不同的列中。一列元素0-9和另外三列数组1-3
import java.util.Scanner;
public class IntroductiontoArrays
{
public static void main (String [] args)
{
// put your code here
final int max=10;
int[] first = new int[max];
int [] order = new int [10];
Scanner input = new Scanner(System.in);
for (int x=0; x<9; x++)
{System.out.print("Enter a two-digit number:");
first [x]=input.nextInt();
order [x]=x;
}
System.out.println("\n"+"Elements" + "\t" + "Array 1" + "\t" + "Array 2" + "\t" + "Array 3" + "\t");
int[] second = {2,4,6,8,10,12,14,16,18,20};
int[]third=new int[max];
for (int lcv=0; lcv<9; lcv++)
{
int a=(int)(Math.random()*10);
third[lcv]=a;}
for (int lcv=0; lcv<=9; lcv++)
{ System.out.print(lcv + "\t" + first[lcv] + "\t" + second[lcv]+ "\t"+ third[lcv]);}
}
}
答案 0 :(得分:0)
您可能希望最后<div class='clearfix' ng-if='$index % 2 == 0'>
为System.out.print
。这样每次调用都会附加换行符。我现在猜测所有输出都在一行上。
答案 1 :(得分:0)
你的循环迭代9个值,而不是10 int [] second
for (int x=0; x<9; x++) // could be changed to x<=9
随后for (int lcv=0; lcv<9; lcv++)
正如@Michael Albers指出的那样,经过你所有的努力,你的显示可能会在同一行(行)上结束,因为你使用System.out.print
,应该使用:
System.out.println(lcv + "\t" + first[lcv] + "\t" + second[lcv]+ "\t"+ third[lcv]);