我在打开和读取一个简单的input.txt文件到我在计算机科学课程中创建的Java程序时遇到了一些麻烦(该程序的基本前提是从文件中获取彩票数据,例如玩家姓名和彩票号码,并将其与作为输入提供的用户号码进行比较)。首先,当我编译并尝试运行程序(Eclipse IDE)时,它看不到input.txt文件,即使它位于同一目录中。我必须在命令行中输入"src/Assignment1/input.txt"才能正确找到文件。我查看了Java的Path实用程序,看看是否可以解决这个问题,但不确定如何实现该代码,或者是否有更简单的方法。
同样,当程序找到文件时,我让我的readFile方法将文本文件中的第一个数字作为售出的门票数量,然后创建一个链表来保存每个人的数据。我在readFile方法中的第二个嵌套for循环中得到编译错误,如下所示:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type lottery is accessible. Must qualify the allocation with an enclosing instance of type lottery (e.g. x.new A() where x is an instance of lottery).
at Assignment1.lottery.readFile(lottery.java:89)
at Assignment1.lottery.main(lottery.java:214)
我已经提供了下面的input.txt和我的代码的副本,任何指导将不胜感激!
5 Llewellyn马克1 15 19 26 33 46
年轻布莱恩17 19 33 34 46 47
奥雅纳古哈
1 4 9 16 25 36
Siu马克斯
17 19 34 46 47 48
Balci Murat
5 10 17 19 34 47
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
public class lottery {
// Enumeration method that holds constants for how many matches correlate to how much in winnings
public enum PayOut
{
MATCHTHREE(10), MATCHFOUR(100), MATCHFIVE(10000), MATCHSIX(1000000);
private int winnings;
public int getval()
{
return winnings;
}
private PayOut(int amount)
{
this.winnings = amount;
}
}
// Method call to create linked list with purchaser's data in each node
static LinkedList<PurchaserList> purchasers = new LinkedList<>();
// Data structure for purchaser information, contained in each node in linked list
public class PurchaserList
{
public String lastName;
public String firstName;
public int[] numbers;
public PurchaserList (String last, String first, int[] nums)
{
lastName = last;
firstName = first;
numbers = nums;
}
public String getLastName()
{
return lastName;
}
public String getFirstName()
{
return firstName;
}
public int[] getNumbers()
{
return numbers;
}
}
// Method to open and read in data from input file
public static void readFile (String file) throws IOException
{
File fileName = new File(file);
try
{
Scanner in = new Scanner(fileName);
int numTickets = in.nextInt();
for(int i = 0; i < numTickets; i++)
{
String lastName = in.next();
String firstName = in.next();
int[] numbers = new int[6];
for(int j = 0; j < 6; j++)
{
numbers[j] = in.nextInt();
}
PurchaserList current = new PurchaserList(lastName, firstName, numbers);
purchasers.add(i, current);
}
in.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
}
public static void compareNumbers(int[] winningNumbers)
{
// Creates array to hold winning numbers
List<Integer> winningNums = new ArrayList<>(); //creates a list to store all the winning numbers
// For loop to store winning numbers in prior array
for(int num : winningNumbers)
{
winningNums.add(num);
}
//For loop to go through each purchaser in the linked list and get their lottery numbers
for (PurchaserList purchaser : purchasers)
{
// Generating list that stores all the numbers from the current purchaser
List<Integer> playerNums = new ArrayList<>();
// For loop to add numbers to the list
for(int num : purchaser.getNumbers())
{
playerNums.add(num);
}
//Converts list in to a set
Set<Integer> similar = new HashSet<>(playerNums);
//Compares set of winning numbers to purchaser's numbers, keeps similar values
similar.retainAll(winningNums);
//Calls enum method to look for winning amount based on the numbers matched, returns enumeration
PayOut matchesFound = matches(similar.size());
//Only display winnings if matches are 3 or greater
if(matchesFound != null)
{
displayPrices(matchesFound, purchaser.getFirstName(), purchaser.getLastName(), similar.size());
}
}
}
public static PayOut matches(int num)
{
PayOut numMatches;
if(num == 3)
{
numMatches = PayOut.MATCHTHREE;
}
else if(num == 4)
{
numMatches = PayOut.MATCHFOUR;
}
else if(num == 5)
{
numMatches = PayOut.MATCHFIVE;
}
else if(num == 6)
{
numMatches = PayOut.MATCHSIX;
}
else
{
return null;
}
return numMatches;
}
private static void displayPrices(PayOut numMatches, String first, String last, int matches)
{
switch(numMatches)
{
case MATCHTHREE:
System.out.printf("%n%s %s matched %d numbers and won $%d. %n", first, last, matches, PayOut.MATCHTHREE.getval());
break;
case MATCHFOUR:
System.out.printf("%n%s %s matched %d numbers and won $%d. %n", first, last, matches, PayOut.MATCHFOUR.getval());
break;
case MATCHFIVE:
System.out.printf("%n%s %s matched %d numbers and won $%d.%n", first, last, matches, PayOut.MATCHFIVE.getval());
break;
case MATCHSIX:
System.out.printf("%n%s %s matched %d numbers and won $%d. %n", first, last, matches, PayOut.MATCHSIX.getval());
break;
default:
System.out.println("None of the players matched any numbers %n");
break;
}
}
// Main method
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
// Asking user for input file
System.out.println("Enter the name of the file with the ticket data: ");
String fileName = scan.nextLine();
//Method call to open and read in data from input file
readFile(fileName);
// Creates array to store winning lottery numbers
int[] winningNumbers = new int[6];
// Asking user for winning lottery numbers, in ascending order
System.out.println("Enter the winning Lottery numbers (in ascending order separated by spaces): ");
// For loop to scan in winning lottery numbers to array
for(int i = 0; i < 6; i++)
{
winningNumbers[i] = scan.nextInt();
}
//Method call to compare winning numbers to purchaser's numbers
compareNumbers(winningNumbers);
// Close input file
scan.close();
}
}
PurchaserList
类不是静态的。因此,它必须像lotteryInstance.new PurchaserList()
一样被访问(因为它不是静态的,它属于Lottery实例)。
所以,使PurchaserList
静态应该解决这个问题。