英文:
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 < 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();
}
}
}
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 < 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();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论