我已经浏览了以前的问题,并且真的试图弄清楚这一点。来到这里我觉得很脏,因为是的,这是作业,但我也试图联系我的教授。过去两周没有回复。我确信这是一个简单的修复,我只是没有得到它,但该死的我已经使用了一段时间并且被卡住了。先谢谢了。
我似乎无法在总数的右侧打印星号。每个“*”代表总卷数的 1%。我想我拥有一切,但我一直无法弄清楚如何在右侧打印它们。
所以,而不是;
2:****
3:******
4:**
等等..
我明白了;
****2:
**3:
*****4:
等等..
这是我的代码:
import java.util.Random;
import java.util.Scanner;
public class DoubleDiceRoll
{
public static void main(String[] args)
{
Random r = new Random();
Scanner in = new Scanner (System.in);
int[] frequency = new int[13];//Recording the number of times a number was rolled
int numRolls = 0;//How many rolls the user wants to simulate
int numAsterisks = 0;
int dieOne = 0;//Roll of die one
int dieTwo = 0;//Roll of die two
int rollTotal = 0;//Sum of the rolls of die one and two
String stars = "";
//Welcom user to the program
System.out.println("Welcome to the dice throwing simulator!");
System.out.println(" ");
System.out.println("How many dice rolls would you like to simulate?");
numRolls = in.nextInt();
//Simulate the number of rolls for die 1 and 2
for(int i = 0; i < numRolls; i++)
{
//Roll dieOne
dieOne = r.nextInt(6) +1;
//Roll dieTwo
dieTwo = r.nextInt(6) +1;
rollTotal = dieOne + dieTwo;
frequency[rollTotal]++;
}//end for
//Print Results
System.out.println("DICE ROLLING SIMULATION RESULTS" + "\n" + "Each \"*\" represents 1% of the total number of rolls." + "\n"
+ "Total number of rolls: " + numRolls);
System.out.println("");//Space between text and results
//Create for loop for print statement
for(int total = 2; total < frequency.length; total++)
{
numAsterisks = 100 * frequency[total] / numRolls;
for(int j = 0; j < numAsterisks; j++)
{
System.out.println("*");
}
System.out.println(total + ": ");
}
}
}
我知道我已经将 * 设置为在总数之前打印,但是我尝试打印它的所有其他方式似乎都更加混乱。我已将 * 设置为等于一个字符串,例如:
for(int j = 0; j < numAsterisks; j++)
{
stars += "*";
}
System.out.print (total + stars);
但是 * 与正确的百分比不匹配,最终会打印出随机数量的星号。
料青山看我应如是
鸿蒙传说
忽然笑
相关分类