Java教程

【计理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");
	}

}
}
这篇关于【计理01组02号】程序流程之分支的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!