目录
11:随机输入一个字母,如果是大写字母就转化为小写字母输出;如果是小写字母就转化为大写字母输出。
12:使用if结构实现学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
13:使用条件结构实现:岳灵珊同学参加到Java的学习,他父亲岳不群和母亲宁中则承诺:
14:使用条件结构实现,如果用户名等于字符‘青’,密码等于数字‘123’,就输出“欢迎你,青”,否则就输出“对不起,你不是青”。
如果:b2-4ac>0,则有两个解;b2-4ac=0,则有一个解;b2-4ac<0,则无解;
16:使用switch选择结构实现判断某年某月某日是这一年的第几天?
17:使用switch选择结构判断一个月份属于一年中的那个季节。(春夏秋冬)
-
-
- /*
- * 练习11:随机输入一个字母,如果是大写字母就转化为小写字母输出;如果是小写字母就转化为大写字母输出。
- * 提示:
- * a~z的ASCII码:97~122
- * A~Z的ASCII码:65~90
- */
- public static void main(String[] args) {
- System.out.println("请输入一个大写字母或者小写字母:");
- Scanner sc = new Scanner(System.in);
- String str = sc.next();
- //在键盘输入中没有char型数据的获取方法借用String型数据的获取,在使用charAt()方法获得
- //字符串的第一个字母
- int num = str.charAt(0);
-
- if (num >= 65 && num <= 90) {
- char upperCaseLetters = (char) (num + 32);
- System.out.println(upperCaseLetters);
- } else {
- char lowerCaseLetters = (char) (num - 32);
- System.out.println(lowerCaseLetters);
- }
- sc.close();
- }
-

-
-
- /*
- * 练习12:使用if结构实现学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
- */
- public static void main(String[] args) {
- // 创建键盘录入对象
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入你的成绩:");
- double score = sc.nextDouble();
-
- if (score >= 90) {
- System.out.println("A");
- } else if (score >= 60) {
- System.out.println("B");
- } else {
- System.out.println("C");
- }
- sc.close();
-
- }
-
-
-
- /*
- * 练习13:使用条件结构实现:岳灵珊同学参加到Java的学习,他父亲岳不群和母亲宁中则承诺:
- * 如果岳灵珊的考试成绩==1OO分,父亲给她买辆车
- * 如果岳灵珊的考试成绩>=90分,母亲给她买台笔记本电脑
- * 如果岳灵珊的考试成绩>=60分,母亲给她买部手机
- * 如果岳灵珊的考试成绩<60分,没有礼物
- */
- public static void main(String[] args) {
- // 创建键盘录入对象
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入你的成绩:");
- double score = sc.nextDouble();
-
- if (score == 100) {
- System.out.println("父亲给她买辆车");
- } else if (score >= 90) {
- System.out.println("母亲给她买台笔记本电脑");
- } else if(score>=60) {
- System.out.println("母亲给她买部手机");
- }else{
- System.out.println("没有礼物");
- }
- sc.close();
-
- }
-
-
-
- /*
- * 练习14:使用条件结构实现,如果用户名等于字符‘青’,密码等于数字‘123’,就输出“欢迎你,青”,否则就输出“对不起,你不是青”。
- */
- public static void main(String[] args) {
- // 创建键盘录入对象
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入用户名:");
- String name = sc.next();
- System.out.println("请输入密码:");
- int passwd = sc.nextInt();
- if (name.equals("青") && passwd == 123) {
- System.out.println("欢迎你,青");
- } else {
- System.out.println("对不起,你不是青");
- }
- sc.close();
- }
-
-
-
- /*
- * 练习15:求ax^2+bx+c=0方程的根。 a,b,c分别为函数的参数
- * 如果:b2-4ac>0,则有两个解;b2-4ac=0,则有一个解;b2-4ac<0,则无解;
- * 已知:
- * x1=(-b+sqrt(b2-4ac))/2a
- * x2=(-b-sqrt(b2-4ac))/2a
- *
- */
- public static void main(String[] args) {
- // 创建键盘录入对象
- System.out.println("求 ax^2 + bx + c = 0 的根");
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入a值:");
- double a = sc.nextDouble();
- System.out.println("请输入b值:");
- double b = sc.nextDouble();
- System.out.println("请输入c值:");
- double c = sc.nextDouble();
-
- if (b * b - 4 * a * c >= 0) {
- double x1 = ((-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a));
- double x2 = ((-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a));
- System.out.println("x1 = " + x1);
- System.out.println("x2 = " + x2);
- } else {
- System.out.println("此方程无实根");
- }
- sc.close();
- }
-
-
-
- /*
- * 练习16:使用switch选择结构实现判断某年某月某日是这一年的第几天?(不考虑闰年,一年按365天算)
- */
- public static void main(String[] args) {
- // 创建键盘录入对象
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入月份:");
- int month = sc.nextInt();
- System.out.println("请输入日期:");
- int day = sc.nextInt();
-
- switch (month) {
- case 1:
- System.out.println("你输入的日期是今年的第:" + day + "天");
- break;
- case 2:
- System.out.println("你输入的日期是今年的第:" + (31 + day) + "天");
- break;
- case 3:
- System.out.println("你输入的日期是今年的第:" + (31 + 28 + day) + "天");
- break;
- case 4:
- System.out.println("你输入的日期是今年的第:" + (31 + 28 + 31 + day) + "天");
- break;
- case 5:
- System.out.println("你输入的日期是今年的第:" + (31 + 28 + 31 + 30 + day) + "天");
- break;
- case 6:
- System.out.println("你输入的日期是今年的第:" + (31 + 28 + 31 + 30 + 31 + day) + "天");
- break;
- case 7:
- System.out.println("你输入的日期是今年的第:" + (31 + 28 + 31 + 30 + 31 + 30 + day) + "天");
- break;
- case 8:
- System.out.println("你输入的日期是今年的第:" + (31 + 28 + 31 + 30 + 31 + 30 + 31 + day) + "天");
- break;
- case 9:
- System.out.println("你输入的日期是今年的第:" + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day) + "天");
- break;
- case 10:
- System.out.println("你输入的日期是今年的第:" + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day) + "天");
- break;
- case 11:
- System.out.println("你输入的日期是今年的第:" + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day) + "天");
- break;
- case 12:
- System.out.println("你输入的日期是今年的第:" + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day) + "天");
- break;
-
- default:
- System.out.println("输入数据有误");
- break;
- }
- sc.close();
- }
-
-
-
- /*
- * 练习17:使用switch选择结构判断一个月份属于一年中的那个季节。(春夏秋冬)
- */
- public static void main(String[] args) {
- // 创建键盘录入对象
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入一个月份:");
- int month = sc.nextInt();
-
- switch (month) {
- case 12:
- case 1:
- case 2:
- System.out.println("冬");
- break;
- case 3:
- case 4:
- case 5:
- System.out.println("春");
- break;
- case 6:
- case 7:
- case 8:
- System.out.println("夏");
- break;
- case 9:
- case 10:
- case 11:
- System.out.println("秋");
- break;
- default:
- System.out.println("输入月份有误");
- break;
- }
- sc.close();
- }
-