public class ControlFlowIf {
public static void main(String[] args) {
int score = 85;
System.out.println("Score: " + score);
// Simple If-Else
if (score >= 50) {
System.out.println("Result: Pass");
} else {
System.out.println("Result: Fail");
}
// Else-If Ladder (Grading System)
char grade;
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else if (score >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade: " + grade);
// Nested If
boolean hasLicense = true;
boolean hasCar = false;
if (hasLicense) {
if (hasCar) {
System.out.println("You can drive your car.");
} else {
System.out.println("You need to rent a car.");
}
} else {
System.out.println("You need a license used.");
}
}
}import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Enhanced switch = A replacement to many else if statements
// (Java14 features)
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the day of the Week: ");
String day = scanner.nextLine();
switch (day) {
case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ->
System.out.println("It is a weekday!");
case "Saturday", "Sunday" ->
System.out.println("It is the weekend!");
default -> System.out.println(day + " is not a day!");
}
scanner.close();
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Calculator program
Scanner scanner = new Scanner(System.in);
double num1;
double num2;
char operator;
double result = 0;
boolean validOperation = true;
System.out.print("Enter the first number: ");
num1 = scanner.nextDouble();
System.out.print("Enter an operator (+, -,/, ^): ");
operator = scanner.next().charAt(0);
System.out.print("Enter the second number: ");
num2 = scanner.nextDouble();
switch (operator){
case '+' -> result = num1 + num2;
case '-' -> result = num1 - num2;
case '*' -> result = num1 * num2;
case '/' -> {
if (num2 == 0) {
System.out.println("Cannot divide by zero!");
validOperation = false;
} else {
result = num1 / num2;
}
}
case '^' -> result = Math.pow(num1, num2);
default -> {
System.out.println("Invalid operator!");
validOperation = false;
}
}
if(validOperation) {
System.out.println(result);
}
scanner.close();
}
}
public class Main {
public static void main(String[] args) {
// LOGICAL OPERATORS
// && = AND
// || = OR
// ! = NOT
double temp = 35;
boolean isSunny = true;
if (temp <= 30 && temp >= 0 && isSunny){
System.out.println("The weather is GOOD ");
System.out.println("It is SUNNY outside ");
} else if (temp <= 30 && temp >= 0 && !isSunny ) {
System.out.println("The weather is GOOD! ");
System.out.println("It is CLOUDY outside! ");
} else if (temp > 30 || temp < 0) {
System.out.println("The weather is bad! ");
}
}
}import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// LOGICAL OPERATORS
Scanner scanner = new Scanner(System.in);
// username must be between 4=12 characters
// username must be not contain space or underscores
String username;
System.out.print("Enter your new username: ");
username = scanner.nextLine();
if (username.length() < 4 || username.length() > 12) {
System.out.println("Username must be between 4-12 characters!");
} else if (username.contains(" ") || username.contains("_")) {
System.out.println("Username must not contain Spaces or Underscores!");
} else {
System.out.println("Welcome! " + username);
}
scanner.close();
}
}
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// WHILE LOOP = repeat some code forever
// while some condition remains true
Scanner scanner = new Scanner(System.in);
int age = 0;
do {
System.out.println("Your age can't be negative");
System.out.print("Enter your age: ");
age = scanner.nextInt();
}while (age < 0);
System.out.println("Your age " + age + " years old");
scanner.close();
}
}
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int guess;
int attempts = 0;
int min = 1;
int max = 100;
int randomNumber = random.nextInt(min, max + 1);
System.out.println("Number Guessing Game");
System.out.printf("Guess a number between %d-%d\n", min, max);
do {
System.out.print("Enter a Guess: ");
guess = scanner.nextInt();
attempts++;
if (guess < randomNumber) {
System.out.println("TOO LOW! TRY again");
} else if (guess > randomNumber) {
System.out.println("TOO HIGH! TRY again");
} else {
System.out.println("CORRECT! The number was " + randomNumber);
System.out.println("# of attempts: " + attempts);
}
} while (guess != randomNumber);
scanner.close();
}
}
import java.util.Scanner;
public class First {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of students: ");
int studentCount = scanner.nextInt();
String[] names = new String[studentCount];
int[] grades = new int[studentCount];
for (int i = 0; i < studentCount; i++) {
System.out.println("\nStudent " + (i + 1));
System.out.print("Name: ");
names[i] = scanner.next();
System.out.print("Grade (0-100): ");
grades[i] = scanner.nextInt();
}
int sum = 0;
int max = grades[0];
int min = grades[0];
for (int i = 0; i < studentCount; i++) {
sum += grades[i];
if (grades[i] > max) {
max = grades[i];
}
if (grades[i] < min) {
min = grades[i];
}
}
double average = (double) sum / studentCount;
System.out.println("\n===== STUDENT RESULTS =====");
for (int i = 0; i < studentCount; i++) {
System.out.println(names[i] + " → " + grades[i]);
}
System.out.println("\nAverage grade: " + average);
System.out.println("Highest grade: " + max);
System.out.println("Lowest grade: " + min);
scanner.close();
}
}
import java.util.Scanner;
public class First {
public static void main(String[] args) throws InterruptedException {
Scanner scanner = new Scanner(System.in);
System.out.print("How many seconds to countdown from?: ");
int start = scanner.nextInt();
for (int i = start; i > 0; i--) {
System.out.println(i);
Thread.sleep(1000);
}
System.out.println("HAPPY NEW YEAR!");
scanner.close();
}
}
public class First {
public static void main(String[] args) {
// break = break out of a loop (STOP)
// continue = skip current iteration of a loop (SKIP)
for (int i = 0; i < 10; i++) {
if (i == 5){
continue;
}
System.out.print(i + " ");
}
}
}import java.util.Scanner;
public class First {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int rows;
int columns;
char symbol;
System.out.print("Enter the # of rows: ");
rows = scanner.nextInt();
System.out.print("Enter the # of columns: ");
columns = scanner.nextInt();
System.out.print("Enter the symbol to use: ");
symbol = scanner.next().charAt(0);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(symbol);
}
System.out.println();
}
scanner.close();
}
}
coding with ☕️
import java.util.Scanner; public class First { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int rows; int columns; char symbol; System.out.print("Enter the # of rows: ");…
// nested loop = A loop inside another loop
// Used often with matrices or DS&A
// Data Structure and Algorithms
public class First {
public static void main(String[] args) {
// A method = is a block of code which only runs when it is called.
// You can pass data, known as (parameters), into a method.
// Methods = are used to perform certain actions, and they are also known as (functions).
String name = "Bro";
int age = 25;
happyBirthday(name, age);
}
static void happyBirthday(String name, int age) {
System.out.println("Happy birthday to you!");
System.out.printf("Happy birthday dear %s!\n", name);
System.out.printf("You are %d years old!\n", age);
System.out.println("Happy birthday to you!\n");
}
}public class First {
public static void main(String[] args) {
int age = 12;
if (ageCheck((age))) {
System.out.println("You may sign up!");
} else {
System.out.println("You must be 18+ to sign up!");
}
}
static void happyBirthday(String name, int age) {
System.out.println("Happy birthday to you!");
System.out.printf("Happy birthday dear %s!\n", name);
System.out.printf("You are %d years old!\n", age);
System.out.println("Happy birthday to you!\n");
}
static double square(double number) {
return number * number;
}
static double cube(double number) {
return number * number * number;
}
static String getFullName(String first, String last) {
return first + " " + last;
}
static boolean ageCheck(int age) {
if (age >= 18) {
return true;
} else {
return false;
}
}
}