如何在一个二维双精度数组中去除某些双精度数值的小数位?

huangapple go评论73阅读模式
英文:

How can I cut off decimal places of some double numbers in a 2d double array?

问题

以下是您的代码的翻译:

public class Scratch {
    public static void main(String[] args) {
        double[][] values = new double[10][20];
        for (int i = 0; i < values.length; i++) {
            for (int j = 0; j < values[i].length; j++) {
                double x = Math.random();
                double y = Math.random();
                if (j == 0) {
                    values[i][j] = Math.random() < 0.5 ? 0 : 1;
                } else if (j == 1) {
                    values[i][j] = Math.floor(((Math.random() * (250000000 - 500000)) + 500000));
                } else if (j == 2) {
                    values[i][j] = (Math.random() * 23) + 1;
                } else if (j == 3) {
                    values[i][j] = x < 0.25 ? 1 : x < 0.5 ? 3 : x < 0.75 ? 7 : 20; 
                } else if (j == 4) {
                    values[i][j] = y < 0.25 ? 1 : y < 0.5 ? 3 : y < 0.75 ? 7 : 20;
                } else {
                    values[i][j] = Math.floor(Math.random() * 100000) / 100000;  
                }
                System.out.print(values[i][j] + " ; ");
            }
            System.out.println();
        }
    }
}

示例:

0 ; 1688426 ; 15 ; 7 ; 3 ; 0.59743 ; 0.98284 ; ...
1 ; 69715358 ; 17 ; 20 ; 1 ; 0.78631 ; 0.75347 ; ...
英文:

I have this code, which generates me a 10x20 array with a lot of random numbers. But the values in the first 5 columns should not have any decimal places (see the example below). I don't know, how to fix that.

public class scratch {
public static void main(String[] args) {
double[][] values = new double[10][20];
for (int i = 0; i &lt; values.length; i++) {
for (int j = 0; j &lt; values[i].length; j++) {
double x = Math.random();
double y = Math.random();
if (j == 0) {
values[i][j] = Math.random() &lt; 0.5 ? 0 : 1;
} else if (j == 1) {
values[i][j] = Math.floor(((Math.random() * (250000000 - 500000)) + 500000));
} else if (j == 2) {
values[i][j] = (Math.random() * (23)) + 1;
} else if (j == 3) {
values[i][j] = x &lt; 0.25 ? 1 : x &lt; 0.5 ? 3 : x &lt; 0.75 ? 7 : 20; 
} else if (j == 4) {
values[i][j] = y &lt; 0.25 ? 1 : y &lt; 0.5 ? 3 : y &lt; 0.75 ? 7 : 20;
} else {
values[i][j] = Math.floor(Math.random() * 100000) / 100000;  
}
System.out.print(values[i][j] + &quot; ; &quot;);
}
System.out.println();
}
}
}

example:

0 ; 1688426 ; 15 ; 7 ; 3 ; 0.59743 ; 0.98284 ; ...
1 ; 69715358 ; 17 ; 20 ; 1 ; 0.78631 ; 0.75347 ; ...

答案1

得分: -2

你定义了一个double[][]数组,尝试使用Number[][]并将前5列转换为整数

   public class scratch {
        public static void main(String[] args) {
            Number[][] values = new Number[10][20];
            for (int i = 0; i < values.length; i++) {
                for (int j = 0; j < values[i].length; j++) {
                    double x = Math.random();
                    double y = Math.random();
                    if (j == 0) {
                        values[i][j] = Math.random() < 0.5 ? 0 : 1;
                    } else if (j == 1) {
                        values[i][j] = (int)Math.floor(((Math.random() * (250000000 - 500000)) + 500000));
                    } else if (j == 2) {
                        values[i][j] = (int)(Math.random() * (23)) + 1;
                    } else if (j == 3) {
                        values[i][j] = x < 0.25 ? 1 : x < 0.5 ? 3 : x < 0.75 ? 7 : 20;
                    } else if (j == 4) {
                        values[i][j] = y < 0.25 ? 1 : y < 0.5 ? 3 : y < 0.75 ? 7 : 20;
                    } else {
                        values[i][j] = Math.floor(Math.random() * 100000) / 100000;
                    }
                    System.out.print(values[i][j] + " ; ");
                }
                System.out.println();
            }
        }
    }
英文:

you defined a double[][] array,try Number[][] and cast first 5 columns to integer

   public class scratch {
public static void main(String[] args) {
Number[][] values = new Number[10][20];
for (int i = 0; i &lt; values.length; i++) {
for (int j = 0; j &lt; values[i].length; j++) {
double x = Math.random();
double y = Math.random();
if (j == 0) {
values[i][j] = Math.random() &lt; 0.5 ? 0 : 1;
} else if (j == 1) {
values[i][j] = (int)Math.floor(((Math.random() * (250000000 - 500000)) + 500000));
} else if (j == 2) {
values[i][j] = (int)(Math.random() * (23)) + 1;
} else if (j == 3) {
values[i][j] = x &lt; 0.25 ? 1 : x &lt; 0.5 ? 3 : x &lt; 0.75 ? 7 : 20;
} else if (j == 4) {
values[i][j] = y &lt; 0.25 ? 1 : y &lt; 0.5 ? 3 : y &lt; 0.75 ? 7 : 20;
} else {
values[i][j] = Math.floor(Math.random() * 100000) / 100000;
}
System.out.print(values[i][j] + &quot; ; &quot;);
}
System.out.println();
}
}
}

huangapple
  • 本文由 发表于 2020年10月14日 16:39:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64349576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定