1:编写程序,根据输入的性别,身高,体重,计算体重指数BMI

BMI 是国际上常用的衡量人体胖瘦程度以及是否健康的一个标准,算法:kg/m2

标准:男性低于20, 女性低于19,属于过轻;男性20-25,女性19-24,属于适中;男性25-30,女性24-29,属于过重;男性30-35,女性29-34,属于肥胖;男性高于35,女性高于34,属于非常肥胖。专家指出最理想的体重指数是22。

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Scanner;

public class demol1 {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
System.out.println("输入你的性别:1为男,2为女");
System.out.print("请输入:");

byte chosse= sc.nextByte();
switch (chosse){
case 1:
System.out.println(man());
break;
case 2:
System.out.println(girl());
break;
}
}
//判断男的BMI
public static String man(){
System.out.println("请问你的体重为多少kg?");
System.out.print("请输入:");
double kg= sc.nextDouble();
System.out.println("请问你的身高为多少m?");
System.out.print("请输入:");
double m=sc.nextDouble();
if(kg/(m*m)<20){
return "过轻";
} else if (kg/(m*m)<=25) {
return "适中";
} else if (kg/(m*m)<=30) {
return "过重";
}else if(kg/(m*m)<=35){
return "肥胖";
}
return "非常肥胖";

}

//判断女的BMI
public static String girl() {
System.out.println("请问你的体重为多少kg?");
System.out.print("请输入:");
double kg= sc.nextDouble();
System.out.println("请问你的身高为多少m?");
System.out.print("请输入:");
double m=sc.nextDouble();
if(kg/(m*m)<19){
return "过轻";
} else if (kg/(m*m)<=24) {
return "适中";
} else if (kg/(m*m)<=29) {
return "过重";
}else if(kg/(m*m)<=34){
return "肥胖";
}
return "非常肥胖";

}
}

2:输出2到10000的所有素数,每行显示8个素数

(1)用循环语句在2到10000之间循环,判断该数是否为素数;

(2)对于素数i的判断:为提高运行效率,可考虑从2~i/2之间依次判断是否有能整除i的数,一旦有,就无需进行后续数的判断,直接跳出循环体,循环体的跳出可考虑用break,跳出前可用boolean型变量标识其为非素数;

(3)输出时,要求每行显示8个,用一个整型变量记录素数的个数,每8个换行输出。

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
public class demol2 {
public static void main(String[] args) {
System.out.println("找寻2到10000素数");
//定义计数器并初始化
byte count=0;
for (int i = 2; i < 10000; i++) {
if(Find(i)){
System.out.print(i+"\t");
count++;
if (count==8) {
System.out.println();
count=0;
}
}
}
}

//检查一个数是不是素数
public static boolean Find(int a) {
for (int i = 2; i < a; i++) {
if(a%i==0)return false;
}
return true;
}
}

3:设有英文短文如下,统计输出以字母 w 开头的单词数; 统计输出单词中含“or”字符串的单词数; 统计输出长度为 3 的单词数。

Last fall I walked with a friend in hometown. He was recognized as the most stupid among my childhood playmates, but he has now been Number One in an enterprise.When we came to a fork, a blind man walked from another direction. Testing the road with his bamboo pole, the blind man was walking very slowly towards the road we were going to.I chatted with my friend as we were walking. I talked about my five job-hoppings within two years and the experiences I was still struggling in the plight now. My friend nodded and smiled, but he kept silent all along. After a while, I occasionally looked back and found the blind man had vanished.

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
35
36
37
38
39
40
import java.util.Scanner;

public class WorldCount {

public static void main(String[] args) {
// 创建一个Scanner对象,用于接收用户输入的英文短文
Scanner sc = new Scanner(System.in);
System.out.println("请输入一篇英文短文:");
// 读取用户输入的英文短文,并转换为小写字母
String text = sc.nextLine().toLowerCase();
// 关闭Scanner对象
sc.close();
// 用空格分隔英文短文,得到单词数组
String[] words = text.split(" ");
// 定义三个变量,分别用于统计以字母w开头的单词数,含有or字符串的单词数,长度为3的单词数
int wCount = 0;
int orCount = 0;
int len3Count = 0;
// 遍历单词数组,对每个单词进行判断和计数
for (String word : words) {
// 如果单词以w开头,wCount加1
if (word.startsWith("w")) {
wCount++;
}
// 如果单词包含or字符串,orCount加1
if (word.contains("or")) {
orCount++;
}
// 如果单词长度为3,len3Count加1
if (word.length() == 3) {
len3Count++;
}
}
// 输出统计结果
System.out.println("以字母w开头的单词数为:" + wCount);
System.out.println("含有or字符串的单词数为:" + orCount);
System.out.println("长度为3的单词数为:" + len3Count);
}
}