当前位置:网站首页>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();
}
边栏推荐
- DHCP的工作过程
- torch.autograd.grad finds the second derivative
- Software testing interview questions: the difference and connection between black box testing, white box testing, and unit testing, integration testing, system testing, and acceptance testing?
- MongoDB construction and basic operations
- EL定时刷新页面中的皕杰报表实例
- Interview summary: Why do interviewers in large factories always ask about the underlying principles of Framework?
- PCIe Core Configuration
- Matlab uses plotting method for data simulation and simulation
- GCC: compile-time library path and runtime library path
- 【FreeRTOS】FreeRTOS与stm32内置堆栈的占用情况
猜你喜欢
随机推荐
2022 Hangzhou Electric Multi-School 1004 Ball
主库预警日志报错ORA-00270
4. PCIe 接口时序
torch.autograd.grad求二阶导数
PCIe Core Configuration
2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
阶段性测试完成后,你进行缺陷分析了么?
tiup status
2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 2)
软件测试面试题:测试生命周期,测试过程分为几个阶段,以及各阶段的含义及使用的方法?
内存取证系列1
Software testing interview questions: What are the seven-layer network protocols?
ORA-01105 ORA-03175
BC(转)[js]js计算两个时间相差天数
"No title"
配置类总结
node uses redis
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
仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!








