何らかの理由で、このコードは切り捨てられたtext.txtファイルになります。(私によれば)1000の結果を書き出す必要がありますが、出力ファイルにはさまざまな量の行があります(実行によって異なります)。奇妙なことに、ファイルへの書き込みは書き込みコマンドの途中で停止するため、行が完全でない場合があります。現在、最新の実行のテキストファイルの最後の3行は次のとおりです。
749, 78.97988, 97.80454, 99.6625, 94.00000015258789
750, 4.1745043, 86.64212, 107.59311, 71.00000008583069
751,
以上です。その後は何もありません。
コードは次のとおりです。
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Random;
public class ColorGrayScale {
/**
* @param args
* @throws IOException
*/
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Writer out = new BufferedWriter(new FileWriter("test.txt"),16*1024);
Random generator = new Random();
float red = 0, green = 0, blue = 0;
int i = 0;
while (i<1000) {
float grey = generator.nextInt(127) + 64;
int sequence = generator.nextInt(6) + 1; // to pick from 1 of six
// orders
switch (sequence) { // the various orders that red green and blue
// are going to be in
case 1:
red = (float) (generator.nextFloat() * (grey / .21));
green = (float) (generator.nextFloat() * ((grey - (red * .21)) / .71));
blue = (float) ((grey - (red * .21) - (green * .71)) / 0.08);
break;
case 2:
red = (float) (generator.nextFloat() * (grey / .21));
blue = (float) (generator.nextFloat() * ((grey - (red * .21)) / .08));
green = (float) ((grey - (red * .21) - (blue * .08)) / 0.71);
break;
case 3:
green = (float) (generator.nextFloat() * (grey / .71));
red = (float) (generator.nextFloat() * ((grey - (green * .71)) / .21));
blue = (float) ((grey - (red * .21) - (green * .71)) / .08);
break;
case 4:
green = (float) (generator.nextFloat() * (grey / .71));
blue = (float) (generator.nextFloat() * ((grey - (green * .71)) / .08));
red = (float) ((grey - (green * .71) - (blue * .08)) / .21);
break;
case 5:
blue = (float) (generator.nextFloat() * (grey / .08));
red = (float) (generator.nextFloat() * ((grey - (blue * .08)) / .21));
green = (float) ((grey - (blue * .08) - (red * .21)) / .71);
break;
case 6:
blue = (float) (generator.nextFloat() * (grey / .08));
green = (float) (generator.nextFloat() * ((grey - (blue * .08)) / .71));
red = (float) ((grey - (blue * .08) - (green * .71)) / .21);
break;
}
if (red < 256 && blue < 256 && green < 256) {
out.write("" + i + ", " + red + ", " + green + ", " + blue
+ ", " + (.21 * red + .71 * green + 0.08 * blue + "\n"));
i++;
}
}
}
}