if (/* this expression is true */) {
// run this code
} else if (/* this expression is true */) {
// run this code
} else {
// run this code
}
double money = 100.50;
double price = 100.50;
if (money > price) {
System.out.println("You paid extra, here's your change.");
} else if (money == price) {
System.out.println("You paid the exact amount, have a nice day!");
} else {
System.out.println("That's not enough, you still owe me money.");
}
// what is the output?
if (!true) {
System.out.println("statement 1");
} else {
System.out.println("statement 2");
}
switch (expressionn) {
case value :
// statement
break;
case value :
// statement
break;
default :
// statement
break;
}
The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums
int day = 7;
String dayString;
switch (day) {
case 1: dayString = "Sunday";
break;
case 2: dayString = "Monday";
break;
case 3: dayString = "Tuesday";
break;
case 4: dayString = "Wednesday";
break;
case 5: dayString = "Thursday";
break;
case 6: dayString = "Friday";
break;
case 7: dayString = "Saturday";
break;
default: dayString = "Invalid day";
break;
}
System.out.println(dayString);
if (expression) {
// expressionTrue
} else {
// expressionFalse
}
(expression) ? expressionTrue : expressionFalse;
Sama dengan:
int number = 10;
String result;
if (number >= 0) {
result = "positive";
} else {
result = "negative";
}
System.out.println(number + " adalah bilangan " + result);
int number = 10;
String result;
result = number >= 0 ? "positive" : "negative";
System.out.println(number + " adalah bilangan " + result);
Boolean is a data type that only has two possible values: true or false
int a = 1;
int b = 2;
System.out.println(a == b);
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a >= b);
System.out.println(a <= b);
System.out.println(a != b);
// Operator && (AND)
// What are the results of these:
System.out.println(true && true);
// true
System.out.println(true && false);
// false
System.out.println(false && true);
// false
System.out.println(false && false);
// false
// Operator || (OR)
// What are the results of these:
System.out.println(true || true);
// true
System.out.println(true || false);
// true
System.out.println(false || true);
// true
System.out.println(false || false);
// false
1. Even or odd numbers
prints "even" if the number is an even number
prints "odd" if the number is an odd number
Use if-else and ternary operator
Write a series of conditional statements that:
3. Favorite Programming Language
If user inputs with one of "Java", "Ruby", and "Python", program will answer with the name of its founder. For instance, "Ruby is created by Yukihiro Matsumoto". If user input other programming languages, program will answer with, "Sorry, I don't know that programming language"
import java.util.Scanner;
class FavoriteLanguage {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String language = in.nextLine();
// your code
}
}
4. Show random value by grade (use switch-case)
Write a series of conditional statements that:
import java.util.Scanner;
import java.util.Random.*;
class RandomValue {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String grade = in.nextLine();
double value;
// your code here
System.out.println("Value = " + value);
}
}
class Exercise1 {
public static void main(String[] args) {
int number = 10;
// if-else statement
if (number % 2 == 0) {
System.out.println(number + " is a even number");
} else {
System.out.println(number + " is a odd number");
}
// ternary operator
String result = (number % 2 == 0) ? number + " is a even number" : number + " is a odd number";
}
}
class Exercise2 {
public static void main(String[] args) {
int musicians = 10;
// if-else statement
if (musicians <= 0) {
System.out.println("not a group");
} else if (musicians == 1){
System.out.println("solo");
} else if (musicians == 2){
System.out.println("duet");
} else if (musicians == 3){
System.out.println("trio");
} else if (musicians == 4){
System.out.println("quartet");
} else {
System.out.println("its a large group");
}
}
}
import java.util.Scanner;
class FavoriteLanguage {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String language = in.nextLine();
// if-else statement
if (language.toLowerCase().equals("ruby")) {
System.out.println(language + " is created by Yukihiro Matsumoto");
} else if (language.toLowerCase().equals("java")) {
System.out.println(language + " is created by James Gosling");
} else if (language.toLowerCase().equals("python")) {
System.out.println(language + " is created by Guido van Rossum");
} else {
System.out.println("Sorry, I don't know that programming language");
}
}
}
import java.util.Scanner;
import java.util.Random.*;
class RandomValue {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String grade = in.nextLine();
double value = 0.0;
// create random object
Random random = new Random();
int min = 0;
int max = 100;
if (grade.toLowerCase().equals("a")) {
min = 87;
} else if (grade.toLowerCase().equals("ab")) {
min = 78;
max = 86;
} else if (grade.toLowerCase().equals("b")) {
min = 69;
max = 77;
} else if (grade.toLowerCase().equals("bc")) {
min = 60;
max = 68;
} else if (grade.toLowerCase().equals("c")) {
min = 51;
max = 59;
}else if (grade.toLowerCase().equals("d")) {
min = 41;
max = 50;
}else {
max = 40;
}
value = min + ((max - min) * random.nextDouble());
System.out.println("Value = " + value);
}
}