标签:case lse 选择结构 int new 遇到 ase 现象 string
class IfStruct1{ public static void main(String[] args) { if(布尔表达式){ //语句体 } } }
class IfStruct2{ public static void main(String[] args) { if(布尔表达式){ //语句体1 }else{ //语句体2 } } }
class IfStruct3{ public static void main(String[] args) { if(布尔表达式1){ //语句体1 }else if(布尔表打死2){ //语句体2 }else{ //语句体3 } } }
package com.xuweiwei; public class IfDemo { public static void main(String[] args) { int x = 0; if(x ==0){ System.out.println("你好啊"); } System.out.println("世界"); } }
package com.xuweiwei; public class IfDemo { public static void main(String[] args) { int x = 2; if(x % 2 == 0){ System.out.println("是偶数"); }else{ System.out.println("是奇数"); } } }
package com.xuweiwei; public class IfDemo { public static void main(String[] args) { char c = ‘男‘; if (c == ‘男‘){ System.out.println("你是男生"); }else if(c ==‘女‘){ System.out.println("你是女生"); }else{ System.out.println("你的性别暂时不能识别"); } } }
package com.xuweiwei; public class IfTest { public static void main(String[] args) { int a = 10; int b = 20; int c = 30; if(a > b){ if(a > c){ System.out.println("a="+a+"最大"); }else{ System.out.println("c="+c+"最大"); } }else{ if(b > c){ System.out.println("b="+b+"最大"); }else{ System.out.println("c="+c+"最大"); } } } }
switch(表达式){//表达式可以是byte short int char 枚举,JDK7新增了String case 值1: 语句体1; break; case 值2: 语句体2; break; …… default: 语句体n; break; }
package com.xuweiwei; public class IfTest { public static void main(String[] args) { String str = "a"; switch (str) { case "a": System.out.println("a"); break; case "b": System.out.println("b"); break; default: System.out.println("你好"); break; } } }
package com.xuweiwei; import java.util.Scanner; public class IfTest { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入1-7:"); int num = input.nextInt(); if (num < 0 || num > 7) { System.out.println("抱歉,输入数字不合法"); } else { switch (num) { case 1: System.out.println("星期一"); break; case 2: System.out.println("星期二"); break; case 3: System.out.println("星期三"); break; case 4: System.out.println("星期四"); break; case 5: System.out.println("星期五"); break; case 6: System.out.println("星期六"); break; default: System.out.println("星期天"); break; } } } }
标签:case lse 选择结构 int new 遇到 ase 现象 string
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/9092523.html