首页 > 解决方案 > 如何在布尔方法中使用 PrintWriter 作为参数

问题描述

我正在尝试在我的布尔方法的参数中使用 PrintWriter 到我的输出文件。eclipse 中的错误告诉我 PrintWriter 无法解析为变量。错误发生在 if(!validgroup)... 我是一个非常新手的编码器,不久前开始。任何帮助或建议将不胜感激。


 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;
 }
 
 
 }

标签: java

解决方案


改变

if(!validGroup(score1, score2, score3, PrintWriter)) {

if(!validGroup(score1, score2, score3, outputFile)) {

此外,您实际上并没有使用scoresOutputin validGroup- 所以不清楚为什么添加它。


推荐阅读