2.1 Scanner对象
通过Scanner类来获取用户的输入,实现程序与人的交互。
基本语法:
Scanner类有next()和nextline()两种方法获取输入的字符串,在读取前要使用hasNext()和hasNextline()判断是否还有输入的数据。
next():
1.一定要读取到有效字符后才可结束输入。
2.对输入有效字符之前的空白,next()方法会自动去掉。
3.只有输入有效字符后才将其后面输入的空白作为分隔符或结束符。
4.next()不能得到带有空格的字符串
nextLine():
1.以enter作为结束符,即nextLine()方法返回的是输入回车前的所有字符
2.可以获得空白
例1.
public class Demo01 {
public static void main(String[] args) {
//创建一个扫描器对象,用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("请输入:");
//使用next方式接收
String str = scanner.next(); //程序会等待用户输入完毕
System.out.println("输出的内容为:"+str);
/*凡是属于IO流的类,若不关闭就会一直占用资源,要养成用完就关的好习惯*/
scanner.close();
}
}
例2.
public class Demo04 {
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;
sum=sum+x;
System.out.println("你输入了第"+m+"个数据,然后当前结果sum="+sum);
}
System.out.println(m+"个数的和为"+sum);
System.out.println(m+"个数的平均值是"+(sum/m));
scanner.close();
}
}
2.2顺序选择结构
1.顺序结构
·Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。
·顺序结构是最简单的算法结构。
·语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,任何一个算法都离不开顺序结构这种基本算法结构。
2.选择结构if
(1)if单选结构
·语法
(2)if双选结构
·语法
(3)if多选结构
·语法
(4)if嵌套
·语法
例:
public class IfDemo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
/*if语句
最多有一个else语句,else语句在所有else if语句之后
可以有若干个else if语句,它们必须在else语句之前
一旦其中一个else if语句检测为真,其他的else if和else语句都将跳过执行
*/
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 if (score<70 && score>=60){
System.out.println("D");
}else if (score<60 && score>=0){
System.out.println("不及格");
}else {
System.out.println("输入不合法");
}
scanner.close();
}
}
3.选择结构switch
·switch case语句判断一个变量与一系列值中某个值是否相等,每个值为称为一个分支
·switch语句中的变量类型可以是:byte、short、int或char
从JDK7开始,switch支持字符串String类型了
·case标签必须为字符串常量或字面量
·语法:
例1:
public class SwitchDemo01 {
public static void main(String[] args) {
//case穿透
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("未知等级");
}
}
}
例2:
public class SwitchDemo02 {
public static void main(String[] args) {
String name ="王怡";
//JDK7的新特性,表达式结果可是字符串
//字符的本质还是数字
//反编译 java--class(字节码文件)---反编译(IDEA)
switch (name){
case "王怡":
System.out.println("王怡");
break;
case "xiaowang":
System.out.println("xiaowang");
break;
default:
System.out.println("芽儿哦");
}
}
}
2.3循环结构
1.while循环
·while是最基本的循环,语法为:
while(布尔表达式){
//循环内容
}
·只要布尔表达式为true,循环就会一直执行下去。
·我们一般会让循环停止下来,所以需要一个让表达式失效的方式来结束循环。
·少部分情况需要循环一直执行,比如服务器的请求响应监听等。
·循环条件一直为true会造成死循环,在编程中尽量避免
例1:计算1+2+3……+100的和
public class WhileDemo02 {
public static void main(String[] args) {
int i=0;
int sum=0;
while (i<100){
i++;
sum+=i;
}
System.out.println(sum);
}
}
2.do…while循环
·对于while语句而言,若不满足条件,则不能进入循环。但有时我们需要至少执行一次,就可用到do…while循环。
·do…while循环和while循环相似,语法:
do{
//代码语句
}while(布尔表达式);
·while和do…while的区别:
(1) while先判断后执行。dowhile是先执行后判断!
(2) do…while总是保证循环体会被至少执行一次!这是两者的主要差别。
例2:比较while与dowhile
public class DowhileDemo02 {
public static void main(String[] args) {
int a=0;
while (a<0){
System.out.println(a);//由于a=0,表达式为false,不执行
a++;
}
System.out.println("===============");
do {
System.out.println(a);//dowhile至少执行一次,故输出0
a++;
}while (a<0);
}
}
3.for循环
·所有循环结构都可用while或dowhile表示,但Java提供了另一种语句——for循环来使一些循环结构变得简单。
·for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。
·for循环执行的次数实在执行前就确定的,语法:
for(初始化; 布尔表达式; 更新){
//代码语句
}
例3:区别for和while
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循环说明
1.最先执行初始化步骤,可声明一种类型,也可初始化一个或多个循环控制变量,也可是空语句
2.然后检查布尔表达式的值,若为true,循环体被执行,若为false,循环终止,开始执行循环体后的语句
3.执行一次循环后,更新循环控制变量
4.再次检测布尔表达式,循环执行上面的过程
*/
}
}
例4:打印九九乘法表
public class ForDemo04 {
public static void main(String[] args) {
//1.先打印第一列
//2.把固定的1用循环包起来
//3.去掉重复项
//4.调整格式
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+j*i+" ");
}
System.out.println();
//println 输出完会换行
//print 输出完不会换行
}
}
}
4.增强for循环 (先做了解)
·增强for循环主要用于数组或集合的增强型for循环。
语法:
for(声明语句 :表达式)
{
//代码语句
}
·声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
·表达式:表达式是要访问的数组名,或者是返回为数组的方法
例5:打印数组
public class ForDemo05 {
public static void main(String[] args) {
int[] n={10,20,30,40,50};//定义一个数组
for (int i = 0; i < 5; i++) {
System.out.println(n[i]);
}
System.out.println("===============");
//增强for循环
for (int x:n){//遍历数组元素
System.out.println(x);
}
}
}
5.break和continue
·在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。
·continue语句在循环体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。
·(1)Java没有goto,但带标签的break和continue和goto的用法相似。
(2)“标签”是指后面跟一个冒号的标识符,例如:label:
(3)Java唯一用到标签的地方在循环语句之前,作用是在循环中嵌套另一个循环,由于break和continue通常只中断当前循环,但若随标签使用,它们就会中断到存在标签的地方.
例6:
public class BreakDemo01 {
public static void main(String[] args) {
int i=0;
while (i<100){
i++;
System.out.println(i);
if (i==30)
break; //跳出循环,执行下面的代码
}
System.out.println("12341");
}
}
例7:
public class ContinueDemo {
public static void main(String[] args) {
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+" ");
}
}
}
例8:打印三角形 五行
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();
}
}
}