gpt4 book ai didi

java - 用其他方法写出表格

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:00 25 4
gpt4 key购买 nike

嘿,我想用第二种方法写出表格。首先,我将 int x 更改为表(其他数组索引中的每个数字),在第二种方法中,我想写出表。怎么做 ?

int  tab [] =new int [4];
public int[] change(int x) {
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
System.out.println(tab.toString());

return tab;
}

public void writeout(int a[]) {
this.tab=a;//how to connect tab from change() with int a[]
for( int i=0;i<=3;i++) {
System.out.println(a[i]);
}

最佳答案

您可以使用最新的 Java 8 API 来打印您的数组。您的代码可能如下所示:

import java.util.Arrays;

public class App {

int tab[] = new int[4];

public int[] change(int x) {
tab[0] = x/1000;
tab[1] = (x/100)%10;
tab[2] = (x/10)%10;
tab[3] = x%10;

return tab;
}

public void writeout(int array[]) {
Arrays.stream(array).forEach(System.out::println);
}

public static void main(String[] args) {
App app = new App();
app.writeout(app.change(2));
}
}

关于java - 用其他方法写出表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51284862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com