ギャビー:
私は花の名前を表示し、それが太陽や日陰で育つかどうかをコードで終わることになっています。私は2つのファイルを与えられました。私はからデータを取ることになっていたファイルはflowers.datと呼ばれ、以下のデータが含まれます:
Astilbe
Shade
Marigold
Sun
Begonia
Sun
Primrose
Shade
Cosmos
Sun
Dahlia
Sun
Geranium
Sun
Foxglove
Shade
Trillium
Shade
Pansy
Sun
Petunia
Sun
Daisy
Sun
Aster
Sun
私はこのコードが出ています
// Flowers.java - This program reads names of flowers and whether they are grown in shade or sun from an input
// file and prints the information to the user's screen.
// Input: flowers.dat.
// Output: Names of flowers and the words sun or shade.
import java.io.BufferedReader;
import java.io.FileReader;
public class Flowers {
public static void main(String args[]) throws Exception {
// Declare variables here
String flowerName, flowerPosition;
// Open input file.
FileReader fr = new FileReader("flowers.dat");
// Create BufferedReader object.
BufferedReader br = new BufferedReader(fr);
flowerName = br.readLine();
flowerPosition = br.readLine();
// Write while loop that reads records from file.
while ((flowerName = br.readLine()) != null) {
System.out.println(flowerName + " is grown in the " + flowerPosition);
}
br.close();
System.exit(0);
} // End of main() method.
} // End of Flowers class.
私は日陰に成長しているように表示され、すべてを取得していますことを出力します。例えば、それは「マリーゴールドは、日陰で栽培されています。日は日陰で栽培された」と言うようにと。私は何をしないのですか?
誠:
あなたがやっているすべてはされて再印刷変数を。
System.out.println(flowerName + " is grown in the " + flowerPosition);
あなたは常にそれらの値を読み込み得ることができるので、あなたのループをリワーク。
do {
flowerName = br.readLine();
if(flowerName == null) {
break;
}
flowerPosition = br.readLine();
System.out.println(flowerName + " is grown in the " + flowerPosition);
} while(true);