简体   繁体   English

错误:找不到符号编译(初级java)

[英]Error: cannot find symbol compiling (elementary java)

I'm working on a programming project for my intro class.我正在为我的介绍类编写一个编程项目。 I have a code that I'm trying to compile, but I'm having a hard time getting it to work after I added the PrintWriter.我有一个要编译的代码,但是在添加 PrintWriter 后很难让它工作。 All was running well until I tried to print to a text file.在我尝试打印到文本文件之前,一切都运行良好。 Can someone help me figure out how to get it to run?有人可以帮我弄清楚如何让它运行吗? (Also, if you find any errors in my logic/layout/whatever, try to contain it! I still want to debug the program myself, I just can't do that until it runs :) (另外,如果你在我的逻辑/布局/任何东西中发现任何错误,请尝试包含它!我仍然想自己调试程序,在它运行之前我不能这样做:)

Attempt: (so far)尝试:(到目前为止)

import java.util.Scanner; //import scanner
import java.util.Random; //import randomizer
import java.io.*; //needed for throws clause

public class randomLottery
{
    public static void main(String[] args) throws IOException
    {
        String fullName;

        Scanner keyboard = new Scanner( System.in );

        //so we can generate random numbers
        Random rand = new Random();

        //declare a constant number of numbers
        final int LOTTERY_NUMBERS = 5;

        //Retrieve names
        System.out.print("Please enter a first and last name for lottery "
                         + "entry (type 'quit' to end): ");
        fullName = keyboard.nextLine();

        while(!fullName.contains(" "))
        {
            System.out.print("Please enter BOTH a first and last name." 
                             + " Try Again: ");
            fullName = keyboard.nextLine();
        }   

        while(!fullName.contains("quit"))
        {
            //separate first/last name
            String[] parts = fullName.split(" ");
            String firstName = parts[0];    
            String lastName = parts[1];

            //Open the file
            PrintWriter outputFile = new PrintWriter("LotteryEntrants.txt");

            //Print the name onto the file
            outputFile.print(lastName + ", " + firstName + ": ");

            int number;
            for (number = 1; number <= LOTTERY_NUMBERS; number++)
            {
                if (number == LOTTERY_NUMBERS)
                {
                    int lotteryNumber = rand.nextInt(100) + 1;
                    outputFile.println(lotteryNumber);
                }
                else
                {
                    int lotteryNumber = rand.nextInt(100) + 1;
                    outputFile.print(lotteryNumber + ", ");
                }
            }

            //get the next name
            System.out.print("Please enter BOTH a first and last name." 
                              + " Try Again: ");
            fullName = keyboard.nextLine(); 
        }

        //Winning Lottery Numbers
        outputFile.print("The winning numbers are: ");

        int winning;
        for (winning = 1; winning <= LOTTERY_NUMBERS; winning++)
        {
            if (winning == LOTTERY_NUMBERS)
                {
                    int lotteryNumber = rand.nextInt(100) + 1;
                    outputFile.print(lotteryNumber);
                }
            else
                {
                    int lotteryNumber = rand.nextInt(100) + 1;
                    outputFile.print(lotteryNumber + ", ");
                }
        }
    outputFile.close();
    }
}
PrintWriter outputFile = new PrintWriter("LotteryEntrants.txt");

Should be outside (before) the while loop.应该在 while 循环之外(之前)。 Having it inside the loop means it is not in the scope of your other uses of outputFile after the while loop.将它放在循环中意味着它不在 while 循环之后的 outputFile 其他用途的范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM