使用者互動Scanner
Scanner s = new Scanner(Syatem.in);
package com.kuang.Scanner;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
//建立一個掃描器物件,用於接受鍵盤資料
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接收:");
//判斷使用者有沒有輸入字串
if (scanner.hasNext()){
//使用next方式接收
String str = scanner.next();
System.out.println("輸入的內容為:"+str);
}
//凡是屬於IO流的類如果不關閉會一直佔用資源,要養成好習慣用完就關掉
scanner.close() ;
}
}
package com.kuang.Scanner;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
//從鍵盤接收資料
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接收:");
//判斷使用者有沒有輸入字串
if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("輸入的內容為:"+str);
}
scanner.close() ;
}
}
package com.kuang.Scanner;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
//從鍵盤接收資料
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入資料:");
String str = scanner.nextLine();
System.out.println("輸入的內容為:"+str);
scanner.close() ;
}
}
package com.kuang.Scanner;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//從鍵盤接收資料
int i= 0;
float f = 0.0f;
System.out.println("請輸入整數:");
//如果..那麼..
if (scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整數資料:"+i);
}else{
System.out.println("輸入的不是整數資料!");
}
System.out.println("請輸入小數:");
//如果..那麼..
if (scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小數資料:"+f);
}else{
System.out.println("輸入的不是小數資料!");
}
scanner.close() ;
}
}
package com.kuang.Scanner;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//我們可以輸入多個數字,並求其總和與平均數,每輸入一個數字用回車確認,通過輸入非數字來結束輸入並輸出執行結果
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//計算輸入了多少個數字
int m = 0;
System.out.println("請輸入資料:");
//通過迴圈判斷是否還有輸入,並在裡面對每一次進行求和與統計
while (scanner.hasNextDouble()){
double v = scanner.nextDouble();
//
m = m + 1;//m++
sum = sum + v;
}
System.out.println(m+"個數的和為"+sum);
System.out.println(m+"個數的平均值是"+(sum/m));
scanner.close() ;
}
}
順序結構
package com.kuang.struct;
public class ShunXuDemo {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
}
}
if選擇結構
package com.kuang.struct;
import java.util.Scanner;
//if單選澤結構
public class IfDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入內容:");
String s = scanner.nextLine();
//equasl:判斷字串是否相等
if (s.equals("Hello") ){
System.out.println(s);
}
System.out.println("End");
scanner.close() ;
}
}
import java.util.Scanner;
//if雙選擇結構
public class IfDemo02 {
public static void main(String[] args) {
//考試分數大於60分及格,小於60分不及格
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = scanner.nextInt();
if (score>=60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close() ;
}
}
package com.kuang.struct;
import java.util.Scanner;
//if多選擇結構
public class IfDemo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = scanner.nextInt();
if (score==100){
System.out.println("完美");
}else if (score<100 && score>=90){
System.out.println("優秀");
}else if (score<90 && score>=80){
System.out.println("良");
}else if (score<80 && score>=60){
System.out.println("及格");
}else if (score<60){
System.out.println("不及格");
}else {
System.out.println("成績不合法");
}
scanner.close();
}
}
switch選擇結構
package com.kuang.struct;
public class SwitchDemo01 {
public static void main(String[] args) {
//case穿透 //switch 匹配一個具體的值
char grade = 'F';
switch (grade){
case 'A':
System.out.println("優秀");
break;
case 'B' :
System.out.println("良好");
break;
case 'C' :
System.out.println("及格");
break;
case 'D' :
System.out.println("再接再厲");
break;
case 'E' :
System.out.println("回家去吧");
break;
default :
System.out.println("未知等級");
}
}
}
package com.kuang.struct;
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "北極";
//JDK7新特性,表示式結果可以是字串
//字元的本質是數字
//反編譯 java--class(位元組碼檔案)---反編譯(IDEA)
switch(name){
case"南極":
System.out.println("南極");
break;
case"北極":
System.out.println("北極");
break;
default:
System.out.println("?");
}
}
}
while迴圈
package com.kuang.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//輸出1~100
int i = 0;
while (i<100){
i++;
System.out.println(i);
}
}
}
package com.kuang.struct;
public class WhileDemo02 {
public static void main(String[] args) {
//死迴圈
while (true){
//等待客戶端連線
//定時檢查
//。。。。。。。
}
}
}
package com.kuang.struct;public class WhileDemo03 { public static void main(String[] args) { //計算1+2+3+·····+100 int i = 0; int sum = 0; while(i<=100){ sum = sum + i; i++; } System.out.println(sum); }}
do···while迴圈
- do···while是先執行後判斷,while先判斷後執行
package com.kuang.struct;public class DoWhileDemo01 { public static void main(String[] args) { int i = 0; int sum = 0; do { sum = sum + i; i++; }while(i<=100); System.out.println(sum); }}
package com.kuang.struct;public class DoWhileDemo01 { public static void main(String[] args) { int i = 0; int sum = 0; do { sum = sum + i; i++; }while(i<=100); System.out.println(sum); }}
for迴圈
- for迴圈語句是支援迭代的一種通用結構,是最有效、最靈活的迴圈結構
package com.kuang.struct;public class ForDemo01 { public static void main(String[] args) { int a = 1;//初始化條件 while(a<=100){//條件判斷 System.out.println(a);//迴圈體 a+=2;//迭代 } System.out.println("while迴圈結束!"); //初始化//條件判斷//迭代 for (int i=1;i<=100;i++){ System.out.println(i); } System.out.println("for迴圈結束!"); //死迴圈 for (;;){ } }}
package com.kuang.struct;public class ForDemo02 { public static void main(String[] args) { //練習1:計算0到100之間的奇數和偶數的和 int oddSum = 0; int evenSum = 0; for (int i = 0; i <= 100; i++) { if (i%2!=0){ oddSum+=i; }else{ evenSum+=i; } } System.out.println("奇數的和:"+oddSum); System.out.println("偶數的和: "+evenSum); }}
package com.kuang.struct;public class ForDemo03 { public static void main(String[] args) { //練習2:用while或for迴圈輸出1~1000之間能被5整除的數,並且每行輸出3個 for (int i = 0; i < 1000; i++) { if (i%5==0){ System.out.print(i+"\t"); } if (i%(5*3)==0){ System.out.println(); //System.out.print("\n"); } } //println 輸出完會換行 //print 輸出完不會換行 }}
package com.kuang.struct;public class ForDemo04 { public static void main(String[] args) { /*練習:列印99乘法表 1.先列印第一列 2.把固定的1再用一個迴圈包起來 3.去掉重複項,i<=j */ for (int j = 1; j <= 9; j++) { for (int i = 1; i <= j; i++) { System.out.print(j+"*"+i+"="+(j+i) +"\t"); } System.out.println(); } }}