java.util.Scanner使用这个包中Scanner类获取用户的输出
基本语法
Scanner s = new Scanner(System.in);
通过Scanner类的next() 与 nextLine() 方法获取输入的字符串,在读取前我们要使用hasNext() 与 hasNextLine()判断用户是否还有输入的数据
一定要读取到有效字符后才可以结束输出
对于输入有效字符之前遇到的空白,next()方法会自动去除
只有输入有效字符后才将其后面的空白作为分隔符或者结束符
next() 不能得到带有空格的字符串
package com.arbedu.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()) { String str = scanner.next(); System.out.println("输入的内容为:" + str); } //凡是属于IO流的类如果不关闭就会一直占用资源,要养成习惯用完就关掉 scanner.close(); } }
输入:Hello world
输出: Hello
以Enter(回车)为结束符,返回输入回车之前的所有字符
可以获得空白
package com.arbedu.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(); } }
输入:Hello world
输出:Hello world
Java的基本结构
顺序结构是最简单的算法结构
从上到下
package com.arbedu.structure; import java.util.Scanner; public class ifDemo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入内容:"); String s = scanner.nextLine(); //equals:判断字符串是否相等 if(s.equals("Hello")) { System.out.println(s); } System.out.println("End"); scanner.close(); } }
package com.arbedu.structure; import java.util.Scanner; 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.arbedu.structure; import java.util.Scanner; public class ifDemo03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); //其中一个else if 为true 其他语句不执行 if (score == 100) { System.out.println("恭喜满分"); }else if(score < 100 && score >= 90){ System.out.println("A"); }else if(score < 90 && score >= 80){ System.out.println("B"); }else if(score < 80 && score >= 70){ System.out.println("C"); }else if(score < 70 && score >= 60){ System.out.println("D"); }else if(score < 60 ){ System.out.println("不及格"); }else { System.out.println("成绩不合法"); } } }
if() { if() { } }
package com.arbedu.structure; public class switchDemo01 { public static void main(String[] args) { char grade = 'C';//JDK7新特性 switch可以识别string 利用反编译 hashcode switch (grade) { case 'A': System.out.println("优"); break;// 打破 不执行后续case及default 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("未知"); break; } } }
基本格式
while (布尔表达式) { // 循环体 }
实例
package com.arbedu.structure; public class WhileDemo01 { public static void main(String[] args) { // 输出1 - 100 int i = 0;// 初始化 //条件判断 while (i < 100) { i++; //迭代 循环体 System.out.println(i); //循环体 } } }
死循环
package com.arbedu.structure; public class WhileDemo02 { public static void main(String[] args) { while (true) { // 等待客户端连接 // 定时检查 // while(true)是死循环 除非是特殊需求,一般情况下避免死循环,会影响程序性能或是系统卡死崩溃 } } }
求和练习
package com.arbedu.structure; public class WhileDemo03 { public static void main(String[] args) { // 计算1+2+3+...+100 int i = 1; int sum = 0; while (i <= 100) { sum += i; i++; } System.out.println(sum); } }
至少执行1次
package com.arbedu.structure; 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); } }
while和do-while比较
package com.arbedu.structure; public class DoWhileDemo02 { public static void main(String[] args) { int a= 0; while(a<0){ System.out.println(a); // 无输出结果 a++; } System.out.println("================="); do{ System.out.println(a); //输出0 a++; }while(a<0); } }
package com.arbedu.structure; public class ForDemo01 { public static void main(String[] args) { int a = 1; //初始化 while(a <= 100) { System.out.print(a+" "); //循环体 a++; //迭代 } System.out.println("while循环结束"); // 写for的快捷方式:长度.for再回车 本代码可以用100.for for (int i = 1; i <= 100; i++) { System.out.print(i+" "); } System.out.println("for循环结束"); // for死循环 /*for(; ;) { } */ } }
for循环练习题
package com.arbedu.structure; 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) {// 偶数 evenSum += i; }else {//奇数 oddSum += i; } } System.out.println("奇数的和"+oddSum); System.out.println("偶数的和"+evenSum); } }
package com.arbedu.structure; public class ForDemo03 { public static void main(String[] args) { // 练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出三个 for (int i = 1; i < 1000; i++) { if (i % 5 == 0) { // 能被5整除 System.out.print(i+"\t"); //让结果更整齐 } if(i % 15 == 0) { // 每行输出三个:每过增加15(3 * 5)就换行 System.out.println(); //换行 System.out.print("\n");也行 } } } }
package com.arbedu.structure; public class ForDemo04 { public static void main(String[] args) { // 练习3:打印99乘法表 /* 1 * 1 = 1 1 * 2 = 2 2 * 2 = 4 1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 ... 规律:每行都从1到本行数截至 第二个乘数即为行数 */ for (int i = 1; i <= 9; i++) { for(int j = 1; j <= i; j++) { System.out.print(j+"*"+i+"="+(i*j)+"\t"); } System.out.println(); } } }
package com.arbedu.structure; public class TestDemo { public static void main(String[] args) { // 打印三角形 /* * *** ***** ******* */ for (int i = 1; i <= 5; i++) { for (int j = 5; j >= i; j--) { System.out.print(" "); // 打印空格 空格数随行数增加而减少 } for (int j = 1; j <= i; j++) { System.out.print("*"); // 打印左半部分三角形 *个数与行数相等 } for (int j = 1; j < i; j++) { System.out.print("*"); // 打印右半部分三角形 每行*个数比左半部分少一个 } System.out.println(); // 换行 } //可以自行调试理解本章循环的量的变化 } }
package com.arbedu.structure; public class ForDemo05 { public static void main(String[] args) { int[] arr = {1,2,3,4,5,6,7,8,9,10}; // 定义了一个数组 // 增强for 用于遍历数组与集合 for (int x:arr) { System.out.println(x); } System.out.println("====================="); // 普通for for (int i = 0; i < 10; i++) { System.out.println(arr[i]); } } }
package com.arbedu.structure; public class BreakDemo01 { public static void main(String[] args) { int i = 0; while(i < 100) { i++; System.out.println(i); if(i == 30) { break; // 打破循环 不执行循环体内剩余语句(switch语句也常用) } } System.out.println("123"); //仍然执行 } }
package com.arbedu.structure; public class ContinueDemo01 { public static void main(String[] args) { int i = 0; while (i < 100) { i++; if(i %10 == 0) { System.out.println(); //使缺少的数字清晰可见 continue; // 终止本次循环 不执行本次循环剩余语句 执行下一次循环 } System.out.println(i); } } }
package com.arbedu.structure; public class LabelDemo01 { public static void main(String[] args) { // 打印101 - 105 的质数 int count = 0; // 不建议使用label outer:for(int i = 101; i < 150; i++) { for (int j = 2; j < i / 2; j++) { if (i % j == 0) { //判断质数 continue outer; } } System.out.print(i + " "); } } }