2016-04-14 35 views
-1

這是我的代碼。這些列不打印出來,但數字確實如此。如何將數字對齊到不同的列中。元素0-9的一列列的其他三個列1-3編寫一個程序,創建3個獨立的數組,每個數組有10個元素

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

看起來你可能想最後System.out.printSystem.out.println。這樣每次呼叫都會附加一個換行符。我現在猜測所有的輸出都在一行上。

0
  1. 爲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.println(lcv + "\t" + first[lcv] + "\t" + second[lcv]+ "\t"+ third[lcv]); 
    
  • 相關問題