需要帮助我的java netbeans我的抽奖程序

时间:2014-01-02 05:08:14

标签: java netbeans

我是java编程的新手,目前正在制作这个抽奖活动。以下是该程序的示例输出。

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.

我已经编写了一些源代码,我想我差不多了。不幸的是,我遇到了票号的问题。票号必须生成5次,10位数。输出必须显示获胜者姓名以及他/她的票号,但票号不显示并声明它为空。这是我已经制作的语法。

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);
  }
}

注意:该程序可以为票号生成10位数的唯一号码,并与名称一起生成5次,但是我在选择票号获胜者方面存在问题。

这是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)

最后一项输出必须显示获胜者名称及其票号。我希望你能帮助我。请:)

3 个答案:

答案 0 :(得分:0)

看起来你遇到了范围问题。您可能也会对数据类型略感混淆,但让我们看看......

randomNumber在循环中被指定为作为长,但它在您的类中被声明为静态String 。当进入更广泛的范围(即,main方法内部)时,循环内的声明不适用,因此您不会从randomNumber获得任何相关值。

这本身就很奇怪 - 不同的数据类型对我来说没有意义。您也不需要static randomNumber声明 - 它只在main中使用过,所以您可以在那里声明它。

要解决这个问题,你必须改为输入随机输入的输出,并删除声明:

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

此外,作为代码气味,你有太多Scanner个实例;你应该只需要一个。我将这作为练习留给读者进行清理和重构。

答案 1 :(得分:0)

实际上你已经在循环外部创建了String类型的 randomNumber ,而在你内部创建了long类型的randomNumber ...这个long类型只能在循环中访问,而你调用的randomNumber是一个字符串类型,它是null,因为你没有为它分配任何值..

你需要做的是拥有一个 randomNumber 的数组并保存票号和最后一个,因为你得到胜利者[w]就像你获得randomNumber一样[ W]

答案 2 :(得分:-1)

与存储获胜者类似,也必须存储票号,并在打印输出时调用

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]));
    }
}