【发布时间】:2018-07-31 14:13:41
【问题描述】:
好的,所以我想做的是使用星号制作图片。图片应该不超过或少于 10 行,并且每行应该是 1-10 范围内的任意数量的星号。我遇到的唯一问题是我没有得到 10 行,相反,我得到了 11 到 17 行。我不确定我错过了什么,我很感激你能提供给我的任何见解。 谢谢!
public class RandomPicture {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// Create a Random picture using asterisks
Random rand = new Random();
for (int count = 0; count <=2; count++)
{
if (rand.nextInt(2) == 0){
System.out.println("*");
} if (rand.nextInt(2) == 0){
System.out.println("**");
} if (rand.nextInt(2) == 0){
System.out.println("***");
} if (rand.nextInt(2) == 0){
System.out.println("****");
} if (rand.nextInt(2) == 0){
System.out.println("*****");
} if (rand.nextInt(2) == 0){
System.out.println("******");
} if (rand.nextInt(2) == 0){
System.out.println("*******");
} if (rand.nextInt(2) == 0){
System.out.println("********");
} if (rand.nextInt(2) == 0){
System.out.println("*********");
} if (rand.nextInt(2) == 0){
System.out.println("**********");
}
}
}
}
【问题讨论】:
-
为什么你有 10 个
if语句都评估为true?你可以通过两个复利来逃脱for-loops -
你想实现什么逻辑?为什么要循环 3 次,每次得到 10 个随机数?
-
您能否向我们提供您想要的示例输出?我不明白你想用代码做什么
-
您有 10 个
if语句,每个语句在 50% 的时间内随机评估为真,并被一个迭代 3 次的循环包围。 3 * 10 * 50% = 15,所以程序将打印 15 行 +/- 任何引入的随机变化。 --- 如果要打印 10 行,每行包含 1-10 颗星,则循环 10 次,每次迭代生成一个随机数,并相应打印星数。 -
你的 for (int count = 0; count
标签: java