目錄
一.順序結構
二.分支結構
1.if陳述句
用法展示1-基本用法
用法展示2-多分支情況
注意事項
2.switch陳述句
用法展示
注意事項
三.回圈結構
1.while回圈
用法展示
注意事項
break和continue的用法
2.for回圈
用法展示
注意事項(與while回圈相似)
3.do while回圈
用法展示
注意事項
四.輸入和輸出
1.輸出到控制臺
2.從鍵盤輸出
用法展示1
用法展示2
注意事項
五.練習
1.列印年齡
2.判斷素數
3.列印素數
4.輸出閏年
5.列印乘法口訣表
6.找出最大公約數
7.計算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
8.統計數字9
9.計算水仙花數
10.模擬輸密碼場景
11.回傳引數二進制中1的個數
12. 獲取一個數二進制序的所有奇、偶數位
13. 輸出整數的每一位
14.猜數字游戲
一.順序結構
用法展示
public class Test {
public static void main(String[] args) {
System.out.println("hello");
System.out.println("world");
}
}
用法很簡單,需要注意的是如果調整代碼的書寫順序, 則執行順序也發生變化
二.分支結構
1.if陳述句
用法展示1-基本用法
public class TestDemo {
public static void main(String[] args) {
int a = 10;
int b = 10;
if(a==b){//括號里面的是一個布爾運算式,當運算式為真時執行if中的陳述句
System.out.println("相等");
}else{//運算式為假時執行else里的陳述句
System.out.println("不相等");
}
}
}
用法展示2-多分支情況
public class TestDemo {
public static void main(String[] args) {
int a = 10;
int b = 10;
if(a>b){//條件時執行的代碼
System.out.println("a大于b");
}else if(a<b){//條件滿足時執行的代碼
System.out.println("a小于b");
}else{//條件都不滿足時執行的代碼
System.out.println("a與b相等");
}
}
}
注意事項
1.else匹配時,跟最近的if陳述句匹配,if else陳述句可以不加大括號,但是這種情況只能寫一行代碼,建議都加上大括號,可閱讀性和效率高是檢驗代碼好不好的重要標準,
public class TestDemo {
public static void main(String[] args) {
int x = 10;
int y = 10;
if (x == 10)
if (y == 10)
System.out.println("hello");
else
System.out.println("hi");
}
}
2.還需要注意的是,if()后面不能加分號,加分號會導致分號成為if陳述句中執行的代碼
2.switch陳述句
用法展示
public class TestDemo {
public static void main(String[] args) {
int day = 1;
switch (day) {//switch里的內容可以有:整數、列舉、字符、字串
case 1://根據 switch 中值的不同,會執行對應的 case 陳述句,遇到 break 就會結束該 case 陳述句
System.out.println("星期一");
break;//執行代碼后跳出程式,防止繼續運行下去
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期天");
break;
default://如果 switch 中的值沒有匹配的 case,就會執行 default 中的陳述句,建議switch陳述句都加上default
System.out.println("請輸入1-7有效數字!");
break;
}
}
}
注意事項
1.不要忘記break!!!如果不寫break,程式會一直運行下去,從而失去了多分支選擇的效果
public class TestDemo {
public static void main(String[] args) {
int day = 1;
switch (day) {
case 1:
System.out.println("星期一");
case 2:
System.out.println("星期二");
default:
System.out.println("請輸入1-7有效數字!");
}
}
}

2.注意switch陳述句里面的型別:整數、列舉、字符、字串,型別不對程式將無法運行
3.switch不能表達復雜的條件,這是它的一個局限性,例如num > 10 && num < 20
4.switch陳述句雖然支持嵌套,但是嵌套起來代碼并不易讀,所以建議不要使用
三.回圈結構
1.while回圈
用法展示
public class TestDemo {//以1-10的和為例
public static void main(String[] args) {
int n = 0;
int sum = 0;
while(n<10){//當n<10時執行下面的代碼
n += 1;
sum += n;
}
System.out.println(sum);
}
}
注意事項
1. 和 if 類似, while 下面的陳述句可以不寫 { } , 但是不寫的時候只能支持一條陳述句,建議還是加上 { }
2. 和 if 類似, while 后面不要多寫 分號, 否則可能導致回圈不能正確執行
3.一定要注意代碼的終止條件!如果沒有終止條件,代碼會陷入死回圈
break和continue的用法
1.break:讓回圈提前結束,需要注意的是,break只能結束離它最近的回圈,無法結束所有回圈

