这编译但不运行..任何想法为什么?

时间:2014-11-12 20:38:00

标签: java

我在mac上运行它并且它在终端中编译但是在我输入java Lab9后它没有做任何事情。以下是我放入终端的内容

Last login: Wed Nov 12 15:29:48 on ttys000
Roberts-MacBook-Pro-3:~ robertsemulka$ cd /Users/robertsemulka/Desktop
Roberts-MacBook-Pro-3:Desktop robertsemulka$ javac Lab9.java
Roberts-MacBook-Pro-3:Desktop robertsemulka$ java Lab9

//编译但不运行

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

    public class Lab9
    {
    public static void main(String[] args)
    {
    //Create arrays
    int[] lotteryNum = new int[5];
    int[] userNum = new int[5];

    //Create Scanner and Random objects
    Scanner keyboard = new Scanner(System.in);
    Random randomGenerator = new Random();

    //Declare and initialize a variable to track correct digits
    int numCorrectDigits = 0;

    //TODO: Using a for-loop, generate 5 random numbers; store numbers in the lotteryNum array
    for(int i=0;i<5;i++)
    {
    int randomInt = randomGenerator.nextInt(10);
    lotteryNum[i] = randomInt;
    }


    //TODO: Using a for-loop, prompt user for 5 digits; store digits in userNum array
    for(int i=0;i<5;i++)
    {
    int UserInt = keyboard.nextInt();
    userNum[i] = UserInt;
    }


    //TODO: Using a for-loop, compare lotteryNum array and userNum array; increment variable numCorrectDigits
    //each time you find a matching pair of corresponding numbers
    for(int i=0;i<5;i++)
    {
    if(lotteryNum[i] == userNum[i])
    numCorrectDigits++;
    }



    //TODO: Display the winning number and the user's number
    //Example:
    //The winning number is: 12771
    //Your number is: 30781
    System.out.print("The winning number is: ");
    for(int i=0; i<5; i++)
    {
    System.out.println(lotteryNum[i]);
    }
    System.out.println();
    System.out.print("Your number is: ");
    for(int i=0; i<5; i++)
    {
    System.out.println(userNum[i]);
    }



    //Print a blank line
    System.out.println();

    //TODO: Display the number of correct digits
    //Example:
    //Number of correct digits is: 2



    //TODO: Complete the switch statement below to display results to the user
    switch(numCorrectDigits)
    {
    case 0:
    System.out.println("Your prize is: $0.00");
    case 1:
    System.out.println("Your prize is: $10.00");
    case 2:
    System.out.println("Your prize is: $100.00");
    case 3:
    System.out.println("Your prize is: $1000.00");
    case 4:
    System.out.println("Your prize is: $10,000.00");
    case 5:
    System.out.println("Your prize is: $100,000.00");
    break;

      Why does this compile but not 

    }

    System.out.println("\nPlease play again!");
    }
    }

Your program will generate a 5-digit lottery number, and then it will prompt the user to enter a guess in the form of a 5-digit number. Your program should then determine how many numbers the user guessed correctly and the user’s prize.
Your program must use an object of the Random class to generate five integers in the range 0 through 9 and must store these integers in an array.
Your program also needs to prompt the user to enter 5 integers in the range 0 through 9 and then needs to store these integers in an array.
So you will use two integer arrays in this program.
To determine how many numbers the user correctly guessed, your program needs to compare corresponding elements of both arrays in a for loop. Use a counter variable to keep track of how many numbers the user guessed correctly.
At the end of the program, output to the user the lottery number, the user’s guess, and the number of digits that the user guessed correctly.
Finally, your program needs to determine and display the user’s prize. Here is a chart that defines the prizes for this lottery:



Number of correct digits Prize

0 $0

1 $10

2 $100


3 $1,000

4 $10,000


5 $100,000
Here are some sample runs (user input is given in <>): 1.
Welcome to the Lottery Application!
Please enter a digit in the range 0-9: <3>
Please enter a digit in the range 0-9: <0>
Please enter a digit in the range 0-9: <7>
Please enter a digit in the range 0-9: <8>
Please enter a digit in the range 0-9: <1>
CS7 Lab 9
Fall 2014
The winning number is: 12771
Your number is: 30781
Number of correct digits: 2
Your prize is: $100.00!
Please play again!

1 个答案:

答案 0 :(得分:3)

程序正在等待您的输入。添加提示:

//TODO: Using a for-loop, prompt user for 5 digits; store digits in userNum array
for(int i=0;i<5;i++)
{
    System.out.println("Enter next digit:");
    int UserInt = keyboard.nextInt();
    userNum[i] = UserInt;
}