我有一个名为 的类SnowFallReport
,当从它创建一个对象时,它会随机分配一个从 1-20 的数字到一个名为 的字段snowFall
。目的是生成具有随机降雪量的虚构降雪报告。然后我尝试创建一种方法,可以根据字段中的数字显示一定数量的星号snowFall
。给了我一个提示,我应该使用for
循环来这样做,我只是不知道如何正确地用词。代码如下:
import java.util.Random;
public class SnowFallReport
{
// Random amount of snow
private double snowAmount;
// Default constructor creates random amount and assigns to snowAmount
public SnowFallReport()
{
Random snowFall = new Random();
snowAmount = (snowFall.nextDouble() * 20);
}
public double getSnow()
{
return snowAmount;
}
public String getStars()
{
for (int starCount = 0; starCount >= snowAmount; starCount++)
return "*";
/* This is what I thought it should be^ but it turns out I need a return
statement outside of the for loop. I've tried a couple of different ways with no luck */
}
public static void main(String[] args)
{
SnowFallReport day1 = new SnowFallReport();
String lol = day1.getStars();
System.out.print(lol);
}
}