Scanner
Scanner s=new Scanner(System.in);
System.out.println("使用next方式接受");
//判断用户有没有输入字符串
if(s.hasNext())
{
String s1=s.next();
System.out.println("输出内容为:" +s1);
}
s.close();
//使用next方式接受
//hello word!
//输出内容为:hello
Scanner s=new Scanner(System.in);
System.out.println("使用nextLine方式接受");
//判断用户是否还有输入字符串
if(s.hasNextLine())
{
String s1=s.nextLine();
System.out.println("输出内容为:" +s1);
}
s.close();//关闭Scanner,凡是属于IO流的类如果不关闭会一直占用资源
/**
使用next方式接受
hello word!
输出内容为:hello word!
*/
next()
- 一定要读取到有效字符后才可以结束输入
- 对输入有效字符之前遇到空白,next()方法会自动去掉
- 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符
- next()不能得到带有空格的字符串
next Line()
- 以enter为结束符,也就是说next Line()方法返回的是输入回车之前的所有字符
- 可以获得空白
Scanner s=new Scanner(System.in);
//和
double sum=0;
//计算输入了多少个数字
int m=0;
//通过循环判断是否还有输入,并在里面对每一次进行求和和统计
while (s.hasNextDouble()){
double x=s.nextDouble();
m = m+1;
sum = sum + x;
System.out.println("你输入了第"+m+"个数,当前和为"+sum);
}
System.out.println(m+"个数和为:"+sum);
System.out.println(m+"个数平均值为:"+(sum/m));
s.close();
/*
10
你输入了第1个数,当前和为10.0
20
你输入了第2个数,当前和为30.0
30
你输入了第3个数,当前和为60.0
15
你输入了第4个数,当前和为75.0
45
你输入了第5个数,当前和为120.0
50
你输入了第6个数,当前和为170.0
x
6个数和为:170.0
6个数平均值为:28.333333333333332
*/
顺序结构
从上到下依次进行
选择结构
if单选择结构
Scanner s=new Scanner(System.in);
System.out.println("请输入一个内容:");
String s1=s.nextLine();
if (s1.equals("1231")){
System.out.println(s1);
}
System.out.println("end");
s.close();
/*
12 1231
end 1231
end
if双选择结构
Scanner s=new Scanner(System.in);
System.out.println("请输入成绩:");
int s1=s.nextInt();
if (s1 >= 60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
s.close();
/*
50 60
不及格 及格
if 多选择结构
Scanner s=new Scanner(System.in);
System.out.println("请输入成绩:");
double s1=s.nextDouble();
if (s1 == 100){
System.out.println("恭喜满分");
}else if (s1 < 100 && s1 >=90){
System.out.println("A");
}else if (s1 < 90 && s1 >=70){
System.out.println("B");
}else if (s1 < 70 && s1 >=60){
System.out.println("C");
}else if (s1 < 60 && s1 >=0){
System.out.println("不及格");
}else {
System.out.println("成绩不合法" +
"");
}
s.close();
//其中有一个为true,则跳过执行
if嵌套结构
if(布尔值){
if(布尔值){
if(布尔值){
..
..
}
}
}
switch 多选择结构
char grade = 'A';
switch (grade){
case 'A':
System.out.println("优");
break;//一定要加上,否则case穿透,会一直往下执行
case 'B':
System.out.println("良");
break;
case 'C':
System.out.println("中");
break;
case 'D':
System.out.println("差");
break;
default:
System.out.println("未知等级");
}
循环结构
while循环
循环结构:
while(布尔表达式){
循环内容
}
- 只要布尔表达式为true ,循环就一直执行下去
- 我们大多数情况是会让循环停止下来,我们需要一个表达式失效的方式来结束循环
- 少部分情况需要一直循环,比如服务器的请求响应监听等
- 循环条件一直为true就会造成死循环,我们正常业务编程中应该尽量避免死循环。否则会影响程序性能,造成程序卡死崩溃
//输出1-100
int i = 0;
while (i < 100){
i++;
System.out.println(i);
}
//计算1+2+3+4+..+100
int i = 0;
int sum = 0;
while (i < 100){
i++;
sum = sum + i;
}System.out.println(sum);
}
do…while循环
循环结构:
do{
循环语句
}while(布尔表达式)
while 与 do…while循环的区别
- while先判断后执行, do…while是先执行后判断!
- do…while保证循环体至少被执行一次
//计算1+2+3+4+..+100
int i = 0;
int sum = 0;
do{
i++;
sum = sum + i;
}while (i < 100);
System.out.println(sum);
For循环
for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构
循环结构:
for(初始化;布尔表达式;更新){
代码语句
}
//输出1-100
int a = 0;
while (a<=100){
System.out.println(a);
a+=2;
}
System.out.println("while 循环结束");
for (int i=0;i<=100;i++){
System.out.println(i);
}
System.out.println("for循环结束");
/**
* 关于for循环有一下几点说明
* 1.最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句
* 2.检测布尔表达式的值,如果为true,循环体被执行。如果为false,循环体被终止,开始执行循环体后面的语句
* 3.执行一次循环体后更新循环控制变量(迭代因子控制循环变量的增减)
* 4.再次检测布尔表达式,循环执行上面的过程
*/
//死循环
for ( ; ; ) {
}
//计算1+2+3+..+100的和
int sum = 0;
for (int i=0;i<=100;i++){
sum = sum + i;
} System.out.println(sum);
//计算0-100的奇数和偶数的和
int addSum = 0;
int evenSum = 0;
for (int i = 0; i < 100; i++) {
if((i % 2) != 0){
addSum+=i;
}else {
evenSum+=i;
}
}
System.out.println("奇数的和"+addSum);
System.out.println("偶数的和"+evenSum);
}
//用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
int i = 0;
while (i < 1000) {
if ((i % 5) == 0) {
System.out.print(i+"\t");
}
if ((i % (5 * 3)) == 0){
System.out.println( );
}
i++;
}
for (int i1 = 0; i1 < 1000; i1++) {
if((i1 % 5) == 0){
System.out.print(i1+"\t");
}
if ((i1 % (5 * 3)) == 0){
System.out.println( );
}
}/println 输出完会换行
//print 输出完不会换行
/**
* 1. 我们先打印第一列
* 2. 把固定的1再用循环包起来
* 3. 去掉重复项,i <= j
* 4. 调整样式
*/
//打印九九乘法表
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( );
}
增强for循环
主要是用来遍历数组和集合
循环结构:
for(声明语句:表达式)
{
代码语句
}
- 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组的值相等
- 表达式:表达式是要访问的数组名,或者是返回值为数组的方法
//遍历数组的元素
int [ ] num= {10,20,30,40,50};//定义了一个数组
for (int x : num){
System.out.print(x+"\t");
}
for(int i = 0; i<5;i++){
System.out.println(num[i]);
}
break continue
break
强制退出循环(循环体之后的语句正常执行)
int i = 0;
while (i<100){
i++;
System.out.println(i);
if(i == 10){
break;
}
}
// 12345678910
continue
用于终止某次循环过程(跳过某次循环)
int i = 0;
while (i<50){
i++;
if((i %10)==0){
System.out.println(" ");
continue;
}System.out.print(i);
}
/**
* 123456789
* 111213141516171819
* 212223242526272829
* 313233343536373839
* 414243444546474849
*/
打印三角形
//打印三角形
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(" ");
}