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;
}
}
}