here's what i got today
selection statement and repetition
selection :
- using if statement
- using switch statement
- write boolean expression
- evaluate given boolean expression correctly
- nested if
- comparing objects
- selecting statement for given task
if syntax
- if (
else
relational operator
> more than
< less than
>= more than or equal to
<= less than or equal to
== equal to
!= not equal to
nested if
- if (comparing something){
if (comparing something){
do something
}
}
boolean operator
&& and
|| or
! not
boolean vars
- value : true / false
comparing object
- using ==
ex : String str1 = new String("java");
String str1 = new String("java");
str1 != str2
the location is different
ex : String str1 = new String("java");
str2 = str1
str1 == str2
the location is the same
- another way (to check only the value)
ex : if(str1.equals(str2)){
. . .
}
using switch case
- another branching statement with many possibilities
ex : switch ( N ){
case 1 : x = 30;
break;
case 2 : x = 40;
break;
case 3 : x = 50;
break;
default
trying something new
- using if statement and JOptionPane and also parseInt (converting String to Integer)
- here it is
IdealWeight.java
import javax.swing.JOptionPane;
public class IdealWeight {
public static void main(String[] args) {
int height,age;
double rec;
height = Integer.parseInt(JOptionPane.showInputDialog("input height : "));
age = Integer.parseInt(JOptionPane.showInputDialog("input age : "));
rec = (height - 100 + age / 10) * 0.9;
if(height > 140 && height < 230 && age > 0 && age < 100){
JOptionPane.showMessageDialog (null, "recom weight : " + rec );
}
else
JOptionPane.showMessageDialog (null, "error");
}
}
Repetition / looping
- using while - sentinel (have a certain statement to fulfill)
- using for - count controlled (have fixed times of loop)
commong looping mistakes
- off-by-one error
- make sure there is a statement which will terminate the loop
- make sure it loops just as many times as you wanted
while syntax
- while (statement){
do something
}
- while wont do the "something" unless the statement is fulfilled
do-while syntax
- do {
do something
}
- while(statement);
- do-while will do the "something" at least once before it evaluate the condition
for syntax
- for(starting condition; loop terminated condition; statement looped){
do something
}
a game to try on
not perfect, yet its enough for now
import javax.swing.JOptionPane;
public class HiLo {
private int secret,counter=0,guess;
public HiLo(){
secret = (int)(Math.random()*100);
JOptionPane.showMessageDialog(null, "I have a secret number, try and guess what it is");
while(counter<=6){
guess = Integer.parseInt(JOptionPane.showInputDialog("your guess : "));
if(guess < secret){
JOptionPane.showMessageDialog(null, "to low");
}
else if(guess > secret){
JOptionPane.showMessageDialog(null, "to high");
}
else if(guess == secret){
break;
}
counter++;
}
if(guess == secret){
JOptionPane.showMessageDialog(null, "my number is " + secret + ",you win");
}
else if(guess != secret)
JOptionPane.showMessageDialog(null, "my number is " + secret + ",you lost");
}
public static void main(String[] args) {
HiLo x = new HiLo();
}
}
thats all today...

No comments:
Post a Comment