//Java if条件分支语句
/*
if (表达式) /*若表达式成立则执行复合语句,否则不执行*/
{
//语句
}
*/
/*
/*if (表达式) /*若表达式成立则执行语句1,否则执行语句2*/
{
//语句1
}
else
{
//语句2
}
*/
/*
if (表达式1) /*若表达式1成立则执行语句1,否则继续判断表达式2*/
{
//语句1
}
else if (表达式2) /*若表达式2成立则执行语句2,否则继续判断表达式3*/
{
//语句2
}
else if (表达式3) /*若表达式3成立则执行语句3,否则继续判断下一个表达式*/
{
//语句3
}
……
else /*若以上表达式都不成立,则执行语句4*/
{
//语句4
}*/
public class Main {
public static void main(String args[]) {
int x = 3;
if (x == 1) {
System.out.print("Value of X is 1");
}
else if (x == 2) {
System.out.print("Value of X is 2");
}
else if (x == 3) {
System.out.print("Value of X is 3");
}
else {
System.out.print("Value of X is 0");
}
}
}
java的条件语句其实与C语言基本相同,没有啥区别,只不过输出的语法稍微有点不一样。
判断一个数是否为"水仙花数",所谓"水仙花数"是指这样的一个数:首先是一个三位数,其次,其各位数字的立方和等于该数本身。例如:371是一个"水仙花数",371=3^3+7^3+1^3.
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int i=input.nextInt();
if(i==Math.pow(i/100,3)+Math.pow((i%100)/10, 3)+Math.pow((i%10),3))
System.out.println(1);
else
System.out.println(0);
}
}
Java switch语句
//Java switch
/*
switch (表达式) {
case 常量值1:语句1; break;
case 常量值2:语句2; break;
case 常量值3:语句3; break;
……
case 常量值n:语句n; break;
default:语句n + 1;
}
*/
/*
应用实现
public class Main {
public static void main(String args[]){
char grade = 'B';
switch(grade) {
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
default:
System.out.println("未知");
}
}
}
*/