编程语言
首页 > 编程语言> > 【计理01组02号】程序流程之分支

【计理01组02号】程序流程之分支

作者:互联网

if-then 语句

查看代码
import java.util.Scanner;
public class Main1{
public static void main (String[] args) {

	Scanner scan=new Scanner(System.in);
	System.out.print("The number is:");
	int n=scan.nextInt();
	if(n>0)
		System.out.println("The number is positive");
	else
		System.out.println("The number is zero or negative");
}
}

相等运算符

查看代码
import java.util.Scanner;
public class Main1{
public static void main (String[] args) {

	Scanner scan=new Scanner(System.in);
	System.out.println("The number a is:");
	int a=scan.nextInt();
	System.out.println("The number b is:");
	int b=scan.nextInt();
	if(a==b)
		System.out.println("The two numbers are equal");
	else
		System.out.println("The two numbers are not equal");

}
}

switch语句

查看代码
import java.util.Scanner;
public class Main1{
public static void main (String[] args) {

	Scanner scan=new Scanner(System.in);
	System.out.println("Please choose your number:0,1,2");
	int number=scan.nextInt();
	switch(number) {
	case 0: System.out.println("0");
	case 1: System.out.println("1");
	case 2: System.out.println("2");
	}

}
}

标签:02,01,Scanner,scan,System,程序流程,number,println,out
来源: https://www.cnblogs.com/yyyyfly1/p/15743452.html