当前位置:网站首页>习题:选择结构(一)
习题:选择结构(一)
2022-08-05 00:53:00 【抬眼远望】
01: 使用if选择结构判断一个整数,属于哪个范围:大于0;小于0;等于0
/*
* 练习01:使用if选择结构判断一个整数,属于哪个范围:大于0;小于0;等于0
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = sc.nextInt();
if (num > 0) {
System.out.println("大于0");
} else if (num == 0) {
System.out.println("等于0");
} else {
System.out.println("小于0");
}
sc.close();
}

02:使用if选择结构判断一个整数是偶数还是奇数。
/*
* 练习02:使用if选择结构判断一个整数是偶数还是奇数。
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = sc.nextInt();
//能整除2的数是偶数
if (num % 2 == 0) {
System.out.println("偶数");
} else {
System.out.println("奇数");
}
sc.close();
}
03:使用if选择结构对三个整数进行排序,输出时按照从小到大的顺序输出。
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int num1 = sc.nextInt();
System.out.println("请输入第二个整数:");
int num2 = sc.nextInt();
System.out.println("请输入第三个整数:");
int num3 = sc.nextInt();
// 比较num1和num2,得出较小数,存放在num1变量中,num2中存放较大数
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
// 比较num1和num3,得出较小数,存放在num1变量中,num3中存放较大数
if (num1 > num3) {
int temp = num1;
num1 = num3;
num3 = temp;
}
// 比较num2和num3,得出较小数,存放在num2变量中,num3中存放较大数
if (num2 > num3) {
int temp = num2;
num2 = num3;
num3 = temp;
}
// 输出
System.out.println("三个整数从小到大排列:" + num1 + " " + num2 + " " + num3);
sc.close();
04:使用if选择结构判断一个三位的整数是否是水仙花数。
水仙花数的条件:三位数为abc,则满足:a3+b3+c3=abc
/*
* 练习04:使用if选择结构判断一个三位的整数是否是水仙花数。 水仙花数的条件:三位数为abc,则满足:a^3+b^3+c^3=abc
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个3位整数:");
int num = sc.nextInt();
int geWei = num % 10;
int shiWei = num / 10 % 10;
int baiWei = num / 100;
if (geWei * geWei * geWei + shiWei * shiWei * shiWei + baiWei * baiWei * baiWei == num) {
System.out.println("是水仙花数");
} else {
System.out.println("不是水仙花数");
}
sc.close();
}
05:使用if选择结构判断某一年份是否是闰年。
闰年的条件:
普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年);
世纪闰年:能被400整除的为世纪闰年。(如2000年是世纪闰年);
/*
* 练习05:使用if选择结构判断某一年份是否是闰年。
* 闰年的条件:
* 普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年);
* 世纪闰年:能被400整除的为世纪闰年。(如2000年是世纪闰年);
*
*/
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("闰年");
}else{
System.out.println("平年");
}
sc.close();
}
06:使用if选择结构判断一个4位整数,统计出此整数各个位包含多少个偶数,多少个奇数
/*
* 练习06:使用if选择结构判断一个4位整数,统计出此整数各个位包含多少个偶数,多少个奇数
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个4位整数:");
int num = sc.nextInt();
int geWei = num % 10;
int shiWei = num / 10 % 10;
int baiWei = num / 100 % 10;
int qianWei = num / 1000;
int evenNumber = 0;// 初始化偶数数量为0
int oddNumber = 0;// 初始化奇数数量为0
if (geWei % 2 == 0) {
evenNumber++;
} else {
oddNumber++;
}
if (shiWei % 2 == 0) {
evenNumber++;
} else {
oddNumber++;
}
if (baiWei % 2 == 0) {
evenNumber++;
} else {
oddNumber++;
}
if (qianWei % 2 == 0) {
evenNumber++;
} else {
oddNumber++;
}
System.out.println("偶数个数:" + evenNumber + "\n奇数个数:" + oddNumber);
sc.close();
}
07:开发一个程序,根据公式(身高-108)*2=体重,可以有10斤左右的浮动。来观察测试者体重是否合适。
/*
* 练习07:开发一个程序,根据公式(身高-108)*2=体重,可以有10斤左右的浮动。来观察测试者体重是否合适。
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的身高(cm):");
double height = sc.nextDouble();
System.out.println("请输入你的体重(斤):");
double weight = sc.nextDouble();
double standardWeight = (height - 108) * 2;
//输出标准体重
System.out.println("标准体重:" + standardWeight);
if ((weight >= standardWeight - 10) && (weight <= standardWeight + 10)) {
System.out.println("体重合适");
} else if (weight < standardWeight - 10) {
System.out.println("体重偏轻");
} else {
System.out.println("体重偏重");
}
sc.close();
}
08: 判断此考试成绩为什么等级。
90-100之间为优秀
80-89之间为优良
70-79之间为良好,
60-69之间为及格
60分以下为不及格。
/*
* 练习08:判断此考试成绩为什么等级。
* 90-100之间为优秀
* 80-89之间为优良
* 70-79之间为良好
* 60-69之间为及格
* 60分以下为不及格
* 使用if-else-if多分支选择结构
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入成绩:");
double score = sc.nextDouble();
if (score >= 90 && score <= 100) {
System.out.println("优秀");
} else if (score >= 80) {
System.out.println("优良");
} else if (score >= 70) {
System.out.println("良好");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
sc.close();
}
09: 写一个程序,输出类似09:03:12的时间格式,需要判断输入的数字是否符合实际情况,比如小时数就不能大于24,如果大于24就提示输入错误;分钟数和秒数不能大于60,如果大于60就提示输入错误。
/*
* 练习09:写一个程序,输出类似09:03:12的时间格式。
*
* 需要判断输入的数字是否符合实际情况,比如小时数就不能大于24,如果大于24就提示输入错误;
* 分钟数和秒数不能大于60,如果大于60就提示输入错误。
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入小时:");
int hour = sc.nextInt();
//输入小时数,当小于0或大于24时,不符规范,输出输入错误
if (hour > 24 || hour < 0) {
System.out.println("输入错误");
}
System.out.println("请输入分钟:");
int minute = sc.nextInt();
if (minute < 0 || minute > 60) {
System.out.println("输入错误");
}
System.out.println("请输入秒:");
int second = sc.nextInt();
if (second < 0 || second > 60) {
System.out.println("输入错误");
}
//输出格式 xx-xx-xx
//当小时数,分钟数,秒数小于10时,前面数字是0,故需要分情况输出
String newHour;
String newMinute;
String newSecond;
if (hour >= 0 && hour <= 9) {
newHour = "0" + hour;
} else {
newHour = "" + hour;
}
if (minute >= 0 && minute <= 9) {
newMinute = "0" + minute;
} else {
newMinute = "" + minute;
}
if (second >= 0 && second <= 9) {
newSecond = "0" + second;
} else {
newSecond = "" + second;
}
System.out.println("时间:" + newHour + ":" + newMinute + ":" + newSecond);
sc.close();
}
10: 有3个整数,给出提示信息:
能否创建三角形;
如果能构建三角形,提示是直角三角形还是等边三角形等腰三角形还是普通三角形;
最后输出三角形面积;
/*
* 练习10:有3个整数,给出提示信息: 能否创建三角形; 如果能构建三角形,提示是直角三角形还是等边三角形等腰三角形还是普通三角形;最后输出三角形面积;
*
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int a = sc.nextInt();
System.out.println("请输入第二个整数:");
int b = sc.nextInt();
System.out.println("请输入第三个整数:");
int c = sc.nextInt();
//外层if判断是否是三角形
if (a + b > c && a + c > b && b + c > a) {
System.out.println("你输入的三个数能构建三角形");
//内层判断时什么三角形
if (a == b && a == c) {
System.out.println("等边三角形");
}
if (a == b && a != c || a == c && a != b || b == c && b != a) {
System.out.println("等腰三角形");
}
if ((a * a + b * b == c * c) || (a * a + c * c == b * b) || (c * c + b * b == a * a)) {
System.out.println("直角三角形");
}
if ((a != b && a != c && b != c)
&& ((a * a + b * b != (c * c)) && (a * a + c * c != (b * b)) && (c * c + b * b != (a * a)))) {
System.out.println("普通三角形");
}
double zhouChang = a + b + c;
double p = zhouChang / 2;
System.out.println("p:" + p);
double area = Math.sqrt(p * (p - a) * (p - b) * (p - c));
System.out.println("三角形面积:" + area);
} else {
System.out.println("你输入的三个整数不能构建三角形");
}
sc.close();
}
边栏推荐
- Bit rate vs. resolution, which one is more important?
- Software Testing Interview Questions: What is Software Testing?The purpose and principle of software testing?
- 【FreeRTOS】FreeRTOS与stm32内置堆栈的占用情况
- 【TA-霜狼_may-《百人计划》】图形4.3 实时阴影介绍
- Pytorch usage and tricks
- GCC: compile-time library path and runtime library path
- Redis visual management software Redis Desktop Manager2022
- 主库预警日志报错ORA-00270
- Lattice PCIe Learning 1
- 软件基础的理论
猜你喜欢
随机推荐
软件测试面试题:软件都有多少种分类?
LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
Software Testing Interview Questions: About Automated Testing Tools?
Gartner Hype Cycle:超融合技术将在2年内到达“生产力成熟期”
2022牛客多校第三场 A Ancestor
【FreeRTOS】FreeRTOS与stm32内置堆栈的占用情况
第十一章 开关级建模
深度学习原理学习小结 - Self-Attention/Transformer
Software Testing Interview Questions: What do you think about software process improvement? Is there something that needs improvement in the enterprise you have worked for? What do you expect the idea
The method of freely controlling concurrency in the sync package in GO
tiup status
手把手基于YOLOv5定制实现FacePose之《YOLO结构解读、YOLO数据格式转换、YOLO过程修改》
JUC thread pool (1): FutureTask use
SV class virtual method of polymorphism
After the staged testing is complete, have you performed defect analysis?
linux(centOs7)部署mysql(8.0.20)数据库
Jin Jiu Yin Shi Interview and Job-hopping Season; Are You Ready?
码率vs.分辨率,哪一个更重要?
软件测试面试题:测试生命周期,测试过程分为几个阶段,以及各阶段的含义及使用的方法?
自定义线程池








](/img/4d/2d81dc75433c23c5ba6b31453396f0.png)
