星期四, 6月 02, 2011

20110531 Java 練習小記


package chapter6;


public class Sample1 {


public static void main(String[] args) {
//設定for 迴圈, 起始值為 1; 繼續迴圈的條件; 遞增或是遞減運算
for(int i=1; i<=5; i++)
{
System.out.println("執行迴圈"+i);
}
System.out.println("迴圈執行結束");


}


}


package chapter6;

public class Sample2 {

public static void main(String[] args) {
for(int i=1; i<=5; i++)
{
System.out.println("第"+i+"次的迴圈");
}
System.out.println("迴圈結束");

}

}


package chapter6;
import java.io.*;
public class Sample3 {

public static void main(String[] args) throws IOException
{
System.out.println("請輸入要印出幾個*");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int num = Integer.parseInt(str);
//把迴圈繼續條件套用變數來執行
for(int i=1; i <= num; i++)
{
System.out.print('*');
}

}

}


package chapter6;
import java.io.*;
public class Sample4 {

public static void main(String[] args) throws IOException 
{
System.out.println("請輸入要從1 加到 那個數字為止的和?");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int num = Integer.parseInt(str);
int sum = 0;
for (int i=1; i <= num; i++)
{
System.out.println("sum="+sum);
System.out.println("i="+i);
sum += i;
}
System.out.println("從1加到"+num+"為止的和"+sum);
}

}


package chapter6;

public class Sample5 {

public static void main(String[] args) {
int i = 1;
while ( i <= 5)
{
System.out.println("第"+i+"次的迴圈");
i++;
}
System.out.println("迴圈結束");

}

}


package chapter6;

public class Sample6 {

public static void main(String[] args) {
int i = 1;
// do while 與 while 的差別是 do while 會先執行程式, 但是 while 會先判斷
//也就是說 do while 至少會執行一次
do{
System.out.println("第"+i+"次的迴圈");
i++;
}while( i <= 5);
System.out.println("迴圈結束");

}

}


package chapter6;

public class Sample7 {

public static void main(String[] args) {
//巢狀迴圈 
//外側的for 迴圈每執行 1 次, 裡面的迴圈要執行 3 次 
for (int i=0; i<5; i++)
{
for (int j=0; j < 3; j++)
{
System.out.println("i是"+i+"j是"+j);
}
}

}

}


package chapter6;

public class Sample8 {

public static void main(String[] args) {
boolean b1 = false;
for ( int i=0; i < 5; i++)
{
//觀察 i 的值
System.out.println("i="+i);
//觀察 b1 的值
System.out.println("b1="+b1);
for (int j=0; j < 5; j++)
{
if(b1 == false)
{
System.out.print('*');
b1 = true;
}
else
{
System.out.print('-');
b1 = false;
}
}
System.out.println("\n**************");
}

}

}


package chapter6;
import java.io.*;
public class Sample9 {

public static void main(String[] args) throws IOException
{
System.out.println("請問要在第幾次終止迴圈呢? (1 ~ 10)");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int num = Integer.parseInt(str);
for (int i=1; i <= 10; i++)
{
System.out.println("第"+i+"次的迴圈");
//如果 i 的值 等於 num 的值, 就執行 break 跳出迴圈
if ( i == num )
{
break;
}
}

}

}


package chapter6;
import java.io.*;
public class Sample10 {

public static void main(String[] args) throws IOException
{
System.out.println("請輸入成績 1 ~ 5");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int res = Integer.parseInt(str);
switch (res){
case 1:
case 2:
System.out.println("還要在加強喔");
break;
case 3:
case 4:
System.out.println("就是這樣保持下去");
break;
case 5:
System.out.println("你太優秀了");
break;
default:
System.out.println("請輸入 1 ~ 5 的成績");
break;
}

}

}


package chapter6;
import java.io.*;
public class Sample11 {

public static void main(String[] args) throws IOException
{
System.out.println("要跳過第幾次的處理 (1~10)");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int res = Integer.parseInt(str);
for (int i=1; i<=10; i++)
{
if ( i == res)
{
//continue 會暫時停止執行, 然後回到迴圈
continue;
}
System.out.println("第"+i+"次的處理次的處理");
}

}

}


package chapter6;

public class Homework6_1 {
public static void main(String[] args) {
System.out.println("輸出出 1~10的偶數");
for (int i=1; i<=10; i++)
{
//因為是偶數, 所以 除以2 的餘數為 0 的時候, 才顯示
if ( i%2 == 0 )
{
System.out.println(i);
}
}

}

}


package chapter6;
import java.io.*;
public class Homework6_2 {

public static void main(String[] args) throws IOException
{
System.out.println("請輸入測驗的分數(0就結束)");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//先將 初始值定義為 0
int num = 0;
int sum = 0;
//這邊我倒是沒有想到使用 do ~ while 進行 
do{
String str = br.readLine();
num = Integer.parseInt(str);
sum += num;
}while ( num != 0);
System.out.println("測驗分數總和"+sum);

}

}


package chapter6;

public class Homework6_3 {
public static void main(String[] args) {
for ( int i=1 ; i < 10 ; i++)
{
for ( int j=1; j < 10; j++)
{
System.out.print(i*j+"\t");
}
System.out.println();
}

}

}


package chapter6;

public class Homework6_4 {

public static void main(String[] args) {
for ( int i=1; i<=5; i++)
{
//這邊要使用 j=0; j<i; 的方式, 又忘記了Orz...
for ( int j=0; j<i; j++)
{
System.out.print("*");
}
System.out.println();
}

}

}


package chapter6;
import java.io.*;
public class Homework6_5 {
public static void main(String[] args) throws IOException
{
System.out.println("請輸入2以上的整數");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int num = Integer.parseInt(str);
for ( int i=2; i <= num; i++)
{
//用來觀察 i 迴圈的變化
System.out.println("i="+i);
//用來判斷是不是只有自己為因數
if ( i == num)
{
System.out.println(num+"是質數");
}
//用來判斷是否可以被 i 整除 例如 10 如果可以被 2 整除就不是質數
else if (num % i == 0)
{
System.out.println("num="+num);
System.out.println("i="+i);
System.out.println(num+"不是質數");
break;
}
}

}

}

沒有留言: