试图制作一个彩票计划,询问用户有多少人想进入彩票

时间:2015-06-21 18:59:53

标签: java

Shows the output for further help on my program I am attempting to make.程序应输出姓名,电子邮件和电话号码。到目前为止,我需要帮助我为每个用户提供抽奖...我需要帮助尝试获得一个中奖彩票并让每个人输入他们的数字。此外,在输入两个或更多人时存在问题。它只显示中奖彩票加上之前中奖彩票号码。

public static void main(String[] args) {

  Scanner input = new Scanner(System.in);
  boolean error = true;
  int customerAmount;
  String name, phone, email, lottery = "";
  String lotteryNum = "";
  String userGuess;
  double pickedNumber;
  String stringOfPickedNumber = "";
  int correct;
  int counter;

  customerAmount = Integer.parseInt(JOptionPane.showInputDialog("How many people would you like to enter?"));


  for (int i = 0; i < customerAmount; i++) {

    name = JOptionPane.showInputDialog("Please enter person " + (i + 1) + "'s name");

    phone = JOptionPane.showInputDialog("Please enter person " + (i + 1) + "'s phone number");

    email = JOptionPane.showInputDialog("Please enter person " + (i + 1) + "'s email");

    JOptionPane.showMessageDialog(null, "Please type in the output box below.");
    System.out.println("Guess person " + (i + 1) + "'s three digit number (e.g. 123): ");
    userGuess = input.next();

    System.out.println("");
    System.out.println("Name: " + name);
    System.out.println("Phone Number: " + phone);
    System.out.println("Email: " + email);


    //Identify the repeated steps and use a for loop structure
    //Input: Ask user to guess 3 digit number


    //Generate a 3-digit "lottery" number composed of random numbers
    for (counter = 1; counter <= 3; counter++) {
      pickedNumber = Math.random();

      if (pickedNumber < 0.1) {
        stringOfPickedNumber = "0";
      } else if (pickedNumber < 0.2) {
        stringOfPickedNumber = "1";
      } else if (pickedNumber < 0.3) {
        stringOfPickedNumber = "2";
      } else if (pickedNumber < 0.4) {
        stringOfPickedNumber = "3";
      } else if (pickedNumber < 0.5) {
        stringOfPickedNumber = "4";
      } else if (pickedNumber < 0.6) {
        stringOfPickedNumber = "5";
      } else if (pickedNumber < 0.7) {
        stringOfPickedNumber = "6";
      } else if (pickedNumber < 0.8) {
        stringOfPickedNumber = "7";
      } else if (pickedNumber < 0.9) {
        stringOfPickedNumber = "8";
      } else if (pickedNumber < 1) {
        stringOfPickedNumber = "9";
      }

      System.out.println(counter + ": " + stringOfPickedNumber);
      lotteryNum += stringOfPickedNumber;
    }

    //print the lottery number
    System.out.println("The winning lotto number for person " + (i + 1) + " was: " + lotteryNum);

    //convert each string to appropriate substrings
    //Compare the user's guess to the lottery number and report results
    correct = 0;
    for (counter = 0; counter <= 2; counter++) {
      if (lotteryNum.substring(counter, (counter + 1)).equals(userGuess.substring(counter, (counter + 1)))) {
        correct++;
      }
    }

    if (correct == 2 && !(lotteryNum.substring(0, 1).equals(userGuess.substring(0, 1)) && (lotteryNum.substring(2, 3).equals(userGuess.substring(2, 3))))) {
      System.out.println("One pair matched, congrats!");
    } else if (correct == 3) {
      System.out.println("All numbers matched - you WIN!");
    } else if (correct == 1) {
      System.out.println("One number matched, but you aren't a winner, sorry.");
    } else if (correct == 2) {
      System.out.println("Two numbers matched, but they weren't adjacent so you aren't a winner, sorry");
    } else {
      System.out.println("No numbers matched, sorry.");
    }

  }

}

}

0 个答案:

没有答案