顯示具有 JAVA7筆記內容 標籤的文章。 顯示所有文章
顯示具有 JAVA7筆記內容 標籤的文章。 顯示所有文章

2016年6月20日 星期一

迴圈敘述(page2)-

3.for

語法:
for(變數=初值;條件;變數增減值)
{
主體
}
說明:
(1)變數的命名是迴圈控制變數,可以宣告在迴圈內
註:宣告在內式外不影響迴圈執行的圈數
如
int x;
for(x=1; x<=5; x++)
→
for(int x=1; x<=5;x++)

宣告於迴圈內的x只能活在迴圈內,離開迴圈無效
宣告於迴圈外的x,離開回圈,宣告繼續有效
(2)條件式的結果必須布林值
(3)變數增減值以指定之運算表示
如
for(x=1; x<=10; x+=2)
for(x=1; x<25 ; x++)
for(x=5; x>0 ; x--)
(4)可以有兩個或兩個以上的迴圈控制變數
如
for(x=1, y=2 ; x<=5 && y<=16; x++ ; y+=2)
x=1,2,3,4,5
y=2,4,6,8,10,12,14,16
→
x=1~5
y=2~10
for(x=1, y=2; x<=5 || y<=16; x++, y+=2)
x=1,2,3,4,5,6,7,8
y=2,4,6,8,10,12,14,16
→
x=1~8
y=2~16
(5)三個項目都可以省略,但是「;」
不可以省略
for(;;)
int x;
for(x=1; x<=5; x++)
{
total += x;
}
(6)迴圈執行的流程
for( x=1; x<=5; x++ )
// x=1步驟A
// x<=5步驟B
//x++步驟C
{
total+=x;//步驟D
}
Step1:執行A
Step2:判斷B,C是否合理
Step3:
(1)不合理─跳脫迴圈
(2)合理─執行D
Step4:執行C
Step5:執行B
(1)false─跳脫迴圈
(2)true─執行D

迴圈敘述(page1)-

說明:
(1)Java提供四種迴圈
分別為-
while, do while, for, enhance for
(2)選擇適合的迴圈
    1)迴圈數可數
    2)迴圈數不可數:while, do while, enhance for
    3)先判斷後執行:while, for, enhance for
    4)先執行後判斷:do while
1.while
語法:
while(條件)
{
主體
}

1.while
語法:
int total=0, x=1;
while(x<=5)
{
total=total+x;
x=x+1;
}
System.out.println("總和:"+total);

do{
total=total+x;
x=x+1;
}while(x<=5);

說明:
(1)條件式的結果只能是布林值
(2)迴圈的主體只有一個敘述的時候,大括號可以省略
2.do while
語法:
do{
主體
}while(條件);
說明:(1)(2)同while
(3)「;」不可以省略

//分數-81,評分A,B,C,D,E發覺異常

//分數-81,評分A,B,C,D,E發覺異常

public class U8_Switch_OK1 
{
public static void main(String[] args) 
{
int s=-81;

switch (s/10)
{
case 10:case 9:
System.out.println("學生成績 A");
break;
case 8:
System.out.println("學生成績 B");
break;
case 7:
System.out.println("學生成績 C");
break;
case 6:
System.out.println("學生成績 D");
break;
case 5:case 4:case 3:case 2:case 1:case 0:
System.out.println("學生成績 E");
break;
default:
System.out.println("Data 異常!");
}
}
} 



//分數-81,評分A,B,C,D,E沒發覺異常

//分數-81,評分A,B,C,D,E沒發覺異常

public class U8_Switch_OK1 
{
public static void main(String[] args) 
{
int s=-81;

switch (s/10)
{
case 10:case 9:
System.out.println("學生成績 A");
break;
case 8:
System.out.println("學生成績 B");
break;
case 7:
System.out.println("學生成績 C");
break;
case 6:
System.out.println("學生成績 D");
break;
default:
System.out.println("學生成績 E");
}
}
} 





//請輸入分數1-100,評分A,B,C,D,E

//請輸入分數1-100,評分A,B,C,D,E

public class U807_Switch 
{
public static void main(String[] args) 
{
java.util.Scanner scanner = 
new java.util.Scanner(System.in);
System.out.print("請輸入分數1-100: ");
byte score = scanner.nextByte();
//byte score = 81;

switch(score)
{
case 100:
case 99:
case 98:
case 97:
case 96:
case 95:
case 94:
case 93:
case 92:
case 91:
case 90:
System.out.println("成績:A");
break;

case 89: case 88: case 87: case 86: case 85: case 84: case 83: case 82: case 81: case 80:
System.out.println("成績:B");
break;

case 79:case 78:case 77:case 76:case 75:case 74:case 73:case 72:case 71:case 70:
System.out.println("成績:C");
break;

case 69:
case 68:
case 67:
case 66:
case 65:
case 64:
case 63:
case 62:
case 61:
case 60:
System.out.println("成績:D");
break;

default:
System.out.println("成績:E");
}
}
}


//U806_Switch.java

//U806_Switch.java
public class U806_Switch 
{
public static void main(String[] args) 
{
int grade = 66;
Integer oo=new Integer(66);
switch(grade='A')
{
case 'A':
System.out.println("學生成績 A");
//break;
case 'B':
System.out.println("學生成績 B");
//break;
case 'C':
System.out.println("學生成績 C");
//break;
default:
System.out.println("學生成績 D");
}
}
}

switch敘述-

語法:
switch(運算式)
{
    case 結果值a:
    敘述群1
    case結果值b:
    敘述群2
          .
          .     
          .
default:
    敘述群n
}

說明:
(1)運算式的結果只能是
int(byte, short, int)
char, String
註:多搶promotion
(2)case代表入口,出口則是
    1)將所有敘述群都執行1次
    2)執行了「break;」
VB:
select運算式
    case結果a
    敘述群1
    case結果b
    敘述群2
    case else
    敘述群n

//輸入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)

//使用floor()產生四捨五入到小數第2位

//使用floor()產生四捨五入到小數第2位

import java.io.*;

public class Round2OK
{
public static void main(String[] args) throws IOException
{
BufferedReader keyin;
keyin = new BufferedReader(
new InputStreamReader(System.in));

System.out.print("請輸入數值: ");
String sNum = keyin.readLine();
double iNum = Double.parseDouble(sNum);

iNum = iNum*100;

double result = Math.floor(iNum+0.5);

System.out.println("結果 = " + result/100);

System.out.println();
}
}





//以floor()四捨五入到小數第2位

//以floor()四捨五入到小數第2位

public class Round2{
public static void main(String[] args) {
java.util.Scanner scanner = 
new java.util.Scanner(System.in);
int replay = 0;
do {
System.out.print("輸入數值:(例1.3含小數位)");
double num1 = scanner.nextDouble();
//System.out.print((int)(Math.round(num1*100.0))/100.0);
System.out.print((int)(Math.floor(num1*100.0))/100.0);
System.out.print("繼續(1:繼續 0:結束)?");
replay = scanner.nextInt();
} while(replay == 1);
}
}