coding with ☕️
2 subscribers
262 photos
14 videos
11 files
165 links
Anwendungsentwicklung
Download Telegram
import java.util.Scanner;

public class Main {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

double temp;
double newTemp;
String unit;

System.out.print("Enter the temprature: ");
temp = scanner.nextDouble();

System.out.print("Convert to Celsius to Fahrenheit? (C or F): ");
unit = scanner.next().toUpperCase();

// (condition) ? true : false

newTemp = (unit.equals("C")) ? (temp - 32) * 5 / 9 : (temp * 5 / 9) + 25;

System.out.printf("%.1f°%s", newTemp, unit);

scanner.close();
}

}
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter your age: ");
int age = scanner.nextInt();

System.out.print("Do you have VIP pass? (true/false): ");
boolean hasVIPPass = scanner.nextBoolean();

System.out.print("Do you want 3D? (true/false): ");
boolean wants3D = scanner.nextBoolean();

System.out.println("\n--- Result ---");

if (age >= 18) {
if (hasVIPPass) {
if (wants3D) {
System.out.println("You can watch VIP 3D movie!");
} else {
System.out.println("You can watch VIP Standard movie!");
}
} else {
System.out.println("You can watch Standard movie only.");
}
} else {
System.out.println("No movie for you!");
}

scanner.close();
}
}
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();
}
}
🔖 Kodni formatlash

🔗 VS Code: Shift + Alt + F

📌 IntelliJ IDEA: Ctrl + Alt + L
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 + " ");
}


}
}