如何将PrintWriter用作布尔方法中的参数。

huangapple go评论74阅读模式
英文:

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(&quot;scoresInput.txt&quot;); // Making a new .txt file for input
Scanner inputFile = new Scanner(myFile); // Letting it read the file
PrintWriter outputFile = new PrintWriter(&quot;scoresOutput.txt&quot;); // 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(&quot;Score 1: &quot; + score1 + &quot;   &quot;);
outputFile.print(&quot;Score 2: &quot; + score2 + &quot;    &quot;);
outputFile.print(&quot;Score 3: &quot; + score3 + &quot;   \n\n\n&quot;);
score1 = inputFile.nextInt();
outputFile.println(&quot;Total number of groups processed: &quot; + totalGroups);
totalGroups++;
if(!validGroup(score1, score2, score3, PrintWriter)) {
outputFile.println(&quot;Group is invalid&quot;);
}
}
inputFile.close();
outputFile.close();
}
public static boolean validGroup(int score1, int score2, int score3, PrintWriter scoresOutput) {
boolean validGroup = true;
if(score1 &gt; 300 &amp;&amp; score1 &lt; 0) {
validGroup = false;
}
if(score2 &gt; 300 &amp;&amp; score2 &lt; 0) {
validGroup = false;
}
if(score3 &gt; 300 &amp;&amp; score3 &lt; 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.

huangapple
  • 本文由 发表于 2020年10月6日 07:35:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64217482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定