close
條件判斷
if (條件){
敘述;
}
|
public static void main(String[] args) {
int score =(int)(Math.random()*101); //以 int型態隨機產生0~100的值
System. out.println("分數=" +score);
if (score>=60){
System. out.println("YA~" );
}
}
|
if( 條件 ){
敘述;
} else {
敘述;
}
|
public static void main(String[] args) {
//亂數產生1~10的值 並印出是偶數或基數
int a = (int )(Math.random()*10+1);
System. out.println(a);
if(a%2==0){
System. out.println("偶數" );
} else{
System. out.println("奇數" );
}
}
|
if( 條件 ){
敘述;
} else if( 條件 ){
敘述;
} else if( 條件 ){
...
...
} else {
敘述;
}
|
public static void main(String[] args) {
int a=(int )(Math.random()*11-5);
System. out.println(a);
if(a>0){
System. out.println("positive" );
} else if (a<0){
System. out.println("negative" );
} else {
System. out.println("0" );
}
}
|
全站熱搜
留言列表