下面的评论回答了另一个问题,这是我提出新问题的唯一途径......
好。我的程序就像在.txt文件上写信息一样。目前它正在将信息写入文本文件的末尾,如下所示:
t/1/15/12
o/1/12/3
o/2/15/8
... (lots of lines like this.. all with different numbers)
o/1/16/4
然后..当我使用:
添加行时BufferedWriter fw = new BufferedWriter(new FileWriter(new File("C://Users/Mini/Desktop/Eclipse/Japda/map/" +Numbers.map +".txt"), true));
fw.newLine();
fw.write(RightPanel.mode.charAt(0) +"/" +ID +"/" +Numbers.middlex +"/" +Numbers.middley);
fw.close();
它添加我想要的行但当前到文本文件的末尾..但是我希望它将该行写入文本文件的特定部分..我已经知道行的编号我想写它..(根据其他线计算..):D有什么办法吗?或者,在该文本文件的中间编辑一个特定行的最佳方法是什么?
答案 0 :(得分:1)
您需要 do-while循环:
do {
//code
} while (expression);
来源:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
你可能想要这样的东西:
int[] done = new int[100];
int randomquestion;
do{
randomquestion = (int)(Math.random() * 83 + 1);
if(done[randomquestion] != 1)
{
//ask random question
//if answer is correct, set done[randomquestion] = 1
//else just let do-while loop run
}
//check if all questions are answered
} while (!areAllQuestionsComplete(done));
这是方法areAllQuestionsComplete(int []):
private boolean areAllQuestionsComplete(int[] list)
{
for(int i = 0; i<list.length; i++)
{
if(list[i] != 1)
{
return false;//found one false, then all false
}
}
return true;//if it makes it here, then you know its all done
}
查看您的最新代码:
for(int i = 0; i<done.length; i++)
{
done[i] = 0;//need default values else wise itll just be NULL!!!
}
do{
ran = (int)(Math.random() * 83 + 1);
//before entering the do-while loop, you must set default values in the entire done[] array
if(done[ran] != 1)
{
//ask random question
//if answer is correct, set done[ran] = 1
//else just let do-while loop run
if (ran == 1) { //1
question = "kala";
rightanswer = "fish";}
if (ran == 2) { //2
question = "peruna";
rightanswer = "potato";}
if (ran == 3) { //3
question = "salaatti";
rightanswer = "cabbage";}
if (ran == 4) { //4
question = "kalkkuna";
rightanswer = "turkey";}
if (ran == 5) { //5
question = "kia";
rightanswer = "tikku";}
//YOU MUST HAVE EVERY CONDITION COVERED
//say your random number makes the number 10
//you dont set question to anything at all (hence getting null!)
System.out.println(question);
System.out.print("Vastaus?: ");
answer = in.readLine();
//if (answer == rightanswer){
//must use .equals with Strings...not ==
if (answer.equals(rightanswer)){
right++;
done[ran] = 1;}
else{wrong++;}
}
//check if all questions are answered
} while (!areAllQuestionsComplete(done));//use the method I wrote!
修改强>
您必须在数组中输入默认值。创建数组时,默认值为null。
int[] done = new int[100];//create array but everything is null
for(int i = 0; i<done.length; i++)
{
done[i] = 0;//need default values else wise it'll just be NULL!!!
}
//must be done before the do-while loop starts
最后,确保您的随机数生成器选择正确的数字范围。如果你有一个大小为100的数组,那么它的索引将是0-99。这意味着没有完成[100]。它从完成[0]到完成[99]。
如果你已经完成[]大小为5,那么它将从完成[0]到完成[4]。这意味着你应该随机生成如下:
randomquestion =(int)(Math.random()* 5);
答案 1 :(得分:1)
要实现您的需求,您需要使用RandomAccessFile。脚步: 首先创建一个RandomAccessFile:
创建一个名为lineStart的变量,该变量最初设置为0 (文件的开头)
使用readline
检查是否是您希望在
如果它是正确的位置,则lineStart将保持该位置 就在您希望之前插入的行之前。使用寻求 通过最初使用seek(0)来将你定位在正确的位置 将你定位在文件的开头,然后搜索(lineStart)来获取 所需的位置。然后使用writeChars写入文件。 请记住,您必须明确地写新行。
如果它不是您要插入的位置,则调用getFilePointer,然后 在lineStart中存储值
重复步骤2-5直到您到达所需的插入地点