当前位置:网站首页>Exercise: Selecting a Structure (1)
Exercise: Selecting a Structure (1)
2022-08-05 00:58:00 【look up】
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();
//能整除2The number is even
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,Get a smaller number,存放在num1变量中,num2Store larger numbers in
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
// 比较num1和num3,Get a smaller number,存放在num1变量中,num3Store larger numbers in
if (num1 > num3) {
int temp = num1;
num1 = num3;
num3 = temp;
}
// 比较num2和num3,Get a smaller number,存放在num2变量中,num3Store larger numbers in
if (num2 > num3) {
int temp = num2;
num2 = num3;
num3 = temp;
}
// 输出
System.out.println("Arrange the three integers from smallest to largest:" + 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();
//Both ordinary leap years and century leap years are leap years,满足其中一个条件即可,即用||运算符连接
if(year%4==0&&year%100!=0||year%400==0){
System.out.println("闰年");
}else{
System.out.println("平年");
}
sc.close();
}
06:使用if选择结构判断一个4位整数,Count how many even numbers each bit of this integer contains,多少个奇数
/*
* 练习06:使用if选择结构判断一个4位整数,Count how many even numbers each bit of this integer contains,多少个奇数
*/
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;// Initialize an even number of 0
int oddNumber = 0;// Initialize odd numbers as 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("Appropriate weight");
} else if (weight < standardWeight - 10) {
System.out.println("Underweight");
} 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
//hours,分钟数,秒数小于10时,前面数字是0,Therefore, it needs to be output according to the situation
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("The three numbers you enter build a triangle");
//What is the triangle when the inner layer is judged
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("The three integers you entered cannot build a triangle");
}
sc.close();
}
边栏推荐
- 2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
- matlab 采用描点法进行数据模拟和仿真
- 多线程涉及的其它知识(死锁(等待唤醒机制),内存可见性问题以及定时器)
- [230] Execute command error after connecting to Redis MISCONF Redis is configured to save RDB snapshots
- OPENWIFI实践1:下载并编译SDRPi的HDL源码
- 2021年11月网络规划设计师上午题知识点(上)
- CNI (Container Network Plugin)
- E - Distance Sequence (prefix and optimized dp
- Getting Started with Kubernetes Networking
- 2022杭电多校第三场 K题 Taxi
猜你喜欢
随机推荐
二叉树[全解](C语言)
Software testing interview questions: Have you used some tools for software defect (Bug) management in your past software testing work? If so, please describe the process of software defect (Bug) trac
How DHCP works
深度学习训练前快速批量修改数据集中的图片名
Software Testing Interview Questions: What is Software Testing?The purpose and principle of software testing?
Matlab uses plotting method for data simulation and simulation
Lattice PCIe Learning 1
活动推荐 | 快手StreamLake品牌发布会,8月10日一起见证!
Gartner Hype Cycle:超融合技术将在2年内到达“生产力成熟期”
Software Testing Interview Questions: What aspects should be considered when designing test cases, i.e. what aspects should different test cases test against?
安装oracle11的时候为什么会报这个问题
测试工作这么难找吗?今年32,失业2个月,大龄测试工程师接下来该拿什么养家?
Opencv——视频跳帧处理
Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 1)
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
Redis visual management software Redis Desktop Manager2022
MongoDB construction and basic operations
蓝牙Mesh系统开发五 ble mesh设备增加与移除
Zombie and orphan processes
torch.autograd.grad求二阶导数