Easy practice with if statement and boolean type
-
Upload
nooria-esmaelzade -
Category
Self Improvement
-
view
31 -
download
0
Transcript of Easy practice with if statement and boolean type
Easy practice with if statement and Boolean
typeJava programming Nooria Esmaelzade
2016
Review to data types74532573
723.3634
MaryamThis is a string.
A
int, short, byte, long
Float, double
String
char
But how you can save true or false values??How do you save the result of comparison? int x = 10;
int y = 20;
(X > Y ) ( X < Y ) (X = Y)
Primitive data types
integral (صحیح) floating point(اعشاری) Boolean (true, false)
Byte short int long
8-bit 16-bit 32-bit 64-bit
Float double 32-bit 64-bit
Boolean data type
A boolean is used to perform logical operations, most commonly to determine whether some condition is true.
Boolean only can have two values : True
false
Boolean examplepublic class testBoolean {
public static void main(String[] args) {
boolean b = true;boolean b2 = false;boolean b3 = b2;
System.out.println(b);System.out.println(b2);System.out.println(b3);
}
}
Boolean data typeThe result of the comparison is a boolean value
int radius = 10;
Boolean example 2 public class testBoolean {
public static void main(String[] args) {
boolean b ;int x = 10;int y = 20;
b = x>y;
System.out.println(b);System.out.println( x < y);}
}
practice additions gameimport java.util.Scanner; public class AdditionQuiz { public static void main(String[] args) {
int number2 = (int)(System.currentTimeMillis() * 7 % 10); int number1 = (int)(System.currentTimeMillis() % 10);int addition = number2 + number1;
Scanner input = new Scanner(System.in);
System.out.print( "What is " + number1 + " + " + number2 + " ? ");int answer = input.nextInt();
boolean result = addition == answer;System.out.println(number1 + " + " + number2 + " = " + answer + " is " + result);}
}
If statements
What do you think??
So???
If you are a good person You will go to the heaven
If you are a bad person You will go to the hell
How we use if in java
if (you are bad) {
you are in hell ; You will be tortured;
}
if (you are good) {
you are in heaven;
You will enjoy;
}
Another example Who succeeds and who fails ???
If the test score is greater than 55, the student is passed
If the test score is less than 55, the student is failed
How we use it in java
if (score > 55){
Student passed;
}
if (score < 55){
Student failed;
}
Simple program to check if student is passed or failed
import java.util.Scanner;public class passOrFail {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);System.out.println("Enter your score: ");int score = input.nextInt();
if (score >= 55){System.out.println("hahaha you are passed \u263A");}
if (score < 55){
System.out.println("Sorry you are failed \u2639");}
}}
Homework Write the program that ask the user to enter an integer number. The program should check if it is odd or even.
If the number is even, print “the number is even”Otherwise print, “the number is odd”