一、Scanner对象
1、java.util.Scanner是Java5的新特征,我们可以通过Scanner类获取用户的输入;
2、基本语法:Scanner s = new Scanner(System.in);
3、案例:next()方法与nextLine()区别;
next()
一定要读取到有效字符后才可以结束输入;(必须输入,否则不停止)
对输入的的有效字符之前遇到的空白,next()方法会自动去掉空白;
只有输入有效字符后才能将其后面输入的空白作为分割符或者结束符;
**next()不能得到带有空格的字符;**如Hello World,只输出Hello,空格之后的World不会输出;
Scanner scanner = new Scanner(System.in);
System.out.println("使用next方式接受:");
//判断用户还有没有输入字符串
if (scanner.hasNext()){
//使用next方法接受
String next = scanner.next();
System.out.println("输出的内容为:"+next);
// 使用next方式接受:
// s ddddd
// 输出的内容为:s
}
//关闭资源,io流的类如果不关闭会占用资源
scanner.close();
nextLine()
回车就结束,可以输出回车之后的所有字符;
可以获取中间有空格的字符;
Scanner scanner = new Scanner(System.in);
System.out.println("用nextLine方式接受:");
//判断是否还有输入
if (scanner.hasNextLine()){
String next = scanner.nextLine();
System.out.println("输出为:"+next);
// 用nextLine方式接受:
// s dddd
// 输出为:s dddd
}
//关闭资源,io流的类如果不关闭会占用资源
scanner.close();
4、Scanner对象的进阶使用
public static void main(String[] args) {
//求和与平均数
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//输入了多少个数
int count = 0;
//通过循环判断是否还有输入,并对里面每一次进行求和统计
while (scanner.hasNextDouble()) {
double v = scanner.nextDouble();
count++;
sum = sum + v;
System.out.println("输入的第" + count + "个数为:" + v);
}
System.out.println(count + "个数的和为:" + sum);
System.out.println(count + "个数的平均数为:" + (sum / count));
scanner.close();
}
二、顺序结构
1、java的基本结构就是顺序结构,除非特别指明,否则按照一句一句的顺序执行;
2、顺序结构是最简单的算法结构;
三、if选择结构
1. if单选择结构:
2、if双选择结构
3、if多选择结构
4、嵌套if结构
四、Switch选择结构
1、switch case语句判断一个变量与一系列值中某个值是否相等。每个值称为一个分支;
2、switch语句中的变量类型可以是:int、char、byte、short;
3、从javase7开始,switch支持String类型;
4、同时case标签必须为字符串常量或者字面量;
case穿透:如果不写break,会执行后面的case语句;
五、while循环
1、while是最基本的循环,基本结构为
//输出1-100;
int i = 0;
while(i < 100){
//循环内容
i++;
System.out.print(i);
}
2、只要布尔表达式为true,循环就会一直执行
案例:计算1-100的和?
public static void main(String[] args) {
//求和1+2+。。+100=
int i = 0;
int sum = 0;
while (i < 100){
i++;
sum = sum + i;
}
System.out.println(sum);
}
六、DoWhile循环
1、对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次;
2、do…while循环,与while循环不同的是,do…while循环至少执行一次;do while先执行后判断,while先判断后执行!
七、For循环
1、for循环是支持迭代的一种通用结构,是最有效、最灵活的循环结构;
2、案例:
public static void main(String[] args) {
//计算1-100之间奇数和偶数的和?
/*
* 最先执行初始化步骤,可以声明一种类型,但可初始化一个或者多个循环控制变量,也可以是空语句;
* 然后检测布尔表达式的值,如果为true,继续执行,false终止,执行后面的语句
* 执行一次循环后,更循环控制变量
* 再检测布尔表达式,循环执行上面过程
* */
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i <= 100; i++) {
//i % 2==0,说明是偶数,否则是奇数
if (i % 2 == 0){
sum1 = sum1 + i;
}else {
sum2 = sum2 + i;
}
}
System.out.println("1-100偶数和为:"+sum1);
System.out.println("1-100奇数和为:"+sum2);
}
public static void main(String[] args) {
//输出1-1000之间能被5整除的数,并且每行输出3个
//引入一个计数器,每计算一次加一次
int count = 0;
for (int j = 1; j <= 1000; j++) {
if (j % 5 == 0) {
System.out.print(j + "\t");
count++;
if (count % 3 == 0){
System.out.println();
}
}
/* if (j % (3*5) == 0){//换行,被5整除,每行的结尾肯定被15整除
System.out.println("");
}*/
}
}
public static void main(String[] args) {
//打印九九乘法表
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();
}
}
八、增强for循环
public static void main(String[] args) {
int[] num = {1,23,4,5};
for (int i = 0; i < num.length - 1; i++) {
System.out.println(num[i]);
}
System.out.println("------------");
//增强for循环,遍历数组的元素
for (int i : num) {
System.out.println(i);
}
}