为了让我的Java代码能够识别Math.random()方法,我需要做些什么?

时间:2016-03-05 15:23:42

标签: java

每次我尝试运行涉及Math.random()方法的代码时,我总是收到错误,找不到符号。我不明白为什么,因为正如有人说我之前提到的Math类已经在java.lang包中,所以我不明白。如果任何人可以给我一些我可能做错的想法或者解决这个问题的任何事情,我会很感激,因为直到我弄清楚这一点我无法运行/或创建任何涉及此方法的程序。
这是一个由于错误而无法编译的代码示例:

import java.util.Scanner;

public class LotteryUsingStrings {
  public static void main(String[] args) {
    // Generate a lottery as a two-digit string
    String lottery = "" + (int)(Math.random() * 10)
      + (int)(Math.random() * 10);

    // Prompt the user to enter a guess
    Scanner input = new Scanner(System.in);
    System.out.print("Enter your lottery pick (two digits): ");
    String guess = input.nextLine();

    // Get digits from lottery
    char lotteryDigit1 = lottery.charAt(0);
    char lotteryDigit2 = lottery.charAt(1);

    // Get digits from guess
    char guessDigit1 = guess.charAt(0);
    char guessDigit2 = guess.charAt(1);

    System.out.println("The lottery number is " + lottery);

    // Check the guess
    if (guess.equals(lottery))
      System.out.println("Exact match: you win $10,000");
    else if (guessDigit2 == lotteryDigit1
          && guessDigit1 == lotteryDigit2)
      System.out.println("Match al digits: you win $3,000");
    else if (guessDigit1 == lotteryDigit1
          || guessDigit1 == lotteryDigit2
          || guessDigit2 == lotteryDigit1
          || guessDigit2 == lotteryDigit2)
      System.out.println("Match one digit: you win $1,000");
    else 
      System.out.println("Sorry, no match");
  }
}

1 个答案:

答案 0 :(得分:0)

它编译好我。

您是否正在使用Eclipse进行开发?也许您已经将类路径或编译器设置配置错误了?

问候 费边

PS:您是否尝试重新启动Eclipse或重新构建项目?