第一階段
- 循環語句
-
- for
- while
- do while
- 程式跳轉語句
- 快捷鍵
- 轉義字元
- 練習
循環語句
for
-
格式
for( 1 初始化條件;2 循環條件;3 控制循環條件的語句){
4 循環體;
}
- 流程
-
例子
輸出1~100的所有偶數,并五個偶數分一行
int count=0;
for (int i = 1; i <=100; i++) {
if(i%2==0){
System.out.print(i+" ");
count++;
if(count%5==0){
System.out.println();
}
}
}
while
-
格式
1 初始化條件
while( 2 循環條件 ){
3 循環體;
4 控制循環條件的語句;
}
- 流程
do while
-
格式
1 初始化條件
do {
2 循環體;
3 控制循環條件的語句;
}while( 4 循環條件 )
- 流程
程式跳轉語句
-
break
1.在switch中代表結束/跳出 這個switch
2.在循環中代表 結束/跳出 “目前循環”,( 一般配合if使用 )
-
continue
隻會在循環中使用,結束本次循環,繼續下次循環,( 一般配合if使用 )
-
return
結束/跳出 目前方法 ( 一般配合if使用 )
快捷鍵
- 重命名:F2
轉義字元
- 換行:\r\n
練習
- 直角三角形1
package test;
public class Triangle_1 {
public static void main(String[] args) {
for(int i = 1;i<=5;i++){
for(int j = 1;j<=i;j++){
System.out.print("o"+" ");
}
System.out.println();
}
}
}
-
直角三角形2
偶數行列印o,奇數行列印z
package test;
public class Triangle_2 {
public static void main(String[] args){
for(int i =1;i<=5;i++){
for(int j =1 ;j<=i;j++){
if(i%2==0){
System.out.print("o"+" ");
}else{
System.out.print("z"+" ");
}
}
System.out.println();
}
}
}
- 等腰三角形1
思路:
先用循環做出左邊白色部分的三角形,再做出右邊部分的三角形(下面幾題均适用)
package test;
public class Triangle_3 {
public static void main(String[] args) {
for(int i =5;i>0;i--){
for(int j = 0;j<i;j++){
System.out.print(" ");
}
for(int z = 5;z>=i;z--){
System.out.print("o"+" ");
}
System.out.println();
}
}
}
-
等腰三角形2
偶數行列印z,奇數行列印o
package test;
public class Triangle_4 {
public static void main(String[] args) {
for(int i =5;i>0;i--){
for(int j = 0;j<i;j++){
System.out.print(" ");
}
for(int z = 5;z>=i;z--){
if(i%2==0){
System.out.print("z"+" ");
}else{
System.out.print("o"+" ");
}
}
System.out.println();
}
}
}
- 菱形
package test;
public class Rhombus {
public static void main(String[] args) {
for(int i =5;i>0;i--){
for(int j = 1;j<i;j++){
System.out.print(" ");
}
for(int k = 5;k>=i;k--){
System.out.print("o"+" ");
}
System.out.println();
}
for(int x = 1;x<=4;x++){
for(int y = 1;y<=x;y++){
System.out.print(" ");
}
for(int z=4;z>=x;z--){
System.out.print("o"+" ");
}
System.out.println();
}
}
}
- 九九乘法表
package test;
public class Math {
public static void main(String[] args){
int sum = 1;
for(int i = 1;i<=9;i++){
for(int j = 1;j<=i;j++){
sum = i*j;
System.out.print(i+"x"+j +"="+sum +" ");
}
System.out.println();
}
}
}