基本Java程序调整帮助

时间:2016-10-05 15:17:09

标签: java

我的程序询问用户他们的名字,然后要求提供3个随机数的随机数。

当其中一个混洗添加到所需的数字(即31)时,随机播放停止。我需要发生这个程序只能读取最后的SHUFFLE。例如。

  

你想要多少次洗牌:3

     

10 + 11 + 10 = 31恭喜你是赢家!!

目前的输出是:

  

9 + 6 + 8

     

8 + 10 + 12

     

7 + 9 + 11

我需要帮助确保用户不能在其名称中添加非字母字符。我还需要能够在打印出数字之前打印出用户有多少次洗牌。

这是我的代码,

  `import java.util.InputMismatchException;
   import java.util.Scanner;
   import java.io.IOException;
   import java.util.Random;

 public class StringVariables {



public static void main(String[] args) throws NumberFormatException,
        IOException {

    // user inputs their name in this section
    Scanner user_input = new Scanner(System.in);
 //enter their first name
    String first_name;
    System.out.print("Enter Your First Name: ");
    first_name = user_input.next();
  //enter their last name
    String last_name;
    System.out.print("Enter Your Last Name: ");
    last_name = user_input.next();
 //full name printed together
    String full_name;
    full_name = first_name + " " + last_name;

    System.out.println(full_name + " Is Now Playing");

    // this is the shuffle portion as well as something to see if a number

    int numShuffles = -1;
    while (numShuffles < 0) {

        System.out.println("How many times do you want the numbers shuffled? ");

        try {
            numShuffles = user_input.nextInt();
        } catch (InputMismatchException inputException) {
            System.out.print("Please enter a valid number. \n");
            //this is the buffer that resets if the user types a letter instead of a number, or any other character
            user_input.next();
        }
    } 

    // here is going to be the loop for shuffles

    // we are now going to generate their random number and add a delay
    // after completing their name fields

    delay(3000);
    System.out
            .println(" You will be given a hand of 3 random numbers between 7-13"
                    + "\n you will be drawn a the number of shuffles as you entered above ");

    delay(2000);
    System.out
            .println(" Then, the computer will add the random numbers and  if it is equal to 31, you win.");

    /*
     * end of explanation of the game, next i will create a new screen with
     * the user's name and numbers
     */

    delay(4000);
    // printing 25 blank lines
    for (int i = 0; i < 25; i++)
        System.out.println(" ");


    System.out.println("User playing: " + full_name);

    System.out.println("Your lucky numbers are...");

    // random number generator

    Random random = new Random();



    while (true) {

        // the shuffle loop
        boolean isWinner = false;
        for (int i = 0; i < numShuffles; i++) {
            int num1 = 7 + random.nextInt(7);
            int num2 = 7 + random.nextInt(7);
            int num3 = 7 + random.nextInt(7);

            System.out.println(num1 + " + " + num2 + " + " + num3 + " = "
                    + (num1 + num2 + num3));

  // adding the numbers together          

            if (num1 + num2 + num3 == 31) {
                isWinner = true;
                System.out
                        .println("Congratulations !! You are the Lucky Winner !!!!");
                break;
                //if you loose every shuffle
            }
        }
        if (!isWinner)
            System.out.println("Better Luck Next Time");

        // play again prompt
        System.out
                .println(" Do you want to play again? (If you do enter y or yes) \n To exit press any other key ");
        String input = user_input.next();
        if (!"y".equalsIgnoreCase(input) &&  !"yes".equalsIgnoreCase(input)) {
            break;
        }
    }

    // if pressed y or yes the program will run again with the same number of shuffles entered from before
    user_input.close();
}

// delay field

public static void delay(int millis) {
    try {
        Thread.sleep(millis);
    } catch (InterruptedException exp) {

        // delay field

    }
}
 }`

1 个答案:

答案 0 :(得分:1)

Arraylist numberStore = new Arraylist();
for (int i = 0; i < numShuffles; i++) {
    int num1 = 7 + random.nextInt(7);
    int num2 = 7 + random.nextInt(7);
    int num3 = 7 + random.nextInt(7);

    System.out.println(num1 + " + " + num2 + " + " + num3 + " = " + (num1 + num2 + num3));

    numberStore.add(num1 + num2 + num3);

}

int lastNumber = (numberStore.size() - 1);
if (lastNumber == 31) {
        isWinner = true;
        System.out
                .println("Congratulations !! You are the Lucky Winner !!!!");
        break;
        //if you loose every shuffle
}
  

确保只有最后一个随机播放才能被视为赢家或输家。

因为你正在for循环中初始化num1,num 2 ...变量,所以那些变量的范围是for循环。我建议如果你想确保只能判断一组数字,那么你就可以将范围移出循环。将总数添加到数组将允许您选择您想要判断的数量。

在对输入进行消毒时,您可以使用util.Scanner为您做大部分操作,并且对正则表达式有一点了解:

while (!scan.hasNext("[A-Za-z]+")) {
    System.out.println("Nope, that's not it!");
    sc.next();
}

这将停止您的扫描仪,允许输入任何字母字符,您可以通过this tool了解有关正则表达式的更多信息