英文:
How to use PrintWriter as a parameter in a boolean method
问题
import java.io.*;
import java.util.*;
public class BowlingScores {
public static void main(String args[]) throws IOException{
File myFile = new File("scoresInput.txt"); // Making a new .txt file for input
Scanner inputFile = new Scanner(myFile); // Letting it read the file
PrintWriter outputFile = new PrintWriter("scoresOutput.txt"); // Creating the .txt output file
int score1, score2, score3, totalGroups = 1;
score1 = inputFile.nextInt();
while(score1 != -999) {
score2 = inputFile.nextInt();
score3 = inputFile.nextInt();
outputFile.print("Score 1: " + score1 + " ");
outputFile.print("Score 2: " + score2 + " ");
outputFile.print("Score 3: " + score3 + " \n\n\n");
score1 = inputFile.nextInt();
outputFile.println("Total number of groups processed: " + totalGroups);
totalGroups++;
if(!validGroup(score1, score2, score3, outputFile)) {
outputFile.println("Group is invalid");
}
}
inputFile.close();
outputFile.close();
}
public static boolean validGroup(int score1, int score2, int score3, PrintWriter scoresOutput) {
boolean validGroup = true;
if(score1 > 300 && score1 < 0) {
validGroup = false;
}
if(score2 > 300 && score2 < 0) {
validGroup = false;
}
if(score3 > 300 && score3 < 0) {
validGroup = false;
}
return validGroup;
}
}
英文:
I am trying to use a PrintWriter to my output file in my parameter for my boolean method. The error in eclipse tells me that PrintWriter cannot be resolved to a variable. The error occurs at if(!validgroup)... I am a very novice coder and started not too long ago. Any help or suggestions would be greatly appreciated.
import java.io.*;
import java.util.*;
public class BowlingScores {
public static void main(String args[]) throws IOException{
File myFile = new File("scoresInput.txt"); // Making a new .txt file for input
Scanner inputFile = new Scanner(myFile); // Letting it read the file
PrintWriter outputFile = new PrintWriter("scoresOutput.txt"); // Creating the .txt output file
int score1, score2, score3, totalGroups = 1;
score1 = inputFile.nextInt();
while(score1 != -999) {
score2 = inputFile.nextInt();
score3 = inputFile.nextInt();
outputFile.print("Score 1: " + score1 + " ");
outputFile.print("Score 2: " + score2 + " ");
outputFile.print("Score 3: " + score3 + " \n\n\n");
score1 = inputFile.nextInt();
outputFile.println("Total number of groups processed: " + totalGroups);
totalGroups++;
if(!validGroup(score1, score2, score3, PrintWriter)) {
outputFile.println("Group is invalid");
}
}
inputFile.close();
outputFile.close();
}
public static boolean validGroup(int score1, int score2, int score3, PrintWriter scoresOutput) {
boolean validGroup = true;
if(score1 > 300 && score1 < 0) {
validGroup = false;
}
if(score2 > 300 && score2 < 0) {
validGroup = false;
}
if(score3 > 300 && score3 < 0) {
validGroup = false;
}
return validGroup;
}
}
答案1
得分: 1
将
if(!validGroup(score1, score2, score3, PrintWriter)) {
改为
if(!validGroup(score1, score2, score3, outputFile)) {
另外,你在 validGroup
函数中实际上并没有使用 scoresOutput
,所以不太清楚你为什么添加了它。
英文:
Change
if(!validGroup(score1, score2, score3, PrintWriter)) {
to
if(!validGroup(score1, score2, score3, outputFile)) {
Also, you aren't actually using scoresOutput
in validGroup
- so it isn't clear why you added it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论