我已经定义符号时找不到符号

时间:2020-08-01 14:33:32

标签: java compiler-errors cannot-find-symbol

我正在制作“猜数字”游戏,这是我的游戏代码。

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

public class Guess {

    public static void main(String[] args) {
        int guess, diff;
        Random random = new Random();
        Scanner in = new Scanner(System.in);
        int number = random.nextInt(100) + 1;
    
        System.out.println("I'm thinking of a number between 1 and 100");
        System.out.println("(including both). Can you guess what it is?");
    
        System.out.print("Type a number: ");
        guess = in.nextInt();
        System.out.printf("Your guess is: %s", guess);
    
        diff = number - guess;
        printf("The number I was thinking of is: %d", guess);
        printf("You were off by: %d", diff);
    }
}

但是,当我尝试编译它时,它会出现以下错误:

Guess.java:20: error: cannot find symbol
        printf("The number I was thinking of is: %d", guess);
        ^
    symbol:   method printf(String,int)
    location: class Guess
Guess.java:21: error: cannot find symbol
       printf("You were off by: %d", diff);
       ^
   symbol:   method printf(String,int)
   location: class Guess
2 errors

代码有什么问题?

1 个答案:

答案 0 :(得分:1)

我假设您正在尝试调用printf对象的System.out方法。看起来像这样:

System.out.printf("You were off by: %d", diff);

您需要使用正确的对象目标进行方法调用:通常,方法调用的语法为“ receiver。方法名称(参数)”。如果接收方是当前对象,则可以省略。