Java sees a single equal sign which is an assignment operator. So Java assigns the value true to the variable firstSmaller. So the if
condition asks if firstSmaller is true. And, indeed, firstSmaller is true no matter what happened when the computer executed the earlier statement
import java.util.Scanner;
public class WhatKindOfNumber {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
int number;
System.out.print("Enter an integer value: ");
number = keyboard.nextInt();
if (number > 0) {
System.out.println("positive");
} else if (number < 0) {
System.out.println("negative");
} else {
System.out.println("zero");
}
keyboard.close();
}
}
import java.util.Scanner;
public class TrafficSignal3 {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
char color;
char safeToProceed;
char officerDirectingNotToProceed;
System.out.print("What color is the traffic light? (G/Y/R) ");
color = keyboard.findWithinHorizon(".", 0).charAt(0);
System.out.print("Is it safe to proceed? (Y/N) ");
safeToProceed = keyboard.findWithinHorizon(".", 0).charAt(0);
System.out.print("Is is a traffic officer directing you not to proceed? (Y/N) ");
officerDirectingNotToProceed = keyboard.findWithinHorizon(".", 0).charAt(0);
if (color == 'G' &&
safeToProceed == 'Y' &&
officerDirectingNotToProceed != 'Y') {
System.out.println("Go");
} else {
System.out.println("Stop");
}
keyboard.close();
}
}
import java.util.Scanner;
public class TrafficSignal4 {
enum Color {green, yellow, red}
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
char colorOfLight;
Color signal;
System.out.print("What color is the traffic light? (G/Y/R) ");
colorOfLight = keyboard.findWithinHorizon(".", 0).charAt(0);
if (colorOfLight == 'G') {
signal = Color.green;
} else if (colorOfLight == 'Y') {
signal = Color.yellow;
} else {
signal = Color.red;
}
System.out.print("The signal is ");
System.out.print(signal);
System.out.println(".");
keyboard.close();
}
}