【问题标题】:PrintStream error in main "variable output is already defined in main"主“变量输出已在主中定义”中的 PrintStream 错误
【发布时间】:2013-11-21 01:28:36
【问题描述】:

我目前正在尝试使用 PrintStream 方法使我的代码生成一个输出文件。我的教科书建议我在我的 main 中使用这行特定的代码:

    PrintStream output = new PrintStream(new File("results.txt"));

但是,当我输入这行代码时,Java 给了我以下错误:

    Personality.java:17: error: variable output is already defined in method main(String[])
    PrintStream output = new PrintStream(new File("results.txt"));
                ^
    Personality.java:23: error: cannot find symbol
        output.println();
              ^

我的主要方法目前是这样的:

    public class Personality {
public static void main (String[] args)  throws FileNotFoundException   {
    Scanner input = new Scanner(System.in);
    intro();
    Scanner output = asksForFile(input);
    PrintStream output = new PrintStream(new File("results.txt"));
  while(output.hasNextLine()){
        int[] aCounts = new int[4];
        int[] bCounts = new int[4];
        String name = output.nextLine();
        String data = output.nextLine();
        output.println();
        System.out.print(name + ": ");
        int[] percentB = numberOfAnswers(name, data, aCounts, bCounts);
        output.print(Arrays.toString(percentB));
     output.print(" = ");
        output.println(determineType(percentB));    
    }       
}

我从错误中猜测,我不能在一个方法中定义两次输出,但是如果我没有定义我的输出,我的程序怎么知道那个符号是什么?另外,如果我已经在 main 中定义了输出,我还能调用什么来使 PrintStream 工作,同时保持程序的其余部分也运行?

我将 main 中的“输出”变量重命名为扫描仪,但我收到了这个新错误:

    Personality.java:34: error: cannot find symbol
    output.println("This program processes a file of answers to the");
    ^
      symbol:   variable output
  location: class Personality

这就是我现在的整个代码的样子:

import java.util.*;
import java.io.*;

public class Personality {
    public static void main (String[] args)  throws FileNotFoundException   {
        Scanner input = new Scanner(System.in);
        intro();
        Scanner scanner = asksForFile(input);
        PrintStream output = new PrintStream(new File("results.txt"));

      while(scanner.hasNextLine()){
            int[] aCounts = new int[4];
            int[] bCounts = new int[4];
            String name = scanner.nextLine();
            String data = scanner.nextLine();
            output.println();
            System.out.print(name + ": ");
            int[] percentB = numberOfAnswers(name, data, aCounts, bCounts);
            output.print(Arrays.toString(percentB));
         output.print(" = ");
            output.println(determineType(percentB));    
        }       
    }

   //Introduces the program
    public static void intro()  {
        output.println("This program processes a file of answers to the");
        output.println("Keirsey Temperament Sorter.  It converts the");
      output.println("various A and B answers for each person into");
      output.println("a sequence of B-percentages and then into a");
        output.println("four-letter personality type.");
        output.println();       
    }

    //Asks for input file
    public static Scanner asksForFile(Scanner input) throws FileNotFoundException   {
        output.print("input file name? ");
        String filename = input.nextLine();
        return new Scanner(new File(filename));

    }

   //This while loop puts counts inside arrays
    public static int[] numberOfAnswers(String name, String data, int[] aCounts, int[] bCounts)  throws FileNotFoundException   {
        data = data.toLowerCase();
        for (int i = 0; i < data.length(); i++) {
            int x = ((i % 7) + 1) / 2;
            if (data.charAt(i) == 'a'){
                aCounts[x]++;
            } else if(data.charAt(i) == 'b'){
                bCounts[x]++;
            }
        }
        return percentB(aCounts, bCounts);
    }

    public static void printOutcome(int[] aCounts, int[] bCounts){
        String[] ratios = new String[4];
        for(int i = 0; i < 4; i++){
            String temp = aCounts[i] + "A-" + bCounts[i] + "B";
            ratios[i] = temp;
        }
        output.println(Arrays.toString(ratios));
    }

    public static int[] percentB(int[] aCounts, int[] bCounts){
        int[] percentB = new int[4];
        for(int i = 0; i < 4; i++){
            double percent = (double) bCounts[i] / (aCounts[i] + bCounts[i]);
            percentB[i] = (int) Math.round(percent * 100);
        }
        return percentB;    
    }

    public static String determineType(int[] percentB){
        String sub50 = "ESTJ";
        String sup50 = "INFP";
        String type = "";
        for(int i = 0; i < 4; i++){
            if(percentB[i] > 50){
                type += sup50.charAt(i);
            } else if(percentB[i] < 50){
                type += sub50.charAt(i);
            } else {
                type += "X";
            }
        }
        return type;
    }

}

【问题讨论】:

  • 注意:Java 与 JavaScript 相关。
  • 对不起!我的代码中仍然出现错误!你知道为什么吗?
  • 密切注意错误信息 - 它们是正确的。当前发布的表单不起作用,因为PrintStream outputmain 方法中的局部变量,因此[用于引用变量的符号]找不到 在intro 方法中。
  • (我会让intro 采取一个 PrintStream 对象作为参数。)

标签: java compiler-errors main outputstream printstream


【解决方案1】:

代码中有两个同名的变量,这是不允许的。更改这些变量名称之一并重构代码:

Scanner output = asksForFile(input);
PrintStream output = new PrintStream(new File("results.txt"));

重构版本

public class Personality {
    public static void main (String[] args)  throws FileNotFoundException   {
        Scanner input = new Scanner(System.in);
        intro();
        Scanner scanner = asksForFile(input);
        PrintStream output = new PrintStream(new File("results.txt"));

        while(scanner.hasNextLine()){
            int[] aCounts = new int[4];
            int[] bCounts = new int[4];
            String name = scanner.nextLine();
            String data = scanner.nextLine();
            output.println();
            System.out.print(name + ": ");
            int[] percentB = numberOfAnswers(name, data, aCounts, bCounts);
            output.print(Arrays.toString(percentB));
            output.print(" = ");
            output.println(determineType(percentB));    
        }       
    }
}

注意 这段代码 sn-p 中有几个方法没有提供源代码。考虑到这些未知因素,此版本可能需要额外的重构。

【讨论】:

  • 我按照你说的重构了代码,但我仍然遇到错误!我编辑了主要问题以显示我遇到了什么错误。
猜你喜欢
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
  • 1970-01-01
  • 2014-10-09
  • 1970-01-01
  • 2022-12-10
  • 2020-11-30
  • 2013-03-26
相关资源
最近更新 更多