Assignemnt 65 and Number-Guessing With a Counter
Code
/// Name: Xinting Chen
/// Period: 7
/// Program Name: Number-Guessing With a Counter
/// File Name: NumberGuessingWithCounter.java
/// Date Finished: 12/8/2015
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.Random;
public class NumberGuessingWithCounter
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int number = 1 + r.nextInt(10);
int guess = 0, attempt= 0;
String response= " ";
System.out.println("I have chosen a number from 1 to 10!");
System.out.println();
int attempts = 0;
System.out.print("Your guess is: ");
guess = keyboard.nextInt();
attempts++;
while (guess!= number)
{
System.out.println("That's incorrect. Try again.");
guess = keyboard.nextInt();
attempts++;
}
System.out.println("Yay! You guessed it right! It only took you " + attempts + " tries." );
}
}
Picture of the output