我在Java中每千行打印时遇到一些麻烦。如果我尝试1,2,3和4它工作正常,但每个数字超过4,它什么都不打印。这是我的循环:
public class Monkey extends Thread
{
String[] chars = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "æ", "ø", "å", " "};
String currentString;
String name;
public Monkey(String currentString, String name)
{
super();
this.currentString = currentString;
this.name = name;
}
public void run()
{
String workingString = "";
int loops = 0;
Random ran = new Random();
while(!currentString.equalsIgnoreCase(workingString))
{
String c = chars[ran.nextInt(chars.length)];
workingString += c;
if(currentString.length() < workingString.length())
{
if((loops % 1000) == 0)
{
System.out.println(name + ": \"Forfra! Loops: " + loops + " Strengen var: " + workingString + "\"");
}
workingString = "";
}
loops++;
}
System.out.println(name + " har skrevet shakespears værker!! Det tog kun " + loops + " loops!");
System.exit(MAX_PRIORITY);
}
}
从我的主要内容我创建了以下对象:
Monkey james = new Monkey("der var engang", "James");
james.start();
我要更改的参数是if:
中的1000if(currentString.length() < workingString.length())
{
if((loops % 1000) == 0)
{
System.out.println(name + ": \"Forfra! Loops: " + loops + " Strengen var: " + workingString + "\"");
}
workingString = "";
}
如果我将其更改为1,2,3或4,则打印正常。但如果数字(代码中的1000)大于4,则不会打印任何内容。