2.continue:跳過這次回圈, 立即進入下次回圈

2.for回圈
用法展示
public class TestDemo {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println("sum = " + sum);
}
}

注意事項(與while回圈相似)
1. 和 if 類似, for 下面的陳述句可以不寫 { } , 但是不寫的時候只能支持一條陳述句,建議還是加上 { }
2. 和 if 類似, for后面不要多寫 分號, 否則可能導致回圈不能正確執行
3.do while回圈
用法展示
public class TestDemo {
public static void main(String[] args) {
int n =0;
int sum = 0;
do {
n++;
sum += n;
}while(n!=10);//先回圈一次再判斷條件
System.out.println(sum);
}
}
注意事項
1.do while回圈一般不常用,建議使用while回圈或者for回圈
2.while后面有個分號千萬別忘了!!
四.輸入和輸出
1.輸出到控制臺
public class TestDemo {
public static void main(String[] args) {
System.out.println("hello"); // 輸出一個字串, 帶換行
System.out.print("hello"); // 輸出一個字串, 不帶換行
System.out.printf("%d", 1123); // 類似c語言中的printf
}
}
2.從鍵盤輸出

用法展示1
用 Scanner 讀取字串/整數/浮點數
import java.util.Scanner; // 需要匯入 util 包
public class TestDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入你的姓名:");
String name = scanner.nextLine();
System.out.println("請輸入你的年齡:");
int age = scanner.nextInt();
System.out.println("請輸入你的工資:");
float salary = scanner.nextFloat();
System.out.println("你的資訊如下:");
System.out.println("姓名: "+name+"\n"+"年齡:"+age+"\n"+"工資:"+salary);
scanner.close(); // 注意, 要記得呼叫關閉方法
}
}
用法展示2
使用 Scanner 回圈讀取 N 個數字
import java.util.Scanner; // 需要匯入 util 包
public class TestDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sum = 0.0;
int num = 0;
while (scanner.hasNextDouble()) {//連續讀取多個數字
double tmp = scanner.nextDouble();
sum += tmp;
num++;
}
System.out.println("sum = " + sum);
System.out.println("avg = " + sum / num);
scanner.close();
//當輸入多個物件時,使用ctrl+d/z停止輸入資料
}
}

注意事項
直接使用 System.in.read 可以讀入一個字符, 但是需要搭配例外處理,用法比較麻煩,這里不作說明,
五.練習
學習完的朋友可以適當練習增加熟練度,以下是筆者的解法,如有更好的解法歡迎交流,如有錯誤請斧正,謝謝大家
1.列印年齡
//1. 根據年齡, 來列印出當前年齡的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int age = scanner.nextInt();
if(0<=age && age<=18){
System.out.println("少年");
}else if(18<age && age<=28){
System.out.println("青年");
}else if(28<age && age<=55){
System.out.println("中年");
}else if(age>55){
System.out.println("老年");
}else{
System.out.println("請重新輸入");//當輸入資料錯誤時
}
scanner.close();
}
}

2.判斷素數
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
System.out.println("請輸入一個數:");
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
for (int i = 2; i <x ; i++) {
if(x%i==0){
System.out.println(x+"不是素數");
break;//已經確定不是素數,不用繼續計算
}else{
System.out.println(x + "是素數");
break;
}
}
scanner.close();
}
}

3.列印素數
//3. 列印 1 - 100 之間所有的素數
//ps:列印素數!不是列印非素數
public class Test3 {
public static void main (String []args){
int i=0;
int j=0;
for (i=1;i<=100;i++){//里面不能寫int i,因為已經定義過i了
for(j=2;j<i;j++){
if(i%j==0){
break;
}
}
if(j==i){
System.out.println(i + " ");
}
}
}
}

4.輸出閏年
public class Test4 {
public static void main(String[] args) {
for (int year = 1000; year <=2000 ; year++) {
if(year%100!=0){
if(year%4==0){
System.out.println(year+"是閏年!");
}
}else if(year%400==0){
System.out.println(year+"是閏年!");
}
}
}
}

5.列印乘法口訣表
public class Test5 {
public static void main(String[] args) {
int x = 0;//定義乘積
for (int i = 1; i <=9; i++) {
for (int j = 1; j <=i ; j++) {
x=i*j;
System.out.print(j+"*"+i+"="+x+"\t");//使用水平制表符對齊操作
if(j==i){//當i=j時,進行換行操作
System.out.println();
}
}
}
}
}

