使用 Excel 資料進行抽獎

使用 Excel 資料進行抽獎

我擁有的是一個 Excel 電子表格,其中包含用戶的名字、姓氏以及他們上個月正確回答每日瑣事問題的次數。我想要做的是根據抽獎系統選擇一名獲勝者,用戶每正確回答一個問題就可以獲得一個條目。我有一種用 Java 實作的方法,但如果可能的話,我想用 Excel 實作。這是我的 Java 程式碼(如果有助於解釋的話):

public static void main(String[] args) throws FileNotFoundException {
    File file = new File("TriviaParticipantsList.txt");
    Scanner scanner = new Scanner(file);
    int x = 0;
    int j = 0;
    int i = 0;

    while(scanner.hasNextLine())
    {
        String line = scanner.nextLine();
        Scanner scan = new Scanner(line);

        i = scan.nextInt();
        String name = scan.nextLine();
        if(line.charAt(0)==0)
        {
            //do nothing if no questions were answered
        }
        else
        {
            for(j=0; j<i; j++)
            {
                System.out.print(j+x); //Print out "ticket number"
                System.out.println(" " + name); //Print out owner name of ticket
            }
        }
        x=x+i;
    }
    scanner.close();
    System.out.println(x); //Verify correct number of entries
    int winner = (int) (Math.random()*x); //Select random number based on number of entries
    System.out.println(winner); //Display value, look through list to find who number belongs to!
}

任何幫助是極大的讚賞!

答案1

我可以想像(如“我沒有嘗試過”)正確答案的累積總和與隨機數的查找相結合可能會成功。

Name | Correct | Sum
P1        3       3
P2        2       5
P3        4       9

創建一個帶有隨機數字的單元格,也許還有一個按鈕來觸發重新計算,或者讓它用隨機數的公式或類似的東西填充單元格。

然後你應該能夠進行返回「P2」的查找,例如如果你畫了數字4。否則,您可能需要使用輔助列來解決這個問題。

當然,隨機數必須從 1 到累積和的最大值。

希望能有所幫助;-)

相關內容