为什么我的代码每次都显示相同的输出?
Why is my code displaying the same output every time?
我应该以显示花名以及它是生长在阳光下还是阴凉处的代码结束。我得到了 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.
我得到的输出显示所有东西都在阴影中生长。例如,它表示 "Marigold is grown in the Shade. Sun is grown in the Shade" 等等。我错过了什么?
您所做的只是重印变量。
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);
我应该以显示花名以及它是生长在阳光下还是阴凉处的代码结束。我得到了 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.
我得到的输出显示所有东西都在阴影中生长。例如,它表示 "Marigold is grown in the Shade. Sun is grown in the Shade" 等等。我错过了什么?
您所做的只是重印变量。
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);