【问题标题】:write a program that creates 3 separate arrays of 10 elements each编写一个程序,创建 3 个单独的数组,每个数组包含 10 个元素
【发布时间】:2016-04-14 23:49:50
【问题描述】:

这是我的代码。列不会打印出来,但数字会打印出来。如何将数字对齐到不同的列中。一列元素 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]);}
}
}

【问题讨论】:

    标签: arrays multiple-columns


    【解决方案1】:

    看起来您可能希望最后一个 System.out.printSystem.out.println。这样每个调用都会附加一个换行符。我猜现在所有的输出都在一行中。

    【讨论】:

      【解决方案2】:
      1. 您的循环迭代 9 个值,而不是 int [] second 的 10 个值

        for (int x=0; x<9; x++) // could be changed to x<=9
        

      for (int lcv=0; lcv&lt;9; lcv++) 的后续相同

      1. 正如@Michael Albers 指出的那样,由于您使用System.out.print,经过所有努力,您的显示可能会出现在同一行(行)上,应该使用:

        System.out.println(lcv + "\t" + first[lcv] + "\t" + second[lcv]+ "\t"+ third[lcv]);
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-09-27
        • 1970-01-01
        • 1970-01-01
        • 2019-09-19
        • 1970-01-01
        • 2021-05-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多