if语句和逻辑运算符的使用
package computerArea; import java.util.Scanner; public class ComputerArea { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("请输入一个数"); int num = sc.nextInt(); if(num % 5 == 0 && num % 6 == 0){ System.out.println(num+"能被5和6整除。"); } else if(num % 5 == 0 || num % 6 == 0){ System.out.println(num+"能被5或6整除"); } else if(num % 5 == 0 ^ num % 6 == 0){ System.out.println(num+"能只被5或者只被6整除"); } else System.out.println(num+"不能被5和6中任意一个数整除"); } }
判断闰年
package computerArea; import java.util.Scanner; public class ComputerArea { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("请输入年份:"); int year = sc.nextInt(); if((year % 4 == 0 && year % 100 == 0) || year % 400 == 0){ System.out.println(year+"年是闰年"); } else{ System.out.println(year+"年不是闰年"); } } }
if和String
package computerArea; import java.util.Scanner; public class ComputerArea { public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("请输入您的成绩"); double score = sc.nextDouble(); String grade = ""; if(score > 100 || score < 0){ System.out.println("您输入的成绩不在合法范围"); System.exit(0); }else if(score >= 90){ grade = "优秀"; }else if(score >= 80){ grade = "良好"; }else if(score >= 70){ grade = "中等"; }else if(score >= 60){ grade = "及格"; }else grade = "不及格"; System.out.println("你的成绩为"+grade); } }
时间: 2024-10-08 09:04:29