" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaSE的邏輯控制你了解嗎

 更新時間:2022年03月14日 15:09:24   作者:Living_Amethyst  
這篇文章主要為大家詳細介紹了JavaSE的邏輯控制,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

1. Java中程序的邏輯控制語句

1.1順序結(jié)構(gòu)

順序結(jié)構(gòu)比較簡單,按照代碼書寫的順序一行一行執(zhí)行

1.2分支結(jié)構(gòu)

1.2.1 switch語句

這里補充一道筆試題:Java中不能做switch參數(shù)的數(shù)據(jù)類型有哪些?

  • long類型
  • float類型
  • double類型
  • boolean類型
 switch語句執(zhí)行流程:

1. 先計算表達式的值

2. 和case依次比較,一旦有響應(yīng)的匹配就執(zhí)行該項下的語句,直到遇到break時結(jié)束

3. 當(dāng)表達式的值沒有與所列項匹配時,執(zhí)行default

switch 不能表達復(fù)雜的條件

// 例如: 如果 num 的值在 10 到 20 之間, 就打印 hehe
// 這樣的代碼使用 if 很容易表達, 但是使用 switch 就無法表示.
if (num > 10 && num < 20) {
    System.out.println("hehe");
}

1.3循環(huán)結(jié)構(gòu)

  • while循環(huán)
  • for循環(huán)
  • do while 循環(huán)

2.輸入輸出

2.1輸出到控制臺

基本語法

System.out.println(msg); // 輸出一個字符串, 帶換行
System.out.print(msg); // 輸出一個字符串, 不帶換行
System.out.printf(format, msg); // 格式化輸出
  • println 輸出的內(nèi)容自帶 \n, print 不帶 \n
  • printf 的格式化輸出方式和 C 語言的 printf 是基本一致的

代碼示例

System.out.println("hello world");
int x = 10;
System.out.printf("x = %d\n", x)

 格式化字符串

轉(zhuǎn)換符類型舉例
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.2 從鍵盤輸入

2.2.1第一種方法(不常用)

char i = (char) System.in.read();
System.out.println("your char is:"+i);

 當(dāng)遇到這樣的情況,只需要按一下 alt+回車即可

2.2.2使用 Scanner 讀取字符串/整數(shù)/浮點數(shù)

Scanner scanner = new Scanner(System.in);
//輸入整型數(shù)
int n = scanner.nextInt();
System.out.println(n);
//輸入浮點數(shù)
float a = scanner.nextFloat();
System.out.println(a);
//輸入字符串
String str= scanner.nextLine();
System.out.println(str);

一些解釋:

當(dāng)我們運行代碼,發(fā)現(xiàn)了一些問題 

 

在讀取字符串時

2.2.3實現(xiàn)循環(huán)輸入

//循環(huán)輸入
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextInt()){
int n=scanner.nextInt();
System.out.println(n);

注:如果想要看源代碼

 就可以看到了

3.猜數(shù)字游戲

import java.util.Random;
import java.util.Scanner;
public class TestDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //生成隨機數(shù)
        //Random random = new Random();
        Random random = new Random(1234);//括號內(nèi)放一個數(shù)字,每次生產(chǎn)隨機數(shù)都是根據(jù)這個數(shù)生成的
        int randNum = random.nextInt(100);//在括號內(nèi)輸入100表示隨機數(shù)的范圍是[0,100)
        int randNum2 = random.nextInt(100)+1;//表示[1,100]或[1,101)
        while(true)
        {
            System.out.println("請輸入你要猜的數(shù)字: ");
            int num= scanner.nextInt();
            if(num<randNum){
                System.out.println("猜小了!");
            }else if(num==randNum){
                System.out.println("猜對了!");
                break;
        }else{
                System.out.println("猜大了!");
            }
        }
    }
}

分析:

生成隨機數(shù)

運行程序

成功!

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!  

相關(guān)文章

最新評論