import static java.lang.System.out;
import java.util.Scanner;
import java.util.Random;
public class GuessAgain {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
int numGuesses = 0;
int randomNumber = new Random().nextInt(100) + 1;
out.println(" ************ ");
out.println("Welcome to the Guessing Game");
out.println(" ************ ");
out.println();
out.print("Enter an int from 1 to 100: ");
int inputNumber = keyboard.nextInt();
numGuesses++;
while (inputNumber != randomNumber) {
out.println();
if (inputNumber < randomNumber) {
out.println("Try again with a higher number ...");
} else {
out.println("Try again with a lower number ...");
}
out.print("Enter an int from 1 to 100: ");
inputNumber = keyboard.nextInt();
numGuesses++;
}
out.print("You win after ");
out.println(numGuesses + " guesses.");
keyboard.close();
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number (non-positive to quit): ");
int number = keyboard.nextInt();
int largest = number;
while (number > 0) {
if (number > largest) {
largest = number;
}
System.out.print("Enter a number (non-positive to quit): ");
number = keyboard.nextInt();
}
if (largest > 0) {
System.out.println("The largest is " + largest);
} else {
System.out.println("You didn't enter any positive numbers.");
}
keyboard.close();
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = keyboard.nextInt();
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
System.out.println(n + "! is " + factorial);
keyboard.close();
}
}
The modified code doesn't work correctly because it doesn't check the user's first
input. No matter what the user enters for the first input, the modified code asks for another input. Here's an example:
Enter an int from 1 to 10: 7
Try again...
Enter an int from 1 to 10: 7
You win after 2 guesses.