I'm making a Guess the Number game, and this is my code for the game.
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);
}
}
However, when I try to compile it, it comes up with the following error:
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
What is wrong with the code?
I assume you are trying to call the printf
method of the System.out
object. That would look like:
System.out.printf("You were off by: %d", diff);
You need to make the method call using the right object target: in general the method call syntax is "receiver . method name ( parameters )". If the receiver is the current object, it can be omitted.