Java 数组运行,但在编译时没有给出正确的值?
我只是不知道我哪里弄错了。我已经包括了全部代码。我试图让百分比部分显示30个不同的值,但它一直显示29个。有没有一个简单的解决办法,我是否把它弄得太复杂了Java 数组运行,但在编译时没有给出正确的值?,java,arrays,loops,count,random-seed,Java,Arrays,Loops,Count,Random Seed,我只是不知道我哪里弄错了。我已经包括了全部代码。我试图让百分比部分显示30个不同的值,但它一直显示29个。有没有一个简单的解决办法,我是否把它弄得太复杂了 private static final Scanner stdIn = new Scanner(System.in); private static Random rng; public static void main (String[] args){ long rngSeed;
private static final Scanner
stdIn = new Scanner(System.in);
private static Random
rng;
public static void main (String[] args){
long
rngSeed;
int
numberOfFlips,
totalNumberOfRuns = 1,
run = 0;
boolean
theCoin,
tempVal = false;
System.out.println("Welcome to the coin flip analyzer.\n" +
"How many flips?");
numberOfFlips = stdIn.nextInt();
System.out.println("What do you want to seed the random number generator with?");
rngSeed = stdIn.nextLong();
int[]runLength = new int[50];
rng = new Random(rngSeed);
for (int i = 0; i < numberOfFlips; i++) {
theCoin = rng.nextBoolean();
if (i > 0 && theCoin != tempVal) {
if (i < 50) {
System.out.print(run + " ");
}
runLength[run - 1]++;
totalNumberOfRuns++;
run = 1;
}
else
run++;
if (theCoin) {
if (i < 50) {
System.out.print("H");
}
tempVal = true;
}
else {
if (i < 50) {
System.out.print("T");
}
tempVal = false;
}
}
System.out.print("...");
System.out.println();
System.out.println("There were a total of " + totalNumberOfRuns +
" distinct runs in the simulation.\nTheir breakdown follows.");
// run length table header line
System.out.println("[run length] = # (as percentage of all runs)");
// your code to display the count and frequency percentage of each run length
// should follow
for (int i = 0; i < runLength.length; i++) {
double percentageFreq = ((double) + (runLength[i]) / (totalNumberOfRuns) * 100);
if (runLength[i] > 0)
System.out.println("[" + (i+1) + "] = " + runLength[i] + " (" + String.format("%1.1f", percentageFreq) + " %)");
}
}
}
专用静态最终扫描仪
stdIn=新扫描仪(System.in);
私有静态随机
rng;
公共静态void main(字符串[]args){
长的
RNG种子;
int
翻转次数,
totalNumberOfRuns=1,
run=0;
布尔值
委员会,
tempVal=false;
System.out.println(“欢迎使用硬币翻转分析器。\n”+
“翻了多少次?”);
numberOfFlips=stdIn.nextInt();
System.out.println(“您想用什么作为随机数生成器的种子?”);
rngSeed=stdIn.nextLong();
int[]运行长度=新的int[50];
rng=新随机(rngSeed);
对于(int i=0;i0&&theCoin!=tempVal){
如果(i<50){
系统输出打印(运行+“”);
}
游程长度[游程-1]+;
总运行次数++;
run=1;
}
其他的
运行++;
如果(中央){
如果(i<50){
系统输出打印(“H”);
}
tempVal=true;
}
否则{
如果(i<50){
系统输出打印(“T”);
}
tempVal=false;
}
}
系统输出打印(“…”);
System.out.println();
System.out.println(“总共有”+totalNumberOfRuns+
“模拟中的不同运行。\n它们的细分如下。”);
//运行长度表标题行
System.out.println(“[运行长度]=#(占所有运行的百分比)”);
//您的代码将显示每个运行长度的计数和频率百分比
//应该遵循
for(int i=0;i0)
System.out.println(“[”+(i+1)+“]=“+runLength[i]+”(“+String.format”(“%1.1f”,percentageFreq)+“%)”);
}
}
}
结果最重要的部分使用:50次翻转和随机种子值1200
H1 T1 H1 T1 HHHH3 TTTTT 5 H1 T1 HHHHH4 T1 HH2 T1 H1 T1 H1 T1 H1 TTT3 H1 TTT3 H1 TTTT4 H1 T1 HHHHH3 TT2 H1 T
在模拟中总共有30次不同的运行
他们的分类如下
- [运行长度]=#(占所有运行的百分比)
- [1] =20(66.7%)(我需要它是21,因为最后一个值是“T”)
- [2] =2(6.7%)
- [3] =4(13.3%)
- [4] =2(6.7%)
- [5] =1(3.3%)
您的代码有一些问题。它不起作用的最终原因是最后一次投币没有被计算在内 代码输出的原因是模拟中总共有30次不同的运行。是因为您以错误的值启动了
totalNumberOfRuns
totalNumberOfRuns
应该从0
开始,而不是从1
开始
还要注意掷硬币输出的结尾是如何的T.
,在T
之后应该有一个1
。您需要将print语句放在循环的末尾,否则它将只打印上一次迭代的数字,而根本不会在最后一次迭代中运行
此外,在整个代码中,您都使用了幻数。例如,如果(i<50)
,int[]runLength=newint[50]
。魔法数字是邪恶的,你不应该使用它们。举例说明为什么神奇数字是邪恶的:当被问及硬币应该翻转多少次时,如果用户输入100
,该怎么办?在这种情况下,代码将无法正常运行。不管您在这里是否使用了幻数,这些条件语句都是毫无意义的,因为如果您为numberOfFlips
输入50
,则i
将永远不会大于50
我也有点被你代码中的样式所困扰。请使用大括号,即使是一行长的块。至少,至少要和它保持一致。在某些情况下,对单行if语句使用大括号,但有一种情况下,对单行else语句不使用大括号
另外,输出频率的打印语句非常混乱。这是使用System.out.printf
的理想场所,尤其是因为您已经在print方法内部使用了String.format
。printf
方法是一件很漂亮的事情,你应该习惯它。以下是您应该使用的:
System.out.printf(“[%d]=%d(%1.1f%%)%n”,i+1,运行长度[i],百分比频率)代码>
我让你的代码正常工作了,我也清理了一下。在您复制和粘贴此解决方案之前,我恳请您考虑一下为什么我上面的解释会修复您的代码,以及我如何清理您的代码(例如,rng
不应是静态的)
给你:)
公共类主{
专用静态最终扫描仪stdIn=新扫描仪(System.in);
公共静态void main(字符串[]args){
int totalNumberOfRuns=0;
int run=1;
布尔值theCoin,tempVal=false;
System.out.println(“欢迎使用硬币翻转分析器。\n”
+“翻了多少次?”);
int numberOfFlips=stdIn.nextInt();
系统输出
.println(“您想用什么作为随机数生成器的种子?”);
long rngSeed=stdIn.nextLong();
随机rng=新随机(rngSeed);
int[]runLength=new int[numberOfFlips];
对于(int i=0;ipublic class Main {
private static final Scanner stdIn = new Scanner(System.in);
public static void main(String[] args) {
int totalNumberOfRuns = 0;
int run = 1;
boolean theCoin, tempVal = false;
System.out.println("Welcome to the coin flip analyzer.\n"
+ "How many flips?");
int numberOfFlips = stdIn.nextInt();
System.out
.println("What do you want to seed the random number generator with?");
long rngSeed = stdIn.nextLong();
Random rng = new Random(rngSeed);
int[] runLength = new int[numberOfFlips];
for (int i = 0; i < numberOfFlips; i++) {
theCoin = rng.nextBoolean();
if (theCoin != tempVal) {
runLength[run - 1]++;
totalNumberOfRuns++;
run = 1;
} else {
run++;
}
if (theCoin) {
System.out.print("H");
tempVal = true;
} else {
System.out.print("T");
tempVal = false;
}
System.out.print(run + " ");
}
System.out.print("...");
System.out.println();
System.out
.println("There were a total of "
+ totalNumberOfRuns
+ " distinct runs in the simulation.\nTheir breakdown follows.");
// run length table header line
System.out.println("[run length] = # (as percentage of all runs)");
// your code to display the count and frequency percentage of each run
// length
// should follow
for (int i = 0; i < runLength.length; i++) {
double percentageFreq = ((double) +(runLength[i])
/ (totalNumberOfRuns) * 100);
if (runLength[i] > 0) {
System.out.printf("[%d] = %d (%1.1f%%)%n", i + 1, runLength[i],
percentageFreq);
}
}
}
}