Java 高中需要帮助请;是否将输出写入文件(使用“行驶距离”程序)?
有人能帮我完成下面的代码吗?我已经为此工作了几个小时,无论我做什么,似乎都无法将输出写入文件。我是一名高中生,向我的老师寻求帮助,但她寄给我的东西没有帮助,我现在完全不知所措。我自己独立学习这门课程,希望stackoverflow家族的人能给我一些建议?这不是因为我没有尝试。我只是不知所措。提前谢谢你Java 高中需要帮助请;是否将输出写入文件(使用“行驶距离”程序)?,java,file,output,report,Java,File,Output,Report,有人能帮我完成下面的代码吗?我已经为此工作了几个小时,无论我做什么,似乎都无法将输出写入文件。我是一名高中生,向我的老师寻求帮助,但她寄给我的东西没有帮助,我现在完全不知所措。我自己独立学习这门课程,希望stackoverflow家族的人能给我一些建议?这不是因为我没有尝试。我只是不知所措。提前谢谢你 /** * */ package distanceTraveledReport; import java.util.Scanner; import java.io.*; /** * @a
/**
*
*/
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");
}
}
}
/**
*
*/
包裹距离旅行报告;
导入java.util.Scanner;
导入java.io.*;
/**
*@作者杰林
*
*/
公共类距离TraveledReport{
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
//打印出我的名字和我正在做的项目
System.out.println(“杰林·安德森,这是我的长途旅行报告”);
//创建扫描仪
扫描仪=新的扫描仪(System.in);
double Vehicles speed=-1;//保持车辆的速度
int hoursTraveled=0;//保存车辆行驶的小时数
double distance traveled;//保存每小时的行驶距离
//while循环,输入验证
同时(车速<0)
{
System.out.println(“输入车辆速度(以英里/小时为单位)”;
车速=扫描器.nextDouble();
}
同时(行程小时数<1)
{
System.out.println(“输入车辆行驶的小时数”);
hoursTraveled=scanner.nextInt();
}
//顶部的标题和单独表格的行
System.out.println(“行驶的小时\t距离”);
System.out.println(“-------------------------------”;
{
//写下距离表
距离=1;
while(distance)traveled您必须意识到,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/StackOverFlow/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");
}
}
}
我建议将其用作文件写作的参考。标题不好。重写以总结您的特定技术问题。您实际上没有提出任何问题。本网站不用于一般性讨论和指导。本网站用于狭隘地关注可能有特定解决方案的特定技术问题的问题。具体内容是什么您在将数据写入文件方面有问题吗?关于这个主题的任何一个都没有解决您的具体问题吗?重复:@AnnaSmith希望这对您有所帮助。真的,请阅读有关文件编写者的内容,并阅读评论中的链接。非常感谢您!!@AnnaSmith欢迎您的帮助!
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