在switch中使用enum,并将结果返回给另一个类

时间:2016-01-20 04:19:11

标签: java enums

我没有过多地使用枚举,但是他们假设在这个作业中使用了(即使你不需要它),但是我在遇到麻烦的地方我想要拨打一个开关使用枚举,我从另一个类调用此开关。

我想做的是,我正在使用计数器来计算乐透彩票和中奖彩票之间匹配的数量,并根据该计数使用枚举作为开关语句,其中枚举是用作整数。

package lotto;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;


public class Lottery {

static ArrayList<Ticket> ticketList = new ArrayList(); 

public static void scanFile() throws FileNotFoundException 
{
    String fileName;
    int[] WinningNumbers = new int[6];      
    Scanner scan = new Scanner(System.in);

    System.out.println("enter a string");
    fileName = scan.nextLine();                         //scans file name which is default "input.txt"

    System.out.println("Enter the winning Lottery Numbers");
    for(int i =0; i<WinningNumbers.length;i++)
    {
        WinningNumbers[i] = scan.nextInt();             //scans winning numbers from console input
    }
    scan.close();                                       //closes scanner to avoid leakage

    int NumberofTickets;
    File file = new File(fileName);
    Scanner scanInput = new Scanner(file);  

    NumberofTickets = scanInput.nextInt();              //scans number of tickets
    scanInput.nextLine();                               //skips white space

    String[] PlayersFirstName = new String[NumberofTickets];    //creates array of players depending on how 
    //String[] PlayersLastName = new String[NumberofTickets];   //many tickets there are
    for(int i=0; i < NumberofTickets;i++)
    {
        int[] ticketnumbers = new int[6];               //creates a new array for every individuals ticketholder's numbers
        scanInput.nextLine();                           //skips white space

        PlayersFirstName[i] = scanInput.nextLine();         //scans players names
        //PlayersLastName[i] = 
        scanInput.nextLine();                           //skips white space

        for(int j = 0 ; j<6;j++)
        {
            ticketnumbers[j] = scanInput.nextInt();     //scans players numbers
        }
        if(i != NumberofTickets-1)                      //fixes out of bounds when scanning file
        scanInput.nextLine();

        ticketList.add(new Ticket(PlayersFirstName[i],ticketnumbers));  //adds ticket to a arrayList of tickets
    }
    checkTickets(WinningNumbers,NumberofTickets);       //calls method to check winning numbers 
    scanInput.close();                                  //to ticketholder numbers
}


public static void checkTickets(int[] winningNumbers, int NumberofTickets)
{
 int[] winnersMatchedNumbers = new int[NumberofTickets];

  for(int i =0; i<NumberofTickets; i++)
  {
    int counter = 0;

    for(int j = 0; j < 6;j++)                       //i use 3 for loops in order to check 1 winning 
    {                                               //number to every number on the ticketholders number array
       for(int k = 0; k<6;k++)                      //assuming that there are no duplicate numbers 
       {
           if(winningNumbers[j] == ticketList.get(i).getNumbers()[k])   //checks if winning numbers matches numbers in ticketholders number array
           {
               counter++;                           //counts everytime numbers are matched to winning numbers
           }
       }
    }
        winnersMatchedNumbers[i] = counter;         //puts counter into specific ticketholders array position
  }
    Winners(winnersMatchedNumbers,NumberofTickets); //calls method to print out winners!
}

public static void Winners(int[] matchedNumbers, int NumberofTickets)
{

    for(int i = 0; i<NumberofTickets;i++)
    {
       System.out.println(ticketList.get(i).getTicketName()+ " matched "+ matchedNumbers[i]+" and won $"+ Winner.getWinnings(matchedNumbers[i]));

      if(matchedNumbers[i] == 3)
           System.out.print(Winner.WINNER);
       else if(matchedNumbers[i] == 4)
           System.out.print(Winner.DECENTWINNER);
       else if(matchedNumbers[i] == 5)
           System.out.print(Winner.BIGWINNER);
       else if(matchedNumbers[i] == 6)
           System.out.print(Winner.JACKPOT);
       else
           System.out.print("0");
       System.out.println(); 
    }
}


public static void main(String[] args) {    //Main method
    // TODO Auto-generated method stub
    try {
        scanFile();                         //calls scanFile method to begin scanning process
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}
class Ticket {

private String ticketName;
private int[] personNumber = new int[6];

public Ticket(String Name,int[] ticketNumbers)  //constructor of class Ticket
{                                           //reads in a name and ticketnumber sent from scanFile()
     ticketName = Name;
     personNumber = ticketNumbers;  
}
public String getTicketName()
{
    return ticketName;
}

public int[] getNumbers()   
{
    return personNumber;
}


}
enum Winner{

 WINNER(3),DECENTWINNER(4),BIGWINNER(5),VERYBIGWINNER(6);

 private int winnerSize;

 Winner(int winnerSize){
     this.winnerSize = winnerSize;
 }
 public int getWinnerSize(){
     return winnerSize;
 }
 public int getWinnings(int count)
    {

        int winningsAmount;
        switch(this)                    //switch statement used to chose how much a person has won 
        {                               //depedning on how many numbers they matched
        case WINNER:  winningsAmount = 10;
                    break;
        case DECENTWINNER:  winningsAmount = 100;
                    break;
        case BIGWINNER:  winningsAmount = 10000;
                    break;
        case VERYBIGWINNER:  winningsAmount = 1000000;
                    break;
        default: winningsAmount = 0;
                    break;
        }
        return winningsAmount;
    }

}

1 个答案:

答案 0 :(得分:0)

这最终使一切运转良好,谢谢所有帮助过的人!

System.out.print(Winner.getWinnerWinnings(Winner.WINNER)); //this is called in the other class 

public static int getWinnerWinnings(Winner win){
     int winnings = getWinnings(win);
     return winnings;
 }
 public static int getWinnings(Winner Win)
    {

        int winningsAmount;
        switch(Win)