每天努力一點點
-Make a little progress every day
文章目錄
- 前言
- 1、順序結構
- 2、分支結構
- 2.1 if 陳述句
- 2.2 switch 陳述句
- 3、回圈結構
- 3.1 while 回圈
- 3.2 break
- 3.3 continue
- 3.4 for 回圈
- 3.5 do while
- 4、輸入輸出
- 4.1 輸出到控制臺
- 4.2 從鍵盤輸入
前言
隨著時代的發展編程語言也在進步,Java語言更是成為當今時代潮流,本文簡述了阿杰對Java語言的初步認識,并記錄阿杰初次進行Java入門學習的部分重要基礎內容;
1、順序結構
順序結構比較簡單,按照書寫代碼的順序一行一行的執行,按照書寫代碼一行一行執行;
package com.company;
public class Main {
public static void main(String[] args) {
System.out.println("aaa");
System.out.println("bbb");
System.out.println("ccc");// write your code here
}
}
運行結果
aaa
bbb
ccc
調整順序后
package com.company;
public class Main {
public static void main(String[] args) {
System.out.println("aaa");
System.out.println("ccc");
System.out.println("bbb");
// write your code here
}
}
運行結果
aaa
ccc
bbb
2、分支結構
2.1 if 陳述句
基本語法格式
if (布爾運算式){
}//條件滿足執行代碼
if(布爾運算式){//條件滿足執行代碼
}else{//條件不滿足執行代碼
}
if(布爾運算式){//條件滿足時執行
}else if(){//條件滿足時執行
}else{//條件不滿足時執行
}
例
隨機輸入一年判斷其是否是閏年:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
if ( year % 4 == 0 && year % 100 != 0 )
{
System.out.println("是閏年");
}
else {
System.out.println("不是閏年");
}
}
}
2.2 switch 陳述句
switch的引數型別
可以作為switch引數的有:整數,列舉,字符,字串;
不能作為switch引數的有:Long,float,double,boolean;
例
輸入day=1,輸出星期一到星期三,若輸入不在1-4內則輸出輸入錯誤;
package com.company;
public class Main {
public static void main(String[] args) {
int day = 1;
switch(day){
default:System.out.println("輸入錯誤");
break;
case 1:
System.out.println("星期一");
case 2:
System.out.println("星期二");
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
}
}
}
switch陳述句中,沒有匹配的 case 就會執行 defult 中的陳述句,建議switch陳述句都帶 defult;
switch陳述句遇到 break 就會結束switch陳述句;
switch不能表達復雜的條件;
switch嵌套很丑;
3、回圈結構
3.1 while 回圈
基本語法格式
while(回圈條件){回圈陳述句}
回圈條件為布爾運算式;
例
計算1到5階乘之和:
public class Main{
public static void main(String[] args){
int i = 1;
int j = 1;
int sum = 1;
int num = 0;
while( j < 6 ){
j++;
while( i < j ){
sum *= i;
i++;
}
num += sum;
}
System.out.println(num);
}
}
3.2 break
break的功能是讓回圈提前結束;
3.3 continue
continue功能是跳過本次回圈進入下次回圈;
找到1000以內3的倍數;
public class Main{
public static void main(String[] args){
int num = 1;
while(num <= 1000){
if(num % 3 != 0) {
++num;
continue;
}
else{
System.out.println(num);
++num;
continue;
}
}
}
}
3.4 for 回圈
基本語法格式
for(用于初始化回圈變數;回圈條件;更新回圈變數)
例
計算1到5階乘之和:
public class Main{
public static void main(String[] args){
int i = 1;
int j = 1;
int num = 1;
int sum = 0;
for( ; j<6 ; ++j ){
for ( ; i <= j ; i++ ) {
num *= i;
}
sum += num;
}
System.out.println(sum);
}
}
for下面的陳述句可以不寫 {} 但不寫的時候只能支持一條陳述句;
for陳述句后面不能寫分號,否則可能導致程式無法運行;
3.5 do while
基本語法格式
do{回圈陳述句
}while(回圈條件)
4、輸入輸出
4.1 輸出到控制臺
System.out.print()//輸出一個字串,不換行;
System.out.println()//輸出一個字串換行;
System.out.printf()//格式化輸出:
4.2 從鍵盤輸入
scanner讀取字串 整數 浮點數;
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
例
隨機輸入一年判斷其是否是閏年(可多次輸入):
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextInt()) {
int year = scanner.nextInt();
if ( year % 4 == 0 && year % 100 != 0 ) {
System.out.println("是閏年");
//continue;
} else {
System.out.println("不是閏年");
}
}
}
}
加油!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/272877.html
標籤:java
