Scanner对象
用户交互scanner
1.使用next
package Scanner;
import java.util.Scanner;
public class Demon01 {
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();
}
}
2.使用nextLine
package Scanner;
import java.util.Scanner;
public class Demon02 {
public static void main(String[] args) {
//new.Scanner是从键盘接收数据
Scanner scanner = new Scanner(System.in);
System.out.println("使用nextLine方式接受:");
//判断是否还有输入
if(scanner.hasNext()){
String str = scanner.nextLine();
System.out.println("输出的内特容为:"+str);
}
scanner.close();
}
}
Scanner进阶
package Scanner;
import java.util.Scanner;
public class Demon05 {
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 x = scanner.nextDouble();
m = m+1; //m++
sum = sum + x;
System.out.println("你输入了"+m+"个数据,当前结果为sum="+sum);
}
System.out.println(m+"个数的和为" + sum);
System.out.println(m+"个数的平均值是"+ (sum/m));
scanner.close();
}
}
/*运行过程
请输入数据
10
你输入了1个数据,当前结果为sum=10.0
30
你输入了2个数据,当前结果为sum=40.0
50
你输入了3个数据,当前结果为sum=90.0
70
你输入了4个数据,当前结果为sum=160.0
h
4个数的和为160.0
4个数的平均值是40.0
Process finished with exit code 0
*/
顺序结构
选择结构
if单选择结构
package struct;
import java.util.Scanner;
public class IfDemon01 {
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();
}
}
/*请输入内容:
Hello
Hello
End
Process finished with exit code 0 */
if双选择结构
package struct;
import java.util.Scanner;
public class IfDemon02 {
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();
}
}
/*
请输入成绩:
60
及格
Process finished with exit code 0
*/
if多选择结构
package struct;
import java.util.Scanner;
public class ifDemon03 {
public static void main(String[] args) {
//考试分数大于60就是几个,小于60就不及格
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("A");
}else if (score<90 && score>=80){
System.out.println("B");
}else if (score<80 && score>=70){
System.out.println("C");
}else System.out.println("成绩不合法");
scanner.close();
}
}
switch多选择结构
package struct;
public class SwitchDemon01 {
public static void main(String[] args) {
//case穿透,在每一个case后边加一个break是防止穿透,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("挂科");
default:
System.out.println("未知等级");
}
}
}
//结果 未知等级
字节码,用IDEA将class文件导入,可以反编译,查看源代码。
循环结构
While循环
package struct;
public class WhileDemon01 {
public static void main(String[] args) {
//输出1-100
int i = 0;
while (i < 100) {
i++;
System.out.println(i);
}
}
}
package struct;
//计算1+2+3+。。。。。。100?
public class WhileDemon03 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while (i<=100){
sum = sum+i;
i++;
}
System.out.println(sum);
}
}
DoWhile循
package struct;
public class DoWhileDemon01 {
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 struct;
public class DoWhileDemon02 {
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);
a++;
}while (a<0);
}
}
/* 输出结果
==================
0
Process finished with exit code 0 */
for循环
package struct;
public class ForDemon01 {
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循环有以下几点说明;
最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
然后,检测布尔表达式的值。若为true,循环体被执行。若为false,循环终止,开始执行循环体后面的语句。
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
再次检测布尔表达式。循环执行上面的过程
*/
//死循环
for(; ; ){
}
}
}
package struct;
public class ForDemon02 {
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 struct;
//练习2:用While或for循环输出1-1000之间能被5整除的数,并且每行输出3个
public class ForDemon03 {
public static void main(String[] args) {
for (int i = 1; i <= 1000; i++) {
if(i%5==0){
System.out.print(i+"\t"); //print输出完不会换行, /t是自动对齐
}
if (i%(5*3)==0){ //每行println输出完会换行,是指三个一行输出
System.out.println();
//同 System.out.println("\n");
}
}
}
}
package struct;
//9*9乘法表
public class ForDemon04 {
public static void main(String[] args) {
//1.先打印第一列,√
//2.我们把固定的1在用一个循环包起来
//3.去掉重复项 i<=j
//4.调整样式 "\t"【自动对齐】和System.out.println();【表示换行】的加入
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(); //System.out.println(\n);
}//print不换行,println换行。
}
}
/*结果
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
*/
增强for循环
package struct;
public class ForDemon05 {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};//定义了一个数组
for (int i = 0; i < 5; i++) {
System.out.println(numbers[i]);
}
System.out.println("==================");
//遍历数组的元素 下边属于增强for循环,语句更简单
for (int x:numbers){
System.out.println(x);
}
}
}
package struct;
public class BreakDemon {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i==30){
break;
}
}
System.out.println("123");
}
}
/*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
123
*/
package struct;
public class ContinueDemon {
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);
}
}
//break在任何循环语句的主体部分,均可用break控制循环的流程
//break用于强行退出循环,不执行循环终剩余的语句。(break语句也在switch语句中使用)
//continue语句在循环语句体中,用于种植某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定
//break属于辞职(全部终止),continue属于请假(只终止一小部分)
}
流程控制练习(不太懂,再看看)
package struct;
public class TestDemon {
public static void main(String[] args) {
//打印三角形 5行 就是先打几个空格,再打一个*
/*
*
***
*****
*******
*********
学会用debug一步一步分析
*/
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();
}
}
}