亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將數組輸出格式化為5個塊空間和下一行的其他一半?

如何將數組輸出格式化為5個塊空間和下一行的其他一半?

紅顏莎娜 2022-09-28 14:48:47
所以基本上,如果輸入是1,我需要在我的數組中打印12個給定的數字,如果輸入是2,我需要打印隨機數。格式應如下所示,一行8,塊間距為5,底部為4。1 2 3 4 5 6 7 89 10 11 12all the 12 numbers will be in the same array but the output must be like  this.and if the input is 2, the array must output 12 random numbers all i know and read in the book is something like this to print nomally  System.out.printf(Arrays.toString(array));  if (i == 1){  System.out.printf("%5s", Arrays.toString(array))   ; }     int i = input.nextInt();    int[] array = {1,2,3,4,5,6,7,8,9,10,11,12};  System.out.printf(Arrays.toString(array));  if (i == 1){  System.out.printf("%5s", Arrays.toString(array))   ; }  I wanted the print to be >1 2 3 4 5 6 7 8 /n 9 10 11 12   but the output is within sets {}
查看完整描述

2 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

您可以不用這種方式進行核心打?。篿f


public class TTest1 {


    public static void main(String... args) {

        int[] array = {1,2,3,4,5,6,7,8,9,10,11,12};

        for (int i = 0 ; i < array.length ; i++)

            System.out.printf("%5d%s", array[i], (i % 8 == 7)? "\n\n" : "");

        System.out.println();

    }

}

結果:


1    2    3    4    5    6    7    8


9   10   11   12


查看完整回答
反對 回復 2022-09-28
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

您可以使用掃描儀從用戶處獲取輸入:


Scanner input = new Scanner(System.in);

int i = input.nextInt();


if(i == 1) {

    int[] array = {1,2,3,4,5,6,7,8,9,10,11,12};

    for(int ind = 0; ind < array.length; ind++) {

        System.out.printf("%5d ", array[ind]);

        if(ind == 7) {

            System.out.println(); //to have 8 numbers on the first line

        }

     }

}

else {

     //write code for random number generation here

    }


查看完整回答
反對 回復 2022-09-28
  • 2 回答
  • 0 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號