需要帮助的高中生;将输出写入文件(使用移动距离程序)?
Highschool needing help please; writing output to a file (using the distance traveled program)?
任何人都可以帮我解决下面的代码吗?我已经为此工作了几个小时,无论我做什么,似乎都无法将输出写入文件。我是一名高中生,向我的老师求助,但她发给我的东西没有帮助,我现在完全不知所措。我自己独立学习这门课程,所以希望 Whosebug 家族的人能给我一些指导?这不是因为我缺乏尝试。我只是不知所措。提前致谢!!
/**
*
*/
package distanceTraveledReport;
import java.util.Scanner;
import java.io.*;
/**
* @author Jerin
*
*/
public class DistanceTraveledReport {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//prints out name and project I am working on
System.out.println("Jerin Anderson, This is my Distance Traveled Report");
//create scanner
Scanner scanner = new Scanner(System.in);
double vehicleSpeed = -1; //to hold the speed from the vehicle
int hoursTraveled = 0; //to hold the hours traveled by the vehicle
double distanceTraveled; //to hold the distance traveled for each hour
//while loop, inputs validation
while ( vehicleSpeed < 0 )
{
System.out.println("Enter the speed of the vehicle (in miles-per-hour)" );
vehicleSpeed = scanner.nextDouble();
}
while (hoursTraveled < 1)
{
System.out.println("Enter the number of hours traveled by the vehicle" );
hoursTraveled = scanner.nextInt();
}
//heading at the top and lines to separate table
System.out.println(" Hour\t Distance Traveled");
System.out.println("---------------------------");
{
//write the table of distances
distanceTraveled = 1;
while (distanceTraveled <= hoursTraveled)
{
//Display the distance for this period.
System.out.println(distanceTraveled + "\t\t" + distanceTraveled * vehicleSpeed);
//Increment period
distanceTraveled++;
}
//Close the file.
System.out.close();
System.out.println("Report written to DistanceReport.txt");
}
}
}
您必须意识到 System.out.println() 不会写入文件,但 FileWriter 会 。对代码的修复以 //TODO
.
突出显示
package distanceTraveledReport;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/**
* @author Jerin
*
*/
public class DistanceTraveledReport {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO "TODO check this out" marks addition of FileWriter fileWriter.
//TODO check this out
//TODO make sure you change the filename
首先打开.txt文件的路径,因为当前路径不是你的文件路径(见“cole.henrich/Downloads...”)
然后复制绝对路径:
然后粘贴到FileWriter的参数中:
.
代码继续:
FileWriter fileWriter = new FileWriter("/Users/cole.henrich/Downloads/Whosebug/src/distanceTraveledReport/DistanceReport.txt");
fileWriter.write("The FileWriter writes to the file. System.out.println() does not.\n");
//prints out name and project I am working on
System.out.println("Jerin Anderson, This is my Distance Traveled Report");
//create scanner
Scanner scanner = new Scanner(System.in);
double vehicleSpeed = -1; //to hold the speed from the vehicle
int hoursTraveled = 0; //to hold the hours traveled by the vehicle
double distanceTraveled; //to hold the distance traveled for each hour
//while loop, inputs validation
while ( vehicleSpeed < 0 )
{
System.out.println("Enter the speed of the vehicle (in miles-per-hour)" );
vehicleSpeed = scanner.nextDouble();
}
while (hoursTraveled < 1)
{
System.out.println("Enter the number of hours traveled by the vehicle" );
hoursTraveled = scanner.nextInt();
}
//heading at the top and lines to separate table
System.out.println(" Hour\t Distance Traveled");
System.out.println("---------------------------");
{
//write the table of distances
distanceTraveled = 1;
while (distanceTraveled <= hoursTraveled)
{
//Display the distance for this period.
String display = ""+ (distanceTraveled + "\t\t" + distanceTraveled * vehicleSpeed) + "\n";
System.out.println(display);
//TODO check this out
fileWriter.write(display);
//Increment period
distanceTraveled++;
}
//Close the file.
//TODO check this out
fileWriter.close();
//TODO check this out: DO NOT close the System.out. Close FileWriter fileWriter.
System.out.println("Report written to DistanceReport.txt");
}
}
}
确保您了解 FileWriter。阅读评论中的链接。这是文件中的输出(不是 System.out)。
The FileWriter writes to the file. System.out.println() does not.
1.0 65.0
2.0 130.0
3.0 195.0
4.0 260.0
5.0 325.0
6.0 390.0
7.0 455.0
任何人都可以帮我解决下面的代码吗?我已经为此工作了几个小时,无论我做什么,似乎都无法将输出写入文件。我是一名高中生,向我的老师求助,但她发给我的东西没有帮助,我现在完全不知所措。我自己独立学习这门课程,所以希望 Whosebug 家族的人能给我一些指导?这不是因为我缺乏尝试。我只是不知所措。提前致谢!!
/**
*
*/
package distanceTraveledReport;
import java.util.Scanner;
import java.io.*;
/**
* @author Jerin
*
*/
public class DistanceTraveledReport {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//prints out name and project I am working on
System.out.println("Jerin Anderson, This is my Distance Traveled Report");
//create scanner
Scanner scanner = new Scanner(System.in);
double vehicleSpeed = -1; //to hold the speed from the vehicle
int hoursTraveled = 0; //to hold the hours traveled by the vehicle
double distanceTraveled; //to hold the distance traveled for each hour
//while loop, inputs validation
while ( vehicleSpeed < 0 )
{
System.out.println("Enter the speed of the vehicle (in miles-per-hour)" );
vehicleSpeed = scanner.nextDouble();
}
while (hoursTraveled < 1)
{
System.out.println("Enter the number of hours traveled by the vehicle" );
hoursTraveled = scanner.nextInt();
}
//heading at the top and lines to separate table
System.out.println(" Hour\t Distance Traveled");
System.out.println("---------------------------");
{
//write the table of distances
distanceTraveled = 1;
while (distanceTraveled <= hoursTraveled)
{
//Display the distance for this period.
System.out.println(distanceTraveled + "\t\t" + distanceTraveled * vehicleSpeed);
//Increment period
distanceTraveled++;
}
//Close the file.
System.out.close();
System.out.println("Report written to DistanceReport.txt");
}
}
}
您必须意识到 System.out.println() 不会写入文件,但 FileWriter 会 。对代码的修复以 //TODO
.
package distanceTraveledReport;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/**
* @author Jerin
*
*/
public class DistanceTraveledReport {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO "TODO check this out" marks addition of FileWriter fileWriter.
//TODO check this out
//TODO make sure you change the filename
首先打开.txt文件的路径,因为当前路径不是你的文件路径(见“cole.henrich/Downloads...”)
然后复制绝对路径:
然后粘贴到FileWriter的参数中:
代码继续:
FileWriter fileWriter = new FileWriter("/Users/cole.henrich/Downloads/Whosebug/src/distanceTraveledReport/DistanceReport.txt");
fileWriter.write("The FileWriter writes to the file. System.out.println() does not.\n");
//prints out name and project I am working on
System.out.println("Jerin Anderson, This is my Distance Traveled Report");
//create scanner
Scanner scanner = new Scanner(System.in);
double vehicleSpeed = -1; //to hold the speed from the vehicle
int hoursTraveled = 0; //to hold the hours traveled by the vehicle
double distanceTraveled; //to hold the distance traveled for each hour
//while loop, inputs validation
while ( vehicleSpeed < 0 )
{
System.out.println("Enter the speed of the vehicle (in miles-per-hour)" );
vehicleSpeed = scanner.nextDouble();
}
while (hoursTraveled < 1)
{
System.out.println("Enter the number of hours traveled by the vehicle" );
hoursTraveled = scanner.nextInt();
}
//heading at the top and lines to separate table
System.out.println(" Hour\t Distance Traveled");
System.out.println("---------------------------");
{
//write the table of distances
distanceTraveled = 1;
while (distanceTraveled <= hoursTraveled)
{
//Display the distance for this period.
String display = ""+ (distanceTraveled + "\t\t" + distanceTraveled * vehicleSpeed) + "\n";
System.out.println(display);
//TODO check this out
fileWriter.write(display);
//Increment period
distanceTraveled++;
}
//Close the file.
//TODO check this out
fileWriter.close();
//TODO check this out: DO NOT close the System.out. Close FileWriter fileWriter.
System.out.println("Report written to DistanceReport.txt");
}
}
}
确保您了解 FileWriter。阅读评论中的链接。这是文件中的输出(不是 System.out)。
The FileWriter writes to the file. System.out.println() does not.
1.0 65.0
2.0 130.0
3.0 195.0
4.0 260.0
5.0 325.0
6.0 390.0
7.0 455.0