Java流程控制
用户交互Scanner
我们可以通过Scanner类来获取用户的输入
通过Scanner类的next()与nextLine()方法获取输入的字符串
在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据
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();
}
}
Scanner的进阶使用
public class Demo04 {
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();
}
}
public class Demo05 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过非数字来结束输入并输出执行结果
Scanner scanner = new Scanner(System.in);
//和
double sum = 0;
//计算输入了多少个数字
int m = 0;
//通过循环判断是否还有输入,并在里面对每一次进行求和和统计
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();
}
}
顺序结构
JAVA的基本结构就是顺序结构,它是任何一个算法都离不开的一种基本算法结构
If 选择结构
public class IfDemo03 {
public static void main(String[] args) {
//考试分数大于60分就是及格,小于60分就不及格
Scanner scanner = new Scanner(System.in);
/*
if 语句至多有 1 个 else 语句,else 语句在所有的 else if 语句之后
if 语句可以有若干个 else if 语句,它们必须在 else 语句之前
一旦其中一个 else if 语句检测为 true,其他的 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();
}
//equals:判断字符串是否相等
}
Switch选择结构
public class SwitchDemo02 {
public static void main(String[] args) {
String name = "熊大";
int i = name.hashCode();
System.out.println(i); //查了一下我名字的hashCode:956485
//JDK7的新特性,表达式结果可以是字符串!!!
//字符的本质还是数字
//反编译 java---class(字节码文件)---反编译(IDEA)
switch (name){
case "熊二":
System.out.println("熊二");
break;
case "吉吉":
System.out.println("吉吉");
break;
default:
System.out.println("搞错了!");
}
}
}
While和DoWhile循环
public class DoWhileDemo02 {
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);
}
}
For循环
for循环是最有效、最灵活的循环结构
for循环执行的次数是在执行前就确定的
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循环有以下几点说明:
最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句
然后,检测布尔表达式的值,如果为true,循环体被执行;如果为false,循环终止,开始执行循环体后面的语句
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)
再次检测布尔表达式,循环执行上面的过程
*/
//死循环
for (; ; ) {
}
}
}
public class ForDemo02 {
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; //oddSum = oddSum + i;
} else { //偶数
evenSum += i;
}
}
System.out.println("奇数的和为:" + oddSum);
System.out.println("偶数的和为:" + evenSum);
}
}
public class ForDemo03 {
public static void main(String[] args) {
//练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
for (int i = 1; i <= 1000; i++) {
if (i%5==0) {
System.out.print(i+"\t");
}
if (i%(5*3)==0) {
System.out.println();
//System.out.print("\n");
}
}
//println 输出完会换行
//print 输出完不会换行
}
}
public class ForDemo04 {
public static void main(String[] args) {
//练习3:打印九九乘法表
//1.我们先打印第一列
//2.将固定的1再用一个循环包起来
//3.去掉重复项,i <= j
//4.调整样式
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(i+"*"+j+"="+(i*j) + "\t");
}
System.out.println();
}
}
}
public class TestDemo {
public static void main(String[] args) {
//打印三角形 5行
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();
}
}
}
增强For循环
Java5 引入了一种主要用于数组或集合的增强型for循环
public class ForDemo05 {
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 (int x:numbers) {
System.out.println(x);
}
}
}
break、continue、goto
public class BreakDemo {
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");
}
}
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);
}
//break:在任何循环语句的主体部分,均可用break控制循环的流程
// break用于强行退出循环,不执行循环中剩余的语句(break语句也在switch语句中使用)
//
//continue:用在循环语句体中,用于终止某次循环过程
// 即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定
}
}
public class LableDemo {
public static void main(String[] args) {
//打印101-150之间所有的质数
//质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数
//除了本身,一个数的因数一定小于等于他的一半
//不建议使用
//goto在Java中未得到正式使用,用带标签的break和continue实现goto
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+" ");
}
}
}