需要用到java.util.Scanner类
package com.mlj.coreJava.day3;
import java.util.Scanner;
public class TestScanner {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数:");
int a = scanner.nextInt();
System.out.println(a + "收到了!");
}
}
运行结果:
读入整数、字符串、小数分别用Scanner类中的nextInt()、nextLine()、nextDouble()方法
语法:if(布尔表达式){
代码块1;
}else{
代码块2;
}
package com.mlj.coreJava.day3;
import java.util.Scanner;
public class TestBigger {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num1 = scanner.nextInt();
System.out.println("请输入第二个整数:");
int num2 = scanner.nextInt();
if (num1>num2){
System.out.println("num1最大");
}else {
System.out.println("num2最大");
}
}
}
语法:if(条件1){
条件1代码块;
}else if(条件2){
条件2代码块;
}else if(条件3){
条件3代码块;
}
package com.mlj.coreJava.day3;
import java.util.Scanner;
public class TestScop {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的成绩");
int scop = scanner.nextInt();
if (scop>0 && scop<60){
System.out.println("很遗憾!你不及格");
}else if (scop>=60 && scop<80){
System.out.println("恭喜你!你及格了");
}else if (scop>=80 && scop<=100){
System.out.println("成绩优秀");
}else {
System.out.println("成绩输入不合法");
}
}
}
语法:switch(value){
case value1:
语句块1;
break;
case value2:
语句块2;
break;
case value2:
语句块2;
break;
default:语句块;
}
注意:*value只能是int byte char short*
package com.mlj.coreJava.day3;
import java.util.Scanner;
public class TestSwitch {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("1:手机充值\n2:人工服务\n3:业务办理\n...");
System.out.println("请输入您需要哪种服务:");
int num = sc.nextInt();
switch (num){
case 1:
System.out.println("进入手机充值");
break;
case 2:
System.out.println("进入人工服务");
break;
case 3:
System.out.println("进入业务办理");
break;
default:System.out.println("输入有误!");
}
}
}
语法:for(初始化;循环条件;迭代操作){
循环体;
}
package com.mlj.coreJava.day3;
public class TestFor {
public static void main(String args[]){
for (int i = 10;i>0;i--){
System.out.println(i+"\n");//输出10-1的整数
}
}
}
语法:while(循环条件){
循环体;
}
package com.mlj.coreJava.day3;
import java.util.Scanner;
public class TestWhile {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = scanner.nextInt();
int count = 0;//运行次数
while (num > 5) {
System.out.println(num);
num--;
count++;
}
System.out.println("运行次数:"+count);
}
}
语法:do{
循环体;
}while(循环条件);
package com.mlj.coreJava.day3;
import java.util.Scanner;
public class TestWhile {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = scanner.nextInt();
int count = 0;//运行次数
do {
System.out.println(num);
num--;
count++;
} while (num > 5);
System.out.println("运行次数:" + count);
}
}
两者区别:do…while至少执行一次
break:跳出当前的循环
continue:跳出本次循环
package com.mlj.coreJava.day3;
public class Testbc {
public static void main(String args[]){
for (int i = 10;i>0;i--){
if (i==5) continue;
System.out.println(i);
}
}
}
package com.mlj.coreJava.day3;
public class Testbc {
public static void main(String args[]) {
for (int i = 10; i > 0; i--) {
if (i == 5) break;
System.out.println(i);
}
}
}
打印 *
* * *
* * * * *
package com.mlj.coreJava.day3;
import java.util.Scanner;
public class TestPrint {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = scanner.nextInt();
for (int i = 1; i <= num; i++) {//控制行数
for (int j = 1; j <= num - i; j++) {//控制空格数
System.out.print(" ");
}
for (int m = 1; m <= 2 * i - 1; m++) {//控制*数
System.out.print("*");
}
System.out.println();
}
}
}
package com.mlj.coreJava.day3;
public class TestMultiplication {
public static void main(String args[]) {
for (int i = 1; i <= 9; i++) {
for (int n = 1; n <= i; n++) {
System.out.print(n + " x " + i + " = " + i * n + "\t");
}
System.out.println();
}
}
}