简体   繁体   English

需要帮助我的Java NetBeans Raffle程序

[英]Need help for my raffle program in java netbeans

I'm new to java programming and currently making this raffle program. 我是Java编程的新手,目前正在制作此抽奖程序。 Here is the sample output of the program. 这是程序的示例输出。

Welcome to raffle 2013!
The Prize is <10 million - 100 million> //this is random. I already made.
Ticket number: <Generating unique 10 digits numbers>

//these are the required information for the user.
Name:
Address:
Contact:
Birthday:

//The final output

The winner of <PRIZE> is <NAME>, Ticket number.

I already have written some source code and I think I'm almost there. 我已经写了一些源代码,我想我快要写了。 Unfortunately, I encountered a problem with the ticket number. 不幸的是,我遇到了机票号问题。 The ticket number must generate 5 times with 10 digits number. 票证号码必须生成10次数字5次。 The output must show the winner name together with his/her ticket number but the ticket number wasn't show and state that it is null. 输出中必须显示中奖者姓名和他/她的票证编号,但未显示票证编号,并指出该编号为空。 Here are the syntax I already made. 这是我已经做过的语法。

public class raffle2013 {

  //Title: Raffle 2013
  public static final int SIZE = 5;
  public static final int SIZE1 = 5;
  private static short x;
  private static String randomNumber;

  public static void main(String[] args) {
    String[] names;
    names = new String[SIZE];
    String[] winner;
    winner = new String[SIZE1];
    System.out.println("Welcome To Raffle 2013");

    long Low = 10000000;
    long High = 100000000;
    long randomPrize = (long) (Math.random() * High - Low) + Low;
    System.out.println("The Prize is " + " " + randomPrize);
    for (int a = 0; a < winner.length; a++) {
        long randomNumber = (long) (Math.random() * 9000000000L);
        System.out.println("Ticket number: " + randomNumber);
        Scanner scan = new Scanner(System.in);
        System.out.print("Name " + ":");
        winner[a] = scan.nextLine();
        Scanner scan2 = new Scanner(System.in);
        System.out.print("Address" + ":");
        names[x] = scan.nextLine();
        Scanner scan3 = new Scanner(System.in);
        System.out.print("Contact" + ":");
        names[x] = scan.nextLine();
        Scanner scan4 = new Scanner(System.in);
        System.out.print("Birthday" + ":");
        names[x] = scan.nextLine();
        System.out.println(" ");
    }
    Random random = new Random();
    int w = random.nextInt(SIZE1);
    System.out.println("The winner of" + " " + randomPrize + " " + "Million Peso(s)" + "is" + " " + winner[w] + "," + " " + "Ticket Number:" + " " + randomNumber);
  }
}

Note: the program can generate 10 digits unique numbers for the ticket number and generate it 5 times together with the names, however I have a problem of choosing the ticket number winner. 注意:该程序可以为票证编号生成10位数的唯一编号,并与名称一起生成5次,但是我在选择票证编号优胜者时遇到了问题。

Here is the output in java netbeans: 这是java netbeans的输出:

run:
Welcome To Raffle 2013
The Prize is  38375493
Ticket number: 1991318978
Name :a
Address:a
Contact:a
Birthday:a

Ticket number: 194313423
Name :b
Address:b
Contact:b
Birthday:b

Ticket number: 6017170047
Name :c
Address:c
Contact:c
Birthday:c

Ticket number: 274411236
Name :d
Address:d
Contact:d
Birthday:d

Ticket number: 6183250376
Name :e
Address:e
Contact:e
Birthday:e

The winner of 38375493 Million Peso(s)is a, Ticket Number: null<--- bug
BUILD SUCCESSFUL (total time: 18 seconds)

the last output must show the winner name together with his/her ticket number. 最后的输出必须显示获胜者的名字以及他/她的票号。 I hope you could help me. 我希望你能帮助我。 Please :) 请 :)

