计算标准体重

转载请注明出处计算标准体重_c语言中计算标准体重_Mr_Leixiansheng的博客-CSDN博客

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="标准体重计算"
        android:textSize="20dp"
        android:layout_margin="20dp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:text="性别:"
            android:textSize="15dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioGroup
            android:id="@+id/group"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/man"
                android:text="男"
                android:layout_marginRight="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <RadioButton
                android:id="@+id/woman"
                android:text="女"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </RadioGroup>

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:text="身高:"
            android:textSize="15dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/input"
            android:inputType="number"
            android:hint="请输入您的身高   cm"
            android:textSize="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <Button
        android:id="@+id/reslut"
        android:text="计算"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:textSize="30dp"
        android:layout_margin="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

        <TextView
        android:id="@+id/weight"
        android:textSize="30dp"
        android:layout_margin="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <Button
        android:id="@+id/back"
        android:text="返回"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.leixiansheng.standardweight">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Reslut"/>
    </application>

</manifest>

package com.example.leixiansheng.standardweight;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    private RadioGroup group;
    private EditText input;
    private Button result;

    String sex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        group = (RadioGroup) findViewById(R.id.group);
        input = (EditText) findViewById(R.id.input);
        result = (Button) findViewById(R.id.reslut);

        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.man:
                        sex = "M";
                        break;
                    case R.id.woman:
                        sex = "W";
                        break;
                }
            }
        });

        result.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                double height = Double.parseDouble(input.getText().toString());
                Bundle bundle = new Bundle();
                bundle.putString("SEX", sex);
                bundle.putDouble("HEIGHT", height);
                Intent intent = new Intent(MainActivity.this, Reslut.class);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        });

    }
}

package com.example.leixiansheng.standardweight;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Created by Leixiansheng on 2017/3/10.
 */

public class Reslut extends AppCompatActivity {
    private TextView textView;
    private TextView weightText;
    private Button back;

    private String sex;
    private String sexText;
    private double height;
    private String weight;
    private String result;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reslut);

        textView = (TextView) findViewById(R.id.text);
        weightText = (TextView) findViewById(R.id.weight);
        back = (Button) findViewById(R.id.back);

        Bundle bundle = this.getIntent().getExtras();
        sex = bundle.getString("SEX");
        height = bundle.getDouble("HEIGHT");
        Log.d("MMM", sex + "   " + height);

        if (sex.equals("M")) {
            sexText = "男";
        } else {
            sexText = "女";
        }
        result = getWeight(sex, height);
        show(sexText, height);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Reslut.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }


    public String getWeight(String sex, double height) {
        if (sex.equals("M")) {
            weight = format((height - 80) * 0.7);
        } else {
            weight=format((height-70)*0.6);
        }
        return weight;
    }

    //四舍五入的方法
    private String format(double num)
    {
        NumberFormat formatter=new DecimalFormat("0.00");
        String s=formatter.format(num);
        return s;
    }

    private void show(String sexText, double height) {
        textView.setText("您是一位" + sexText + "性" + "  " + " 您的身高是:" + height + "cm");
        weightText.setText("您的标准体重是:" + result + "kg");
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里提供一个按身高计算标准体重的类 StdWeight 的实现: ```java import java.util.Scanner; public class StdWeight { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入身高(cm):"); double height = sc.nextDouble(); System.out.print("请输入性别(男/女):"); String gender = sc.next(); double standardWeight; if (gender.equals("男")) { standardWeight = forMale(height); } else if (gender.equals("女")) { standardWeight = forFemale(height); } else { System.out.println("性别输入错误!"); return; } System.out.printf("您的标准体重是 %.2f kg\n", standardWeight); double lowerBound = standardWeight * 0.9; double upperBound = standardWeight * 1.1; System.out.printf("正常体重范围:%.2f kg ~ %.2f kg\n", lowerBound, upperBound); } public static double forMale(double h) { return (h - 100) * 0.9; } public static double forFemale(double h) { return (h - 100) * 0.9 - 2.5; } } ``` 在这个程序中,StdWeight 类定义了两个静态方法 forMale() 和 forFemale(),分别用于计算男性和女性的标准体重。这两个方法都接收一个参数 h,表示身高(单位为 cm),并返回一个 double 类型的标准体重值。 在 main 方法中,我们首先使用 Scanner 类获取用户输入的身高和性别。然后根据性别调用相应的方法计算标准体重。最后,我们将计算结果输出到控制台,并计算标准体重的正负 10% 范围,输出到控制台。在输出时,我们使用了 printf() 方法,可以控制输出的小数位数。 希望这个程序对您有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值