写出身高体重计算器的java源代码

写出身高体重计算器的java源代码,第1张

public static void test5() {

Scanner sc = new Scanner(System.in)

System.out.println("请输入身高(单位为cm):")

float height = sc.nextFloat()

System.out.println("请输入体重(单位为Kg):")

float weight = sc.nextFloat()

if (height > 0 && weight > 0) {

float bmi = weight / (float) Math.pow(height / 100, 2)

System.out.println("体质指数为:" + bmi)

if (bmi <= 18.5) {

System.out.print("体型偏瘦!")

} else if (bmi > 18.5 && bmi <= 24.5) {

System.out.print("体型正常!")

} else if (bmi > 24.5 && bmi <= 28) {

System.out.print("体型超重!")

} else if (bmi > 28 && bmi <= 32) {

System.out.print("体型肥胖!")

} else if (bmi > 32) {

System.out.print("非常肥胖!")

}

} else {

System.out.println("输入有误,请重新来过!")

}

}

5'3" 这个是什么单位

哦 知道了 5英尺3英寸

package com.test

import java.io.BufferedReader

import java.io.IOException

import java.io.InputStreamReader

public class CalcWeight {

public static void main(String[] args) throws IOException {

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in))

System.out.println("输入性别:男性输入M,女性输入W")

String sex = bf.readLine()

while(!sex.equalsIgnoreCase("M") &&!sex.equalsIgnoreCase("W")){

System.out.println("输入有误,请重新输入")

sex = bf.readLine()

}

System.out.println("输入身高(身高必须高于5英尺):例如5英尺3英寸 请输入5'3''")

String height = bf.readLine()

while(!judgeHeight(height)){

height = bf.readLine()

}

System.out.println("输入体重(单位是磅)")

String weight = bf.readLine()

while(!judgeNum(weight)){

System.out.println("请输入数字")

weight = bf.readLine()

}

compute(sex, height, weight)

}

public static boolean judgeHeight(String height){

boolean rs=false

if(height.trim().length()!=0){

if(height.indexOf("'")==-1 || height.lastIndexOf("''")==-1){

System.out.println("输入格式有误,请重新输入")

}else{

try {

int feet = Integer.valueOf(height.substring(0,height.indexOf("'")))

int inches = Integer.valueOf(height.substring(height.indexOf("'")+1,height.lastIndexOf("''")))

if(feet<5){

System.out.println("身高必须高于5英尺,请重新输入")

}else{

rs = true

}

} catch (NumberFormatException e) {

System.out.println("请输入数字")

}

}

}

return rs

}

public static boolean judgeNum(String num){

boolean rs=false

try {

Integer.valueOf(num)

rs = true

} catch (NumberFormatException e) {

}

return rs

}

public static void compute(String sex,String height,String weight){

int com_weight=0

int u_weight = Integer.valueOf(weight)

int feet = Integer.valueOf(height.substring(0,height.indexOf("'")))

int inches = Integer.valueOf(height.substring(height.indexOf("'")+1,height.lastIndexOf("''")))

if(sex.equalsIgnoreCase("w")){

com_weight = 100+ ((feet-5)*12 +inches)*5

}else{

com_weight = 106+ ((feet-5)*12 +inches)*6

}

System.out.println("身高:"+feet+"英尺"+inches+"英寸")

System.out.println("最佳体重:" + com_weight + "磅")

System.out.println("您的体重:" + u_weight + "磅" )

double rate=0.00

rate = (double)u_weight/(double)com_weight

if(rate>=0.85 &&rate<=1.15){

System.out.print("您的体重很正常")

}else{

if(rate<0.85){

System.out.println("您可能偏瘦")

}

if(rate>1.15){

System.out.println("您可能偏胖")

}

}

}

}

封装类 Student如下:

package help

public class Student{

private Double height

public Student(Double height) {

super()

this.height = height

}

public Double getHeight() {

return height

}

public void setHeight(Double height) {

this.height = height

}

}

测试类

package help

import java.util.Scanner

public class TestStudent {

public static void main(String[] args){

Scanner sc=new Scanner(System.in)

Student[] stus=new Student[10]

/**

* 将输入的是个学生的身高实例化是个学生,并保存到Student对象数组中

*/

for(int i=1i<=10i++){

System.out.println("输入学生第"+i+"的身高")

double h=sc.nextDouble()

Student s=new Student(h)

stus[i-1]=s

}

Student maxHeightStu=new Student(stus[0].getHeight())//假设第一个学生的身高为最高学生最高

for(int i=1i<stus.lengthi++){

if(maxHeightStu.getHeight()<stus[i].getHeight()){

maxHeightStu.setHeight(stus[i].getHeight())//当有学生的身高更高时,更新最高学生身高

}

}

System.out.println("最高学生的身高:"+maxHeightStu.getHeight())

//注:输入的学生身高仍在stus对象数组中保存。

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/11283874.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存