Java 如何让我的百分比在骰子程序中工作?
我想知道如何计算第二列到第三列的百分比 这就是我所拥有的,但我知道我需要做些别的事情 我宁愿不使用hashmap,而只使用更正确的问题Java 如何让我的百分比在骰子程序中工作?,java,arrays,dice,die,Java,Arrays,Dice,Die,我想知道如何计算第二列到第三列的百分比 这就是我所拥有的,但我知道我需要做些别的事情 我宁愿不使用hashmap,而只使用更正确的问题 for(int i=minRoll;idicer 要获得百分比结果,必须将骰子的类型更改为double而不是int 替换: double percent = ((double)dicer/numRol)*100; 与: 或者,您可以通过远离浮点数学来提高精度: double dicer = sumArray[i]; 或者,您可以通过远离浮点数学来提高精度:
for(int i=minRoll;i
忍者编辑:这是我剩下的代码
for (int i = minRoll; i < maxRoll; i++) {
int dicer = sumArray[i];
double percent = dicer/numRol;
System.out.printf("[%d] \t %d \t %.1f%% \n", i , sumArray[i], percent);
}
import java.util.Scanner;
/**
*
*@作者乔
*/
公共类模拟程序{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.print(“你想掷多少骰子:”);
int amountOfDie=input.nextInt();
System.out.println(“”);
//声明骰子数组
骰子[]骰子数组=新骰子[amountOfDie];
//为每个参考创建一个新模具
int-maxRoll=0;
for(int i=0;i
您使用的是整数算术,这意味着您的结果在保存到百分比
变量之前会转换为int
。为了避免这种情况,只需像这样强制转换一个变量:
import java.util.Scanner;
/**
*
* @author joe
*/
public class DiceSimulator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many dice do you want to roll: ");
int amountOfDie = input.nextInt();
System.out.println("");
//declare diceArray
Die[] diceArray = new Die[amountOfDie];
//create a new Die for each reference
int maxRoll = 0;
for (int i = 0; i < diceArray.length; i++) {
System.out.print("How many sides on die number " + (i + 1) + ""
+ ": ");
int numSides = input.nextInt();
diceArray[i] = new Die(numSides);
int minRoll = amountOfDie;
maxRoll += numSides;
}
int minRoll = amountOfDie;
// int[] sumArray = new int[maxRoll + 1];//to store sum
System.out.println("");
System.out.print("How many times do you want to roll: ");
int numRol = input.nextInt();
System.out.println("\nResults");
int[] sumArray = new int[maxRoll + 1];
for (int i = 0; i < numRol; i++) {
int sum = 0;
for (Die d : diceArray) {
int roll = d.roll();
sum += roll;
}
sumArray[sum]++;
}
System.out.println("");
for (int i = minRoll; i < maxRoll; i++) {
int dicer = sumArray[i];
double percent = (dicer/numRol)*100;
System.out.printf("[%d] \t %d \t %.1f%% \n", i , sumArray[i], percent);
}
}
}
正如@PaulHicks所说,你真的应该乘以100
。您可以这样做,将其声明为浮点文本(100.0
),以避免完全强制转换:
double percent = (double)dicer / numRol;
您使用的是整数算术,这意味着您的结果在保存到
percent
变量之前会转换为int
。为了避免这种情况,只需像这样强制转换一个变量:
import java.util.Scanner;
/**
*
* @author joe
*/
public class DiceSimulator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many dice do you want to roll: ");
int amountOfDie = input.nextInt();
System.out.println("");
//declare diceArray
Die[] diceArray = new Die[amountOfDie];
//create a new Die for each reference
int maxRoll = 0;
for (int i = 0; i < diceArray.length; i++) {
System.out.print("How many sides on die number " + (i + 1) + ""
+ ": ");
int numSides = input.nextInt();
diceArray[i] = new Die(numSides);
int minRoll = amountOfDie;
maxRoll += numSides;
}
int minRoll = amountOfDie;
// int[] sumArray = new int[maxRoll + 1];//to store sum
System.out.println("");
System.out.print("How many times do you want to roll: ");
int numRol = input.nextInt();
System.out.println("\nResults");
int[] sumArray = new int[maxRoll + 1];
for (int i = 0; i < numRol; i++) {
int sum = 0;
for (Die d : diceArray) {
int roll = d.roll();
sum += roll;
}
sumArray[sum]++;
}
System.out.println("");
for (int i = minRoll; i < maxRoll; i++) {
int dicer = sumArray[i];
double percent = (dicer/numRol)*100;
System.out.printf("[%d] \t %d \t %.1f%% \n", i , sumArray[i], percent);
}
}
}
正如@PaulHicks所说,你真的应该乘以100
。您可以这样做,将其声明为浮点文本(100.0
),以避免完全强制转换:
double percent = (double)dicer / numRol;
您使用的是整数算术,这意味着您的结果在保存到
percent
变量之前会转换为int
。为了避免这种情况,只需像这样强制转换一个变量:
import java.util.Scanner;
/**
*
* @author joe
*/
public class DiceSimulator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many dice do you want to roll: ");
int amountOfDie = input.nextInt();
System.out.println("");
//declare diceArray
Die[] diceArray = new Die[amountOfDie];
//create a new Die for each reference
int maxRoll = 0;
for (int i = 0; i < diceArray.length; i++) {
System.out.print("How many sides on die number " + (i + 1) + ""
+ ": ");
int numSides = input.nextInt();
diceArray[i] = new Die(numSides);
int minRoll = amountOfDie;
maxRoll += numSides;
}
int minRoll = amountOfDie;
// int[] sumArray = new int[maxRoll + 1];//to store sum
System.out.println("");
System.out.print("How many times do you want to roll: ");
int numRol = input.nextInt();
System.out.println("\nResults");
int[] sumArray = new int[maxRoll + 1];
for (int i = 0; i < numRol; i++) {
int sum = 0;
for (Die d : diceArray) {
int roll = d.roll();
sum += roll;
}
sumArray[sum]++;
}
System.out.println("");
for (int i = minRoll; i < maxRoll; i++) {
int dicer = sumArray[i];
double percent = (dicer/numRol)*100;
System.out.printf("[%d] \t %d \t %.1f%% \n", i , sumArray[i], percent);
}
}
}
正如@PaulHicks所说,你真的应该乘以100
。您可以这样做,将其声明为浮点文本(100.0
),以避免完全强制转换:
double percent = (double)dicer / numRol;
您使用的是整数算术,这意味着您的结果在保存到
percent
变量之前会转换为int
。为了避免这种情况,只需像这样强制转换一个变量:
import java.util.Scanner;
/**
*
* @author joe
*/
public class DiceSimulator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many dice do you want to roll: ");
int amountOfDie = input.nextInt();
System.out.println("");
//declare diceArray
Die[] diceArray = new Die[amountOfDie];
//create a new Die for each reference
int maxRoll = 0;
for (int i = 0; i < diceArray.length; i++) {
System.out.print("How many sides on die number " + (i + 1) + ""
+ ": ");
int numSides = input.nextInt();
diceArray[i] = new Die(numSides);
int minRoll = amountOfDie;
maxRoll += numSides;
}
int minRoll = amountOfDie;
// int[] sumArray = new int[maxRoll + 1];//to store sum
System.out.println("");
System.out.print("How many times do you want to roll: ");
int numRol = input.nextInt();
System.out.println("\nResults");
int[] sumArray = new int[maxRoll + 1];
for (int i = 0; i < numRol; i++) {
int sum = 0;
for (Die d : diceArray) {
int roll = d.roll();
sum += roll;
}
sumArray[sum]++;
}
System.out.println("");
for (int i = minRoll; i < maxRoll; i++) {
int dicer = sumArray[i];
double percent = (dicer/numRol)*100;
System.out.printf("[%d] \t %d \t %.1f%% \n", i , sumArray[i], percent);
}
}
}
正如@PaulHicks所说,你真的应该乘以100
。您可以这样做,将其声明为浮点文本(100.0
),以避免完全强制转换:
double percent = (double)dicer / numRol;
改变这个
double percent = 100.0 * dicer / numRol;
到
改变这个
double percent = 100.0 * dicer / numRol;
到
改变这个
double percent = 100.0 * dicer / numRol;
到
改变这个
double percent = 100.0 * dicer / numRol;
到
dicer/numRol将始终返回0,因为dicer和numRol是整数,numRol>dicer 要获得百分比结果,必须将骰子的类型更改为double而不是int 替换:
double percent = ((double)dicer/numRol)*100;
与:
dicer/numRol将始终返回0,因为dicer和numRol是整数,numRol>dicer 要获得百分比结果,必须将骰子的类型更改为double而不是int 替换:
double percent = ((double)dicer/numRol)*100;
与:
dicer/numRol将始终返回0,因为dicer和numRol是整数,numRol>dicer 要获得百分比结果,必须将骰子的类型更改为double而不是int 替换:
double percent = ((double)dicer/numRol)*100;
与:
dicer/numRol将始终返回0,因为dicer和numRol是整数,numRol>dicer 要获得百分比结果,必须将骰子的类型更改为double而不是int 替换:
double percent = ((double)dicer/numRol)*100;
与:
或者,您可以通过远离浮点数学来提高精度:
double dicer = sumArray[i];
或者,您可以通过远离浮点数学来提高精度:
double dicer = sumArray[i];
或者,您可以通过远离浮点数学来提高精度:
double dicer = sumArray[i];
或者,您可以通过远离浮点数学来提高精度:
double dicer = sumArray[i];
这就是比例。要获得百分比值,需要将结果乘以100。@Keppil我的第三列仍然显示为零。我已经用我的全部代码编辑了我的文章。有什么地方我做错了吗?@user3023253:试试我的第二个建议。那只会给出比率。要获得百分比值,需要将结果乘以100。@Keppil我的第三列仍然显示为零。我已经用我的全部代码编辑了我的文章。有什么地方我做错了吗?@user3023253:试试我的第二个建议。那只会给出比率。要获得百分比值,需要将结果乘以100。@Keppil我的第三列仍然显示为零。我已经用我的博客编辑了我的帖子