【参考答案】java基础练习:变量、数据类型、输入、输出、运算符
练习1:判断输入的值是否是偶数,另外,要处理输入错误
(目的:熟悉输入、输出,特别是Scanner对象的方法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.qzcsbj; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System. in ); System. out .print( "请输入一个整数:" ); if (input.hasNextInt()) { int num = input.nextInt(); if (num % 2 == 0) { System. out .println(num + "是偶数" ); } else { System. out .println(num + "是奇数" ); } } else { System. out .println( "输入无效!" ); } } } |
练习2:输入并输出姓名、年龄、身高
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.qzcsbj; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print( "请输入您的姓名:" ); String name = input.next(); System.out.print( "请输入您的年龄:" ); int age = input.nextInt(); System.out.print( "请输入您的身高:" ); double height = input.nextDouble(); System.out.println( "姓名:" + name); System.out.println( "年龄:" + age); System.out.println( "身高:" + height); } } |
练习3:从控制台获取Java、Oracle、HTML三门课程的成绩,计算总分和平均分
平均分保留2位小数,要求四舍五入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.qzcsbj; import java.text.DecimalFormat; import java.util.Scanner; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print( "请输入Java成绩:" ); int java = input.nextInt(); System.out.print( "请输入Oracle成绩:" ); int oracle = input.nextInt(); System.out.print( "请输入HTML成绩:" ); int html = input.nextInt(); double sum = java + oracle + html; double avg = sum / 3 ; DecimalFormat df = new DecimalFormat( "#.00" ); System.out.println( "---------------------------------" ); System.out.println( "Java\tOracle\tHTML" ); System.out.println(java + "\t\t" + oracle + "\t\t" + html); System.out.println( "---------------------------------" ); System.out.println( "总分:" + sum); System.out.println( "平均分:" + df.format(avg)); } } |
练习4:输入一个数字,反转输出,如:输入123,输出321
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.qzcsbj; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc= new Scanner(System. in ); System. out .print( "请输入一个整数:" ); if (sc.hasNextInt()) { int num = sc.nextInt(); while (num != 0) { int res = num % 10; System. out .print(res); num = num / 10; } } else { System. out .println( "输入有误。" ); } } } |
练习5:输入tom和jack的年龄,比较年龄并输出相差多少岁,要求使用条件运算符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.qzcsbj; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System. in ); System. out .print( "请输入tom的年龄:" ); int tom = input.nextInt(); System. out .print( "请输入jack的年龄:" ); int jack = input.nextInt(); String result = (tom - jack > 0 ? "tom比jack大:" + (tom - jack) : "jack比tom大:" + (jack - tom)) + "岁" ; System. out .println(result); } } |
练习6:交换两个数值变量的值(至少两种方法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.qzcsbj; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Test { public static void main(String[] args) { int a = 1 ,b = 2 ; // 方法一:不通过中间变量 // a = a + b; // b = a - b; // a = a - b; // 方法二:通过中间变量 int temp=a; a=b; b=temp; System.out.println(a); System.out.println(b); } } |
练习7:下面结果分别是?并说明原因
1 2 3 | int a = 97 ; char b = a; System.out.println(b); |
检查异常,int是4个字节,char是2个字节,需要强制类型转换,可能会损失精度
1 2 | char c = 97 ; System.out.println(c); |
输出结果:a
97不是被当做int来处理
1 2 3 | int m = 3 ; int n = m++ + ++m; System.out.println( "m:" +m+ ", n:" +n); |
m:5, n:8
一目运算符++优先级高于算术运算符+
运算符相同,从左到右执行
所以,第一个m是3,然后++后是4,第二个m,++m后是5,所以n是3+5=8
但是:有同学提出了质疑,说应该是4+4
下面是文心大模型的结果
下面是chatgpt的结果
到底哪个是正确的呢?
练习8:下面结果分别是?说明原因
1 2 3 4 5 6 7 | public class Test { public static void main(String[] args) { short a=1; a = a+1; System. out .println(a); } } |
1 2 3 4 5 6 7 | public class Test { public static void main(String[] args) { short b=1; b+=1; System. out .println(b); } } |
第一个报错,第二个不会
第二个复合赋值运算符会自动地将运算结果转型为其左操作数的类型,而第一个简单赋值运算不会。
【java百题计划汇总】
详见:https://www.cnblogs.com/uncleyong/p/15828510.html
原文会持续更新,原文地址:https://www.cnblogs.com/uncleyong/p/17043874.html
__EOF__

本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
分类:
e1-1 - java基础
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2022-01-11 k8s节点简介、移除节点、新增节点;删除k8s