目錄
一、順序結構
二、分支結構
????????1.if語句
??????????2.swich語句
三、循環(huán)結構
? ? ? ? 1.while循環(huán)
? ? ? ? 2.break
? ? ? ? 3.continue
? ? ? ? 4.for循環(huán)
? ? ? ? 5.do while循環(huán)
四、輸入輸出
? ? ? ? 1.輸出到控制臺
? ? ? ? 2.從鍵盤輸入
一、順序結構
? ? ? ? 按照代碼的書寫結構一行一行執(zhí)行。
????????System.out.println("aaa");
????????System.out.println("bbb");
????????System.out.println("ccc");? ? ??
?????? ? 調整代碼順序
????????System.out.println("bbb");
????????System.out.println("ccc");????????System.out.println("aaa");
?
?
二、分支結構
????????1.if語句
? ? ? ? if(布爾表達式){
? ? ? ? //語句
????????}
????????如果布爾表達式結果為true,執(zhí)行if中的語句,否則不執(zhí)行。
????????int gor=100; ????????if(gor>90) { ???????? System.out.println(gor); ????????}
? ? ? ? ?if(布爾表達式){
? ? ? ? ? ? ? ? //語句1
? ? ? ? ? }else{
????????????????//語句2
??????????}
????????如果布爾表達式結果為true,則執(zhí)行if中語句,否則執(zhí)行else中語句。
????????int a = 92;
????????if(a >= 90){
????????System.out.println("足夠大");
????????}else{
????????System.out.println("太小");
????????
?????????if(布爾表達式){
? ? ? ? ? ? ? ?????????//語句1
? ? ? ? }else if(布爾表達式){
? ? ? ? ? ? ? ? ????????//語句2
????????}else{
? ? ? ? ? ? ? ????????? //語句3
????????}
????????int a = 92; ????????if(a >= 90){ ????????System.out.println("優(yōu)秀"); ????????}else if(a>=60){ ????????System.out.println("良"); ????????} ????????else{ ????????System.out.println("不及格"); ????????}
??????
??????????2.swich語句
? ? ? ? ? ?switch(表達式){
????????????????case 常量值1:{
????????????????????????????????語句1;
????????????????????????????????break;
????????????????????????????????}
????????????????case 常量值2:{
????????????????????????????????語句2;
????????????????????????????????break;
????????????????????????????????}? ? ? ? ? ? ? ? ...
????????????????default:{
?????????????????????內容都不滿足時執(zhí)行語句;
? ? ? ? ? ? ? ? ? ? ?break;
?????????????????????}? ? ? ? ? ? ? ?}
執(zhí)行順序:先計算表達式的值,之后和case依次比較,一旦有響應的匹配就執(zhí)行該項下的語句,直到遇到break時結束,當表達式的值沒有與所列項匹配時,執(zhí)行default。
?
int day=2; switch(day) { case 1: 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: System.out.println("輸入有誤"); break; }
? ? ? ? ?注意:多個case后的常量值不可以重復,switch的括號內只能是以下類型的表達式:基本類型:byte、char、short、int,不能是long類型,引用類型:String常量串、枚舉類型。
? ? ? ? break不能遺漏,否則就會失去多分支效果。
????????int day = 1;
????????switch(day) {
????????case 1:
????????System.out.println("星期一");
????????// break;
????????case 2:
????????System.out.println("星期二");
????????break;? ? ? ? }
? ? ? switch不能表達復雜條件,其雖可以嵌套但不推薦。
三、循環(huán)結構
? ? ? ? 1.while循環(huán)
? ? ? ? while(循環(huán)條件){
? ? ? ? ? ? ? ? 循環(huán)語句;
????????}
? ? ? ? 循環(huán)條件為true時, 則執(zhí)行循環(huán)語句,?否則結束循環(huán)。
示例 打印1-20的數(shù)字
????????int num = 1;
????????while (num <= 20) {
????????System.out.println(num);
????????num++;
????????}
? ? ? ? ?注意:當while的循環(huán)語句只有一條時,while 下面的語句可以不寫 { },建議還是加上 { }。
? ? ? ? 2.break
????????break 是讓循環(huán)提前結束。
? ? ? ? 示例:找100-200中第一個3的倍數(shù)
????????int num = 100;
????????while (num <= 200) {
? ? ? ? ? ?if (num % 3 == 0) {
? ? ? ? ? ? ? ? ? System.out.println("找到了 3 的倍數(shù), 為:" + num);
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? }? ? ? ? ? ? num++;
? ? ? ? }
? ? ? ? 3.continue
? ? ? ? ? continue 的功能是跳過這次循環(huán), 立即進入下次循環(huán).
? ? ? ? 示例找到100-200中所有三的倍數(shù)
????????int num = 100;
????????while (num <= 200) {
????????????????if (num % 3 != 0) {
????????????????num++;
????????????????continue;
????????????????}????????????????System.out.println("找到了 3 的倍數(shù), 為:" + num);
????????????????num++;
? ? ? ? ?}
?
? ? ? ? 4.for循環(huán)
? ? ? ? for(表達式1;表達式2;表達式3){
? ? ? ? ? ? ? ? 表達式4;
}
????????表達式1:用于初始化循環(huán)變量初始值設置,在循環(huán)最開始時執(zhí)行,且只執(zhí)行一次,表達式2循環(huán)條件,滿則循環(huán)繼續(xù),否則循環(huán)結束,表達式3?循環(huán)變量更新方式。
示例 打印1-100的和
int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } System.out.println("sum = " + sum);
?
? ? ? ? 5.do while循環(huán)
????????do{
? ? ? ? ? ?循環(huán)語句;
????????}while(循環(huán)條件);
????????先執(zhí)行循環(huán)語句, 再判定循環(huán)條件,循環(huán)條件成立則繼續(xù)執(zhí)行,否則循環(huán)結束
示例 打印1-10
????????int num = 1;
????????do {
? ? ? ? ? ?System.out.println(num);
? ? ? ? ? ?num++;
? ? ? ? ?} while (num <= 10);
? ? ? ? ?注意:do while 循環(huán)最后的分號不要忘記。
四、輸入輸出
? ? ? ? 1.輸出到控制臺
System.out.println(msg);// 輸出一個字符串, 帶換行 System.out.print(msg);//輸出一個字符串, 不帶換行 System.out.printf(format, msg); // 格式化輸出,printf 的格式化輸出方式和 C 語言的 printf 是基本一致的
? ? ? ? 示例
????????System.out.println("hello world");
????????int x=10;????????System.out.printf("x=%d\n",x);
?格式化字符串
轉換符 | 類型 | 舉例 | 結果 |
d | 十進制整數(shù) | (“%d”,100) | 100 |
x | 十六進制整數(shù) | (“%x”,100) | 64 |
o | 八進制整數(shù) | (“%o”,100) | 144 |
f | 定點浮點數(shù) | (“%f”,100f) | 100.000000 |
e | 指數(shù)浮點數(shù) | (“%e”,100f) | 1.000000e+02 |
g | 通用浮點數(shù) | (“%g”,100f) | 100.000 |
a | 十六進制浮點數(shù) | (“%a”,100) | 0x1.9p6 |
s | 字符串 | (“%s”,100) | 100 |
c | 字符 | (“%c”,'1') | 1 |
b | 布爾值 | (“%b”,100) | true |
h | 散列碼 | (“%h”,100) | 64 |
% | 百分號 | (“%.2f%%”,2/7f) | 0.29% |
? ? ? ? 2.從鍵盤輸入
????????使用 Scanner 讀取字符串/整數(shù)/浮點數(shù)
?
import java.util.Scanner; // 需要導入 util 包
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你的姓名:");
String name = sc.nextLine();//輸入字符串,也可以用next,但若字符串中有空格,使用next則只打印空格前的的字符串System.out.println(name);
Scanner sc=new Scanner(System.in); String name = sc.next(); System.out.println(name);
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你的年齡:");
int age = sc.nextInt();System.out.println(age);
?
????????Scanner sc=new Scanner(System.in);
????????System.out.println("請輸入工資");
????????float salary =sc.nextFloat();
????????System.out.println(salary);
?
import java.util.Scanner; // 需要導入 util 包
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你的姓名:");
String name = sc.nextLine();
System.out.println("請輸入你的年齡:");
int age = sc.nextInt();
System.out.println("請輸入你的工資:");
float salary = sc.nextFloat();
System.out.println("你的信息如下:");
System.out.println("姓名: "+name+"\n"+"年齡:"+age+"\n"+"工資:"+salary);
sc.close(); // 注意, 要記得調用關閉方法
?
?循環(huán)輸入n個整數(shù)并求和讀取平均值
Scanner sc = new Scanner(System.in);
int sum = 0;
int num = 0;//判斷是否有整數(shù)
while (sc.hasNextInt()) {
int tmp = sc.nextInt();
sum += tmp;
num++;
} S
System.out.println("sum = " + sum);
System.out.println("avg = " + sum / num);
sc.close();
文章來源:http://www.zghlxwxcb.cn/news/detail-608970.html
注意:當循環(huán)輸入多個數(shù)據(jù)的時候, 使用 ctrl + z 來結束輸入 (Windows 上使用 ctrl + z(cmd),ctrl(IDEA), Linux / Mac 上使用 ctrl+ d)。文章來源地址http://www.zghlxwxcb.cn/news/detail-608970.html
到了這里,關于Java-邏輯控制的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!