6.找出最大公約數
//6. 求兩個正整數的最大公約數
//Q: 怎么只列印最大的公約數——使用輾轉相除法
import java.util.Scanner;
public class Test6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入兩個正整數:");
int x = scanner.nextInt();
int y = scanner.nextInt();
//除了用do while回圈 也能用while回圈
do{
int i=x%y;
x = y;//除數賦值為被除數
y = i;//余數賦值給除數,直到余數為0
}while(y!=0);//直到y=0時停止運行
System.out.println(x);
scanner.close();
}
}

7.計算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
public class Test7 {
public static void main(String[] args) {
double sum = 0.0;
int flg=1;
for (int i = 1; i <=100; i++) {
sum = sum+ flg*1.0/i ;
flg=-flg;
}
System.out.println(sum);
}
public static void main1(String[] args) {
double sum1=0.0;
double sum2=0.0;
for (int i = 1; i <=100 ; i++) {
if(i%2==0){
double m = -1.0/i;
sum1 += m;
}else{
double n = 1.0/i;
sum2 += n;
}
}
double sum = sum1+sum2;
System.out.println(sum);
}
}

8.統計數字9
public class Test8 {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <=100 ; i++) {
if(i%10 == 9){//先計算個位數是9的情況
count++;
}if(i/10 == 9){//再計算十位數是9的情況,不能加else,會排除99的情況,99個位數十位數均為9
count++;
}
}
System.out.println(count);
}
}

9.計算水仙花數
public class Test9 {
public static void main(String[] args) {
for (int i = 100; i <=999 ; i++) {
int c = i%10;//計算個位
int b = (i%100-c)/10;//計算十位
int a = i/100;//計算百位
int m = a*a*a + b*b*b + c*c*c;//符合水仙花數的條件
if(i == m){
System.out.println(i);
}
}
}
}

10.模擬輸密碼場景
import java.util.Scanner;
public class Test10 {
public static void main(String[] args) {
int count = 3;
while(count!=0) {
System.out.println("請輸入你的密碼:");
Scanner scanner = new Scanner(System.in);
String password = scanner.nextLine();
if (password.equals("200267")) {//注意這里是用.equals()來判斷字串是否相等
System.out.println("登陸成功!");
return;//密碼輸入成功后不再執行
scanner.close();
} else {
count--;
System.out.println("密碼錯誤,還可以輸入:" + count + "次");
}
}
}
}

11.回傳引數二進制中1的個數
public class test11 {
public static void main(String[] args) {
System.out.println(func(15));
}
public static int func(int n){
int count = 0;
while(n!=0){
count++;
n = n & (n-1);
}
return count;
}
}

原理如圖

12. 獲取一個數二進制序的所有奇、偶數位
//12.獲取一個數二進制序列中所有的偶數位和奇數位, 分別輸出二進制序列,
public class Test12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入數字:");
int n = scanner.nextInt();
System.out.println("偶數序列為");
for (int i = 31; i >0 ; i-=2) {
System.out.print((n>>i)&1);//這里采用不換行輸出
System.out.print(" ");
}
System.out.println();
System.out.println("奇數序列為:");
for (int i = 30; i >=0 ; i-=2) {
System.out.print((n>>i)&1);
System.out.print(" ");
}
scanner.close();
}
}

13. 輸出整數的每一位
//13.輸出一個整數的每一位.
import java.util.Scanner;
public class Test13 {//使用遞回實作
public static void main(String[] args) {
System.out.println("請輸入一個整數:");
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
print(num);
scanner.close();
}
public static void print(int n){
if(n>9){
print(n/10);
}
System.out.print(n%10+" ");
}
}

14.猜數字游戲
import java.util.Random;
import java.util.Scanner;
public class Test14 {
public static void main(String[] args) {
Random random = new Random();//默認隨機種子是系統時間
int num = random.nextInt(100);//
while(true) {
System.out.println("請輸入1-100中的一個數字:");
Scanner scanner = new Scanner(System.in);
int ans = scanner.nextInt();
if (ans > num) {
System.out.println("猜大了!");
} else if (ans < num) {
System.out.println("猜小了!");
} else {
System.out.println("恭喜你!猜對了!");
break;
}
scanner.close();
}
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/330379.html
標籤:其他
上一篇:這部分布式事務開山之作,憑啥第一天預售就拿下當當新書榜No.1?
下一篇:小程式購物城專案實戰(上篇)
