for (設定變數初始值;重複條件;變數的變化){}
for (int i=0;i<=10;i++){
System.out.println(i);
}
0
1
2
3
4
5
6
7
8
9
10
Public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("請輸入倍數");
int a=sc.nextInt();
System.out.println("請輸入第一個數");
int b=sc.nextInt();
System.out.println("請輸入第二個數");
int c=sc.nextInt();
int total=0;
if (b<c) {
for (;b<=c;b++) { //設定重複條件為b<=c,並且每次b都+1
if (b%a==0) {
System.out.println(b);
total += b;
}
}
}else {
for (;c<=b;c++) { //設定重複條件為c<=b,並且每次c都+1
if (c%a==0) {
System.out.println(c);
total += c;
}
}
}
System.out.println("上列數字相加="+total);
}
}
此程式順利運行,在我們輸入倍數5、第一數1、第二數100以後會顯示
5
10
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100
上列數字相加=1050
while (重複條件){
重複執行的程式
控制變數的改變
}
int i=1;
int total=0;
while (i<=10) {
total+=i;
i++;
}
int i = 10;
for (; i > 0;) {
System.out.println(i);
}
上面的程式碼因為沒有設定變數改變式,所以條件成立就會一執行下去,要注意這細節。
package top02;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Ex04 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc= new Scanner(System.in);
System.out.println("請輸入倍數");
int a=sc.nextInt();
System.out.println("請輸入第一個數");
int b=sc.nextInt();
System.out.println("請輸入第二個數");
int c=sc.nextInt();
int total=0;
if (b<c) {
while (b<=c) {
if (b%a==0) {
System.out.println(b);
total += b;
b++;
}
}
}else {
while (c<=b) {
if (c%a==0) {
System.out.println(c);
total += c;
c++;
}
}
}
System.out.println("上列數字相加="+total);
}
}
package top02;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Ex04 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc= new Scanner(System.in);
System.out.println("請輸入倍數");
int a=sc.nextInt();
System.out.println("請輸入第一個數");
int b=sc.nextInt();
System.out.println("請輸入第二個數");
int c=sc.nextInt();
int total=0;
if (b<c) {
while (b<=c) {
if (b%a==0) {
System.out.println(b);
total += b;
}
b++; //再擺錯位置阿
}
}else {
while (c<=b) {
if (c%a==0) {
System.out.println(c);
total += c;
}
c++; //再擺錯位置阿
}
}
System.out.println("上列數字相加="+total);
}
}
do {
重複執行的程式
控制變數的式子
}while(終止的條件);
名稱 | 功能 | 範例 | 結果 |
---|---|---|---|
abs | 計算絕對值 | Math.abs(-5) | 5 |
max | 取得兩數的最大值 | Math.max(1.2, 3.4) | 3.4 |
min | 取得兩數的最小值 | Math.min(2, 5) | 2 |
floor | 無條件捨去 | Math.floor(3.8) | 3.0 |
random | 取得0<=x<1 的亂數 | Math.random() | |
sqrt | 開根號 | Math.sqrt(9) | 3 |
round | 小數四捨五入 | Math.round(3.7) | 4 |
ceil | 無條件進位 | Math.ceil(3.2) | 4.0 |
pow | 次方 | Math.pow(2, 3) | 8.0 |
//猜數字(1-6)
//使用者可以猜三次
System.out.println("請開始猜數字,僅有三次機會");
Scanner sc=new Scanner(System.in);
int guess;
Data01 data01=new Data01();
for (int i=0;i<3;i++) {
guess = sc.nextInt();
if (data01.joudge(guess) == true) {
System.out.println("猜對了");
break;
}else {
System.out.println("猜錯了");
}
}
System.out.println("遊戲結束");
int ranno=(int)(Math.floor(Math.random()*6+1)); //
boolean joudge(int guess) { //這裡使用boolean是為了讓下面的程式可以回傳ture或false
System.out.println(ranno);
if (ranno==guess) {
return true;
}
return false;
}
程式成功運行,終於有一次是使用到副程式的例題了(明明就自己不愛用副程式),副程式上面的註解有寫道我們為了讓程式回傳給我們true或false所以使用boolean來讓{}裡面的式子回傳給我們。