gpt4 book ai didi

Java 程序读取文本文件并将其与用户输入进行比较

转载 作者:太空宇宙 更新时间:2023-11-04 13:28:51 24 4
gpt4 key购买 nike

我在打开和读取一个简单的 input.txt 文件到我在计算机科学类(class)之一中创建的 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

卢埃林马克

1 15 19 26 33 46

年轻的布莱恩

17 19 33 34 46 47

古哈奥雅纳

1 4 9 16 25 36

萧麦克

17 19 34 46 47 48

巴尔奇穆拉特

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 设为静态应该可以解决此问题。

关于Java 程序读取文本文件并将其与用户输入进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32383177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com