Looks like you're running into an issue with scope. 好像您遇到了范围问题。 You may also be slightly confused about data types, but let's see... 您可能对数据类型也有些困惑,但是让我们看看...

randomNumber is assigned as a long inside of your loop, but it is declared as a static String inside your class . randomNumber被分配循环中的long ,但在您的class中被声明为静态String The declaration inside of the loop is not applicable when coming to a wider scope (that is, inside of your main method), so you won't get any pertinent values from randomNumber . 循环内的声明不适用于更广的范围(即,在main方法内),因此您不会从randomNumber获得任何相关值。

That's weird in and of itself - the different data types don't make sense to me. 就其本身而言,这很奇怪-不同的数据类型对我而言毫无意义。 You also don't need the static declaration of randomNumber either - it's only ever used in main , so you could just declare it there. 您也不也需要randomNumberstatic声明-它仅在main使用过,因此您可以在那里声明它。

To get around that, you have to wrap the output from your random input instead, and drop the declaration: 为了解决这个问题,您必须包装来自随机输入的输出,并删除声明:

randomNumber = Long.toString(Double.valueOf(Math.random() * 9000000000L).longValue());

Also, as a code smell, you've got too many Scanner instances; 另外,由于代码的味道,您有太多的Scanner实例。 you should only really need one. 您只需要一个。 I leave that as an exercise to the reader to clean up and refactor. 我将其留给读者进行清理和重构的练习。

Actually you have created randomNumber of String type outside the loop , while inside you have created randomNumber of type long...this long type is only accessible within the loop and the randomNumber you are calling is the one which is string type and it is null because you havent assigned any value to it.. 实际上,您在循环外创建了String类型的randomNumber ,而在内部创建了long类型的randomNumber…此long类型只能在循环内访问,并且您要调用的randomNumber是字符串类型,并且为null因为您尚未为其分配任何值。

What you have to do is to have an array of the randomNumber and save the ticket numbers as well and in the last as you are getting the winner[w] the same way you get randomNumber[w] 您要做的是拥有一个randomNumber数组,并保存彩票编号,最后一次是获得中奖者[w],就像获得randomNumber [w]一样

Similar to storing the winner, the ticket number has to be stored as well, and recalled when the output is printed 与存储获奖者相似,票证编号也必须存储,并在打印输出时调出

import java.util.Random;
import java.util.Scanner;

public class raffle2013 {

    // Title: Raffle 2013

    public static final int SIZE = 5;
    public static final int SIZE1 = 5;
    private static short x;
    private static String randomNumber;

    public static void main(String[] args) {
        String[] names;
        names = new String[SIZE];
        String[] winner;
        winner = new String[SIZE1];
        long[] ticketNumber = new long[SIZE1];
        System.out.println("Welcome To Raffle 2013");

        long Low = 10000000;
        long High = 100000000;
        long randomPrize = (long) (Math.random() * High - Low) + Low;
        System.out.println("The Prize is " + " " + randomPrize);
        for (int a = 0; a < winner.length; a++) {
            long randomNumber = (long) (Math.random() * 9000000000L);
            System.out.println("Ticket number: " + randomNumber);
            Scanner scan = new Scanner(System.in);
            System.out.print("Name " + ":");
            winner[a] = scan.nextLine();
            ticketNumber[a] = randomNumber;
            Scanner scan2 = new Scanner(System.in);
            System.out.print("Address" + ":");
            names[x] = scan.nextLine();
            Scanner scan3 = new Scanner(System.in);
            System.out.print("Contact" + ":");
            names[x] = scan.nextLine();
            Scanner scan4 = new Scanner(System.in);
            System.out.print("Birthday" + ":");
            names[x] = scan.nextLine();
            System.out.println(" ");

        }

        Random random = new Random();
        int w = random.nextInt(SIZE1);

        System.out.println("The winner of" + " " + randomPrize + " "
                + "Million Peso(s)" + "is" + " " + winner[w] + "," + " "
                + "Ticket Number:" + " " + Long.toString(ticketNumber[w]));
    }
}

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

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