2016年6月20日 星期一

//輸入a值、b值、c值,比最大值

//輸入a值、b值、c值,比最大值
import java.util.*;
public class IF_Lab 
{
public static void main(String[] args) 
{
//int a=15,b=30,c=60;
java.util.Scanner scanner = 
new java.util.Scanner(System.in);
int replay = 0;
do {
System.out.print("請輸入a值: ");
int a = scanner.nextInt();
System.out.print("請輸入b值: ");
int b = scanner.nextInt();
System.out.print("請輸入c值: ");
int c = scanner.nextInt();
if( a > b & a > c )
{
System.out.println(a+"是最大值");
}
else if( b > a & b > c )
{
System.out.println(b+"是最大值");
}
else
{
System.out.println(c+"是最大值");
} 
System.out.print("繼續(1:繼續 0:結束)?");
replay = scanner.nextInt();
} while(replay == 1);

}
}



//算閏年,U805_IfIfElse.java


import java.util.*;
//算閏年,U805_IfIfElse.java
public class U805_IfIfElse 
{
public static void main(String[] args) 
{
int replay = 0;
do {
int year,rem4,rem100,rem400;

Scanner scanner = new Scanner(System.in);
System.out.print("請輸入年份: ");
year = scanner.nextInt();
System.out.println();

rem4 = year % 4;
rem100 = year % 100;
rem400 = year % 400;

/* 巢狀if */
if ( rem4 == 0 & rem100== 0 & rem400==0)
{
System.out.println(year+"是閏年");
}
else if (rem4 == 0 & rem100 != 0)
{
System.out.println(year+"是閏年");
}
else
{
System.out.println(year+"是平年"); 
}
System.out.print("繼續(1:繼續 0:結束)?");
replay = scanner.nextInt();
} while(replay == 1);

}
}



IF敘述(page2)-

4.巢狀if

↓

條件1→false→A

↓

true

↓

條件2→false→B

↓

true

↓

條件3→false→C

↓

true

↓

.......

n
語法:
if(條件1)
{
    if(條件2)
    {
          if(條件3)
           {          
              敘述群D 
             }
          else
          {
              敘述群C
           }
     }
    else
    {
    敘述群B
     }
else
{
敘述群A
}
}

Lab判斷西元年分是否為閏年
(1)可被4,100,400同時整除
40
2000閏年,1900平年
(2)可被4整除,但是不可被100整除
2004閏年,2008閏年



IF敘述(page1)-

1.單向if
↓
條件→false→
true            ↓
↓                 ↓
敘述群         ↓
↓                 ↓
↓←←←←←←
↓

if(score>=60){
    System.out.println("及格!");
}
→
if(score>=60)
System.out.println("及格!");

2.雙向if
↓
條件→false→
↓                 ↓
true            ↓
A               B
↓←←←←←←
↓
↓

語法:
if(條件式)
{
敘述群A
}
else
{
敘述群B
}
說明:同單向if
例.
if(a>b)
{
max=a;
}
else
{
max=b;
}
↓
if(a>b)
max=a;
else
max=b;
↓
max=a>b?a:b;
註:稱為三元運算式
↓
max=(a>b)?a:b;

3.鏈狀if
↓
條件1→→→↓
↓               ↓
A            條件2→→→↓
                 ↓              ↓
                B            條件3→→→↓
                                ↓              ↓
                               C              ......
語法:
if(條件1)
{
    敘述群A
}
else if(條件2)
    {
    敘述群B
     }
    else if(條件3)
          {
              敘述群C
             }
           else
              .................

承上,現代VB中的if改進版,寫完電腦自動調整:
//陳述層次太多可避免水平捲軸跑出來
if(條件1)
{
敘述群A
}
else if(條件2)
{
敘述群B
}
else if(條件3)
{
敘述群C
}
else if(條件4)
{
.......
}


//if說明 U801_If.java

//if說明  U801_If.java
public class U801_If 
{
public static void main(String[] args) 
{
int score = 50;

if( score >= 60 )
{
System.out.println("及格");
}

if( score < 60 )
System.out.println("不及格");

if( score < 60 ) System.out.println("不及格");

} 
}



Random類別

Random類別
1.產生路徑在 java.util
2.產生物件
Ramdom 物件 = new Random()
3.方法:
nextInt(n):產生0~(n-1)的亂數

rint()四捨六入到個位數

小數中一位是「5」時,當個位數是奇數採進位,個位數是偶數將捨去

Math.rint(123.4):123.0
Math.rint(123.5):124.0
Math.rint(124.5):124.0
Math.rint(123.6):124.0
Math.rint(-123.5):-124.0
Math.rint(-124.5):-124.0

random():回傳0.0<=0<1.0的double數值

例.產生1~6的亂數
0.0<=Math.random()<1.0
0.0<=Math.random()*6<6.0
0<=(int)(Math.random()*6)<6
0<=(int)(Math.random()*6)<=5
1<=(int)(Math.random()*6)+1<=6
      (int)(Math.random()*6+1)

指定運算-
註:產生1~n的亂數,公式為
(int)(Math.random()*n+1)