Excise_day03
一、选择题
1.以下代码的运行结果为:
public class Calc {
public static void main (String args []) {
int total = 0;
for (int i = 0, j = 10; total > 30; ++i, --j) {
System.out.println(" i = " + i + " : j = " + j);
total += (i + j);
}
System.out.println("Total " + total);
}
}
A.产生运行错误
B.产生编译错误
C.输出 "Total 0"
D.产生如下输出 : i = 0 : j = 10 i = 1 : j = 9 i = 2 : j = 8 Total 30
2.下列代码输出结果是:
public class Foo {
public static void main(String[] args) {
int x = 5;
boolean b1 = true;
boolean b2 = false;
if((x==4)&&!b2)
System.out.println(“1”);
System.out.println(“2”);
if((b2=true)&&b1)
System.out.println(“3”);
}
}
A.2
B.3
C.1 2 3
D.2 3
3.下列程序编译或运行的结果是:
public static void main(String[] args) {
int num = 100;
for(int i=0;i<=100;i++) {
int num = 10;
}
System.out.println(num);
}
A. 10 B. 100 C. 201 D.编译出错
二、编程题:
1.计算体质指数BMI
根据用户输入的身高和体重计算BMI指数,并输出对应的文字描述
< 18.5 过轻
18.5-23.9 正常
24-27.9 超重
28 肥胖
注:BMI=体重(kg)/身高(m)的平方
package com.huacit.day03;import java.text.DecimalFormat;
import java.util.Scanner;import static java.lang.Double.parseDouble;/*** @author yyc*/
public class Excise_day03 {/** 1.计算体质指数BMI根据用户输入的身高和体重计算BMI指数,并输出对应的文字描述< 18.5 过轻18.5-23.9 正常24-27.9 超重* */public static void main(String[] args) {System.out.println("请输入身高(m):");Scanner input = new Scanner(System.in);double height = input.nextDouble();System.out.println("请输入体重(kg):");double weight = input.nextDouble();//四舍五入。保留一位小数/* DecimalFormat df = new DecimalFormat(" #0.0");double BMI = parseDouble(df.format(weight / (height * height)));*/double BMI = weight / (height * height);BMI = BMI * 100;if (BMI % 10 >= 5){BMI += 10;}BMI = (int)(BMI /10) / 10.0;System.out.println(BMI);if (weight < 0 || height < 0 ) {System.out.println("输入非法参数");} else {if (BMI < 18.5) {System.out.println("过轻");} else if (BMI < 23.9) {System.out.println("正常");} else if (BMI < 27.9) {System.out.println("过轻");}}}
}
2.编写程序求1到10的和并输出该结果?
package com.huacit.day03;/*** @author yyc*/
public class Excise2_day03 {public static void main(String[] args) {//2.编写程序求1到10的和并输出该结果?int sum = 0;for (int i = 0; i <= 10; i++){sum += i;}System.out.println("sum = "+ sum);}
}
3.求[1,100]中奇数的个数
int count = 0;for (int i = 1; i < 100; i++){if (i % 2 != 0){count++;}}System.out.println("100以内的奇数共 "+ count + "个");}
4.求1到500所有自然数中不含数字4的自然数共有多少个
//4.求1到500所有自然数中不含数字4的自然数共有多少个count = 0;for (int i = 1; i <= 500; i++){//个位 i % 10, 十位 i/10%10 、或 i % 100 / 10 ,百位 i / 100if (i == 4 || i%10 == 4 || i/10%10 == 4 || i/100 == 4){continue;}count++;}System.out.println(count);
5.请输入打印一个倒99乘法口诀
倒着输出99乘法表
//5.请输入打印一个倒99乘法口诀for (int i = 9; i >=1; i--){for (int j = 1; j <= i; j++){System.out.print(j + " * " + i+" = " +(i*j)+"\t" );}System.out.println();}
//打印九九乘法表for (int i = 1; i <= 9; i++){for (int j = 1; j <= i; j++){System.out.print(j+" * "+i+" = "+(i*j)+"\t");}System.out.println();}
6.输出1-100中所有的素数
*素数也称质数。一个大于1的正整数,如果除了1和它本身以外,不能被其他正整数整除,就叫素数。如2,3,5,7,11,13,17…
/** 6.输出1-100中所有的素数*素数也称质数。一个大于1的正整数,如果除了1和它本身以外,不能被其他正整数整除,就叫素数。如2,3,5,7,11,13,17…* */int i = 0;int j = 0;for (i = 2; i < 100; i++){for (j = 2; j < i; j++){if (i % j == 0){break;}}if (j >= i){System.out.print(i+" ");}}
7.输出所有的水仙花数。
*水仙花数是指:一个三位数,其各个位数字立方和等于该数本身。
//.输出所有的水仙花数。//*水仙花数是指:一个三位数,其个位数字立方和等于该数本身。System.out.println("三位数以内的水仙花数有:");for (int i = 100; i < 1000;i++){int b = i / 100;int s = i % 100 / 10;int g = i % 10;if (((b*b*b) + (s*s*s) + (g*g*g)) == i){System.out.println(i);}}
三、简答题
1.switch表达式都可以使用什么数据类型?
byte short char int enum(枚举类型jdk5.0新增),String(jdk7.0新增)
2.怎么结束多层循环?
使用 ①标记break;和 标记continue ;循环后的代码仍然执行
②return ;return 是结束整个程序,return语句后面直接跟分号 ,不能跟其他语句。使用return语句结束循环,循环后的所有代码不再执行
结束循环可以使用:
continue:结束本次循环,继续下一次循环。
break :结束当前循环,如果是多层循环,只是跳出本层循环。
return; 结束整个程序
最新文章
- Windows7 Scrapy框架建立
- PAT甲级1073 Scientific Notation:[C++题解]字符串处理、科学计数法
- hive中操作struct与map三例
- 【cGAN】conditional生成对抗网络--有代码
- 在React中测试和调试
- 在Windows Server 2008 R2(64位)上安装SharePoint 2007出现的一个错误的解决方法
- 将redis加入到Windows服务开机启动
- 惠普打印机墨盒更换教程_惠普打印机安装步骤 打印机墨盒加墨方法
- python函数(一)——def语句,作用域和参数
- Solidworks零件图存放位置更改后装配图识别不出来的解决办法
- Ubuntu常用软件下载以及视频流裁剪转码获取教程(可直接下载油管、B站、优酷等视频资源!)
- kali使用笔记本自带无线网卡_为什么你的无线网卡不好用?
- jks bks 等的定义 如何将jks转化为bks的
- DC/DC电源输入输出滤波电容摆放位置
- 学会通过商品id来获得商品详情
- JVM重要知识点整理和学习
- 十月百度,阿里巴巴,迅雷搜狗最新面试七十题(第201-270题)
- 一个15分钟的视频,教你用Python创建自己的微信聊天机器人文中赠送全部源代码
- 再谈韦伯/费希纳定律以及我对数学公式的理解
- 微信支付请求中文乱码解决方案!!!!!!