我在我的文件中有8個名字,每一行只有一個名字。我試圖用隨機寫出一個名字。我寫了一些代碼,但我不知道如何繼續。(我試圖解決這個問題,而不使用數組,因爲我們還沒有學習)。 我的名單中有這些名字;寫出一個隨機名
patrica
natascha
lena
sara
rosa
kate
funny
ying
,我想與system.out.println
只隨機
在這裏,人們的名字寫出來是我的代碼
BufferedReader inputCurrent = new BufferedReader(new FileReader("aText.txt"));
String str;
int rowcounter =0;
int mixNum =0;
String strMixNum=null;
while((str = inputCurrent.readLine())!= null){
rowcounter++;
mixNum = rnd.nextInt(rowcounter)+1;
//strMixNum = ""+strMixNum;
String str2;
while((str2 = inputCurrent.readLine())!= null){
// i dont know what i s shall write here
System.out.println(str2);
}
}
inputCurrent.close();
非常好,我喜歡評論。 – nhouser9
@LenaMonikaMarshall沒有辦法直接做到這一點。您必須運行該文件並計算行數。我想你最好的嘗試是將閱讀和計數的過程留給'java.nio.file.Files#readAllLines',儘管這有點超出你的水平。 – Paul