

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
public static void main(String[] args) { int num = 7;. System.out.println(What is your name);. Scanner nameInput = new Scanner(System.in);.
Typology: Slides
1 / 3
This page cannot be seen from the preview
Don't miss anything!


class Example1 { public static void main(String[] args) { int num = 7; System.out.println("What is your name"); Scanner nameInput = new Scanner(System.in); String yourName = nameInput.nextLine(); System.out.println("Hello " + yourName); System.out.println( "Please enter a number between 1 and 10"); Scanner numberInput = new Scanner(System.in); int yourNumber = numberInput.nextInt(); System.out.println("Thank-you"); System.out.println("The number stored was " + num); }
class Example2 { public static void main(String[] args) { int num = 7; System.out.println("What is your name"); Scanner nameInput = new Scanner(System.in); String yourName = nameInput.nextLine(); System.out.println("Hello " + yourName); System.out.println( "Please enter a number between 1 and 10"); Scanner numberInput = new Scanner(System.in); int yourNumber = numberInput.nextInt(); if (num == yourNumber) { System.out.println("Well done, a correct guess");} else{ System.out.println("Sorry, an incorrect guess");} } } import java.util.Scanner; class Example3 { public static void main(String[] args) { int num = 7; int correctGuess = 0; System.out.println("What is your name"); Scanner nameInput = new Scanner(System.in); String yourName = nameInput.nextLine(); System.out.println("Hello " + yourName); for (int i = 1; i <= 20; i++) { System.out.println("Please enter a guess"); Scanner numberInput = new Scanner(System.in); int yourNumber = numberInput.nextInt(); if (num == yourNumber) correctGuess = correctGuess + 1; } System.out.println("The number stored was " + num); System.out.println("You chose " + correctGuess + " of these"); }