Scanner对象
java.until.Scanner是Java5的新特性:可以通过Scanner类来获取用户的输入
next():
- 读取到有效字符才结束输入;
- 有效字符前的空白,自动抹去
- 输入有效字符后,后面输入的空白作为分隔符或者结束符。
- next()不能输入空格;
package com.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();
}
}
// 使用next方式接收:
// chen shuang
// 输出的内容为:chen
nextline():
- 以Enter当作结束符,
- 可以获得空白
package com.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();
}
}
// 使用nextline方法接收:
// chen shuang
// 输出内容是:chen shuang
进阶用法:
hasNextDouble()-- NextDouble() hasNextFloat() -- NextFloat() hasNextInt() -- NextInt()
package com.scanner;
import java.util.Scanner;
public class Demo03 {
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();
}
}
//运行结果
请输入一个整数
2
该整数为:2
请输入一个小数:
1.2
该小数为:1.2
练习:我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果;
package com.scanner;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sum=0;
int count=0;
System.out.println("请输入数字:");
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
if (x==888)
break;
sum = sum + x;
count++;
System.out.println("当前输入了第:"+count+"个数。总和为:"+sum+"!");
}
System.out.println("输入了:"+count+"个数");
System.out.println("总和为:"+sum);
System.out.println("平均值为:"+(sum/count));
scanner.close();
}
}
//运行结果
请输入数字:
10
当前输入了第:1个数。总和为:10.0!
20
当前输入了第:2个数。总和为:30.0!
5.5
当前输入了第:3个数。总和为:35.5!
5.55
当前输入了第:4个数。总和为:41.05!
888
输入了:4个数
总和为:41.05
平均值为:10.2625
顺序结构
语句和语句之间,框与框之间从上而下顺序执行。基本的算法结构
选择结构
if单选择结构
语法:
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}
练习:
package com.struct;
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("End")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
//执行结果
请输入内容:
chenshuang
End
if双选择结构
语法:
if (布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为flase
}
练习:
package com.struct;
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();
}
}
//执行结果
请输入考试分数:
80
成绩及格!
if多选择结构
语法:
if(布尔表达式1){
//如果布尔表达式1的值为true执行代码
}else if (布尔表达式2){
//如果布尔表达式2的值为true执行代码
}else if (布尔表达式3){
//如果布尔表达式2的值为true执行代码
}else {
//如果布尔表达式都不为true执行代码
}
练习:
package com.struct;
import java.util.Scanner;
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 >= 90 && score <100) {
System.out.println("A等");
} else if (score >= 80 && score < 90) {
System.out.println("B等");
} else if (score >= 70 && score < 80) {
System.out.println("C等");
} else if (score >= 60 && score < 70) {
System.out.println("C等");
}else if (score>0 && score<60){
System.out.println("成绩不及格");
} else {
System.out.println("成绩不合法");
}
scanner.close();
}
}
//执行结果
请输入成绩:
88
B等
嵌套的if结构
if(布尔表达式1){
//如果布尔表达式1的值为true执行代码
if(布尔表达式2){
//如果布尔表达式2的值为true执行代码
}
}
Switch多选择结构
switch语句中的变量类型可以是:
- byte ,short , int , char
- 从Java SE 7 开始,switch支持String类型了
- case标签必须为字符串常量或者自变量
switch(expresstion){
case value :
//语句
break;
case value :
//语句
break;
case value :
//语句
break;
default:
//语句
}
练习:
package com.struct;
import java.util.Scanner;
public class SwitchDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入等级:");
String grade = scanner.next();
//case穿透--如果不加break一直顺序执行
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("位置等级");
}
scanner.close();
}
}
//执行结果
请输入等级:
D
再接再厉
循环结构
while循环
while(布尔表达式){
//循环内容
}
- 布尔表达式为true,程序就会一直循环下去
- 大部分时候会让循环停下来,写判断表达式
- 避免死循环的发生
练习:
package com.struct;
public class WhileDemo01 {
public static void main(String[] args) {
//输出0-100;
int x = 0 ;
while(x<100){
x++;
System.out.println(x);
}
}
}
//执行结果:
1 2 3 4 5 ………………………… 99 100
do…while循环
至少执行一次
do{
////循环内容
}(布尔表达式);
练习:
package com.struct;
public class DoWhiledEemo01 {
public static void main(String[] args) {
int x = 0 ;
int sum = 0;
do {
x++;
sum=x+sum;
}while (x < 100);
System.out.println("结果为"+sum);
}
}
//执行结果:
结果为5050
For循环
支持迭代的通用结构,是最有效,最灵活的循环结构
for(初始化;布尔表达式;更新){
//循环体
}
练习1:计算0–100之间,奇数的和、偶数的和;
public class ForDemo01 {
public static void main(String[] args) {
//计算0--100之间,奇数、偶数的和;
int oddsum=0;
int evensum=0;
for (int i=0;i<100;i++){
if(i%2==0){
//偶数
evensum=evensum+i;
}else {
//奇数
oddsum=oddsum+i;
}
}
//执行结果:
偶数和为:2450
奇数和为:2500
练习2:循环1–1000之间能被5整除的数,并且每行输出三个
public class ForDemo02 {
public static void main(String[] args) {
//循环1--1000之间能被5整除的数,并且每行输出三个
for(int i = 1;i<=1000;i++) {
if (i % 5 == 0) {
System.out.print(i + "\t");
if (i%3==0){
System.out.print("\n");
// System.out.println();
}
}
}
}
}
练习3:打印九九乘法表
package com.struct;
public class ForDemo03 {
public static void main(String[] args) {
//打印九九乘法表
int result=0;
for (int i = 1; i <=9; i++) {
for (int j = 1; j <=i; j++) {
result=i*j;
System.out.print(j+"*"+i+"="+result+"\t"+"\t");
}
System.out.println();
}
}
}
//执行结果:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
增强For循环
for(声明语句:表达式){
//代码句子
}
- 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时的数组元素的值相等。
- 表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
遍历数组:
package com.struct;
public class ForDemo04 {
public static void main(String[] args) {
int[] numbers = {10,32,34,56,45};//定义一个数组
//遍历数组
for (int x: numbers) {
System.out.println(x);
}
}
}
//执行结果:
10
32
34
56
45
break、continue
break:退出循环,顺序执行代码
package com.struct;
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while(i<100){
i++;
System.out.print(i+"\t");
if (i==10){
break; //退出循环
}
}
}
}
//执行结果:
1 2 3 4 5 6 7 8 9 10
continue:结束本次循环
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue; //结束本次循环,继续执行新的循环
}
System.out.print(i+"\t");
}
}
}
//执行结果:
1 2 3 4 5 6 7 8 9
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
31 32 33 34 35 36 37 38 39
41 42 43 44 45 46 47 48 49
51 52 53 54 55 56 57 58 59
61 62 63 64 65 66 67 68 69
71 72 73 74 75 76 77 78 79
81 82 83 84 85 86 87 88 89
91 92 93 94 95 96 97 98 99
练习:打印五行三角形
public class TextDemo01 {
public static void main(String[] args) {
//打印一个三角形,五行;
for (int i = 1; i <=5 ; i++) {
for (int j =0; j <(5-i) ; j++) {
System.out.print("\u0020");
}
for (int k = 0; k <(2*i-1) ; k++) {
System.out.print("*");
}
System.out.println();
}
}
}