我有一个叫SnowFallReport
的类,当从中创建一个对象时,它会随机将一个1-20的数字分配给一个名为snowFall
的字段。目的是生成随机雪量的虚拟雪情报告。然后我试图创建一个方法,可以根据snowFall
字段中的数字显示一定数量的星号。给了我一个暗示,我应该使用for
循环来做到这一点,但我无法弄清楚如何正确地说出它。码如下:创建一个“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);
}
}
'return'语句将切断循环。程序流程将退出该功能。 – Oneb 2013-02-21 01:48:23