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

Cannot find symbol when I've defined it already

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

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

代码有什么问题?

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

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

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