gpt4 book ai didi

java - 尝试使用 if block 中的变量写入 outfile.txt

转载 作者:行者123 更新时间:2023-12-01 10:49:39 24 4
gpt4 key购买 nike

我正在尝试使用 if 语句内的变量写入 outfile.txt。

当我编译它时,它一直说变量尚未初始化,而实际上它们已经初始化,一个作为字符串,另一个作为字符。

import java.io.*;

import java.util.Scanner;

public class program5{
// read string keyboard inputs
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
Scanner in = new Scanner(System.in);

public static void main(String[] args) throws IOException{
// read string keyboard inputs
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int i, gradeA_count = 0, gradeB_count = 0, gradeC_count = 0,
gradeD_count = 0, gradeF_count = 0, first_grade,
second_grade, third_grade, fourth_grade,
total_of_grades;

String remarks, course_name, semester, instructor_name, student_name;
char grade;
double average_grade;
int[] array_of_int_numbers = new int[10];
Scanner sc = new Scanner(System.in);

// set up a outfile to write to
Writer outfile = new BufferedWriter(new FileWriter("e:outfile.txt"));
// Write the class information to the outfile
outfile.write(" CMPS 161 Program Five, Fall 2015\n\n");
// separate top and next line for ease of sight
outfile.write(" -------------------------------- \n\n");
System.out.println("Enter the course name: ");

course_name=input.readLine();
System.out.println("Enter the Semester: ");
semester=input.readLine();
System.out.println("Enter the Instructor: ");
instructor_name=input.readLine();
outfile.write("Course: " +(String.format("%-8s",course_name))+ " Semester: " +(String.format("%-10s",semester))+ " Instructor: " +(String.format("%-12s",instructor_name))+ "\n\n");
outfile.write("Array Item Student Name T1 T2 T3 T4 Avg Grade Remarks\n\n");
outfile.write("=================================================================\n\n");

// array initialization
for (i = 0; i < 1; i++) array_of_int_numbers[i] = 0;

// prompt user for a value into the array
for (i = 0; i < 1; i++){
System.out.println("Enter a Students Name: ");
student_name=input.readLine();
System.out.println("Enter the first grade: ");
first_grade=sc.nextInt();
System.out.println("Enter the second grade: ");
second_grade=sc.nextInt();
System.out.println("Enter the third grade: ");
third_grade=sc.nextInt();
System.out.println("Enter the fourth grade: ");
fourth_grade=sc.nextInt();
total_of_grades = first_grade + second_grade + third_grade + fourth_grade;
average_grade = (double) total_of_grades / 4.0;
System.out.println("Your average grade is " +(String.format("%.1f",average_grade))+ "\n");

if (average_grade >= 90){
remarks = "Excellent";
grade = 'A';
gradeA_count++;
}else if (average_grade >= 80){
remarks = "Very Good";
grade = 'B';
gradeB_count++;
}else if (average_grade >= 70){
remarks = "Good";
grade = 'C';
gradeC_count++;
}else if (average_grade >= 60){
remarks = "Poor";
grade = 'D';
gradeD_count++;
}else if (average_grade >= 0){
remarks = "Fail";
grade = 'F';
gradeF_count++;
}

outfile.write("["+i+"]"+ student_name +
"" + first_grade +
"" + second_grade +
"" + third_grade +
""+ fourth_grade +
"%" + average_grade +
"" + grade +
"" + remarks + "\n");
}

for (i = 0; i < array_of_int_numbers.length; i++){
System.out.println("array_of_int_numbers["+(i+1)+ "] = "+array_of_int_numbers[i]);
}

System.out.println("Number of A's : " + gradeA_count);
System.out.println("Number of B's : " + gradeB_count);
System.out.println("Number of C's : " + gradeC_count);
System.out.println("Number of D's : " + gradeD_count);
System.out.println("Number of F's : " + gradeF_count);
outfile.close();
} // end program

} // end class

我是编码新手(只是我的第五个类(class)),这是我在大学的第一个学期,任何帮助将不胜感激。

我正在使用 JGrasp,因为我的教授需要它并使用 java 进行编码。

编辑:以下是抛出的异常:

program5.java:107: error: variable grade might not have been initialized
"" + grade +
^
program5.java:108: error: variable remarks might not have been initialized
"" + remarks + "\n");
^

最佳答案

在程序开始时,当您声明graderemarks时,您并没有初始化它。您必须将这些变量设置为默认值。

例如:

String remarks = "";
char grade = '0';

现在设置什么并不重要,因为稍后会更改。

现在,如果您知道 average_grade 永远不会为负数,那么您可以用简单的 else 替换最后一个 else if 语句。这就是为什么 Java 认为变量可能未初始化,因为 average_grade 有可能变为负值。

关于java - 尝试使用 if block 中的变量写入 outfile.txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33986643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com