import java.util.Scanner;
public class SmileyFace {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
char reply;
System.out.print("Would you like to see a smiley face? ");
reply = keyboard.findWithinHorizon(".", 0).charAt(0);
if (reply == 'Y') {
System.out.println(":-)");
} else {
System.out.println(":-(");
}
keyboard.close();
}
}
import java.util.Scanner;
public class SmileyFace2 {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
char reply;
System.out.print("Would you like to see a smiley face? ");
reply = keyboard.findWithinHorizon(".", 0).charAt(0);
if (reply == 'Y') {
System.out.println(":-)");
}
if (reply == 'N') {
System.out.println(":-(");
}
if (reply == '?') {
System.out.println(":-|");
}
keyboard.close();
}
}
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
Random myRandom = new Random();
int randomNumber;
int guess;
randomNumber = myRandom.nextInt(10) + 1;
System.out.print("Guess a number from 1 to 10: ");
guess = keyboard.nextInt();
if (guess == randomNumber) {
System.out.println("You win!");
} else {
System.out.println("You lose.");
}
keyboard.close();
}
}
This problem is a bit tricky. You're reading an int value followed by a letter. For Java to determine where the int value ends, you must have a
blank space after the int value. But Java executes the following two statements one after another,
then the second statement (the findWithinHorizon call) gets the blank space. To fix this problem, you insert an extra findWithinHorizon call between
the the two statements to pick up that blank space character.
import java.util.Scanner;
public class ConvertingLengths {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
int meters;
char blankSpace, convertTo;
System.out.print("Enter the number of meters followed by a blank space and then a letter (c or m): ");
meters = keyboard.nextInt();
blankSpace = keyboard.findWithinHorizon(".", 0).charAt(0);
convertTo = keyboard.findWithinHorizon(".", 0).charAt(0);
if (convertTo == 'c') {
System.out.print(meters * 100);
System.out.println(" centimeters");
}
if (convertTo == 'm') {
System.out.print(meters * 1000);
System.out.println(" millimeters");
}
keyboard.close();
}
}
import java.util.Scanner;
public class Poem {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
char reply;
System.out.print("Would you like to read a poem? (Y/N) ");
reply = keyboard.findWithinHorizon(".", 0).charAt(0);
if (reply == 'Y') {
System.out.println("There once was an old man with glasses");
System.out.println("Who learned about objects and classes.");
System.out.println("At last, I see!");
System.out.println("It's not only for me.");
System.out.println("I'll teach these ideas to the masses!");
} else {
System.out.println("Sorry!");
System.out.println("I thought you were a poetry buff.");
System.out.println("Maybe you'll want to see the poem the next time you run this program.");
}
keyboard.close();
}
}