Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将表中的值从两位数剪切到4位数_Java - Fatal编程技术网

Java 将表中的值从两位数剪切到4位数

Java 将表中的值从两位数剪切到4位数,java,Java,我必须将数字的双值剪切为,但我不知道如何使用表中的值。我需要从0.254300到0.2543。 这是我的代码,所以如何削减额外的零 public class Diagonal{ public static void main(String[] args){ double [][] tab = new double [5][5]; for (int i = 0; i < 5; i++){ for (int j = 0; j < 5; j++){

我必须将数字的双值剪切为,但我不知道如何使用表中的值。我需要从0.254300到0.2543。 这是我的代码,所以如何削减额外的零

public class Diagonal{
public static void main(String[] args){


    double [][] tab = new double [5][5];


    for (int i = 0; i < 5; i++){
        for (int j = 0; j < 5; j++){
            tab[i][j] = (int) (Math.random() * 9000) / 10000.0;

        }
    }
    for(int i = 0; i < 5; i++){
        for (int j = 0; j < 5; j++)
            if(i==j || i+j==4)


                System.out.printf("%4f", tab[i][j]);
            else
                System.out.printf("%4c",' ');

            System.out.println();

      }                 
    }
 }
公共类对角线{
公共静态void main(字符串[]args){
双精度[]选项卡=新双精度[5][5];
对于(int i=0;i<5;i++){
对于(int j=0;j<5;j++){
tab[i][j]=(int)(Math.random()*9000)/10000.0;
}
}
对于(int i=0;i<5;i++){
对于(int j=0;j<5;j++)
如果(i==j | i+j==4)
System.out.printf(“%4f”,制表符[i][j]);
其他的
System.out.printf(“%4c”,”);
System.out.println();
}                 
}
}

如果要打印这样的内容:

0,4435                  0,6241
      0,2373      0,5890      
            0,2100            
      0,4276      0,4411      
0,4157                  0,3537
你应使用:

if(i==j || i+j==4)
    System.out.printf("%.4f", tab[i][j]);
else
    System.out.printf("%6c",' ');

这就是我需要的。只要一点点开关,谢谢