JAVA选择结构练习答案

T1.

public static void main(String []args){
	  System.out.println("请输入出行的路程:");
	        Scanner scan = new Scanner(System.in);
	        float distance = scan.nextFloat();
	        float price;
	        if(distance>0&&distance<=2){
	            price= 7f;
	        }
	        else if(distance>2){
	            price = 7F+(distance-2)*3;
	        }
	        else {
	            price = 0;
	        }
	        System.out.println("你的费用为"+price);
	}

T2.

public static void main(String []args){
		Scanner scan = new Scanner(System.in);
        System.out.println("请输入性别:");
        String sex = scan.nextLine();
        System.out.println("请输入体重(kg):");
        int result=0;
        float weight = scan.nextFloat();
        if("男".equals(sex)){
            if(weight<=60)
                result=250;
            else {
            	  result = 300;
            }
        }
        else  if("女".equals(sex)){
            if(weight<=50){
                result = 200;
            }
            else{
                result = 250;
            }
        }
        else {
            System.out.println("请输入正确的性别!(男/女)");
        }
        System.out.println("你的输血量为:"+result+"ml");
	}

T3.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入年龄:");
    int age = scan.nextInt();
    System.out.println("请输入票型:");
    String ticketType =scan.next();
    float price = 0;
    if(age>=18){
        if("硬座".equals(ticketType)){
            price=100;
        }
        if("硬卧".equals(ticketType)){
            price=190;
        }
    }
    else{
        System.out.println("请输入身高:");
        float height = scan.nextFloat();
        if("硬座".equals(ticketType)&&height<120){
            price=0;
        }
        if("硬座".equals(ticketType)&&height<150&&height>=120){
            price=100/2;
        }
        if("硬座".equals(ticketType)&&height>=150){
            price=100;
        }
        if("硬卧".equals(ticketType)){
            price=190-50;
        }
    }
    System.out.println("你需要支付的价格为:"+price);

T4.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入体重:(kg)");
    float weight = scan.nextFloat();
    System.out.println("请输入身高:(米)");
    float height = scan.nextFloat();
    float BMI = weight/height;
    if(BMI<18.5){
        System.out.println("体重过低!");
    }
    else if(BMI<25&&BMI>=18.5){
        System.out.println("正常!");
    }
    else if(BMI>=25&&BMI<30){
        System.out.println("肥胖前期!");
    }
    else if(BMI>=30&&BMI<35){
        System.out.println("I度肥胖!");
    }
    else if(BMI>=35&&BMI<40){
        System.out.println("II度肥胖!");
    }
    else{
        System.out.println("III度肥胖!");
    }

}

T5.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入当月的利润:(万元)");
    float profit = scan.nextFloat();
    float bonuses = 0f;
    if(profit<=10){
        bonuses = profit*0.1f;
    }
    else if(profit<=20){
        bonuses = 10*0.1f+(profit-10)*0.075f;
    }
    else if(profit<=40){
        bonuses = 10*0.1f+10*0.075f+(profit-20)*0.05f;
    }
    else if(profit<=60){
        bonuses = 10*0.1f+10*0.075f+20*0.05f+(profit-40)*0.03f;
    }
    else if(profit<=100){
        bonuses = 10*0.1f+10*0.075f+20*0.05f+20*0.03f+(profit-60)*0.015f;
    }
    else if(profit>100){
        bonuses = 10*0.1f+10*0.075f+20*0.05f+20*0.03f+40*0.015f*(profit-100)*0.01f;
    }
    System.out.println("你的奖金为:"+bonuses+"万元!");
}

T6.

public static void main(String[] args) {
    System.out.println("请输入你要进行的运算:(加减乘除)");
    Scanner scan = new Scanner(System.in);
    String symbol = scan.nextLine();
    System.out.println("请输入第一个数据:");
    float num1 = scan.nextFloat();
    System.out.println("请输入第二个数据:");
    float num2 = scan.nextFloat();
    float result =0f;

    //进行加减乘除运算
    switch(symbol){
        case "加":result = num1+num2;break;
        case "减":result = num1-num2;break;
        case "乘":result = num1*num2;break;
        case "除":
            if(num2==0){
                System.out.println("除数不能为0:");
            }else{
                result = num1/num2;
            }break;
        default:
            System.out.println("请输入加减乘除运算!");break;
    }
    System.out.println(result);
}

T7.

public static void main(String[] args) {
    //创建Scanner类的对象
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入三个整数:");
    //获取数据
    int a=sc.nextInt();
    int b=sc.nextInt();
    int c=sc.nextInt();

    if(a>=b){
        if(b>=c){//a>=b>=c
            System.out.println(c+","+b+","+a);
        }else if(c>=a){//c>=a>=b
            System.out.println(b+","+a+","+c);
        }else{//a>=c>=b
            System.out.println(b+","+c+","+a);
        }
    }else if(b<=c){//c>=b>=a
        System.out.println(a+","+b+","+c);
    }else if(c<=a){//b>=a>=c
        System.out.println(c+","+a+","+b);
    }else{//b>=c>=a
        System.out.println(a+","+c+","+b);
    }sc.close();
}

T8.

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入四位数整数");
    int num=sc.nextInt();
    if(num%2==0){
        System.out.println("该整数包含了"+(num/2+1)+"个偶数");
        System.out.println("该整数包含了"+(num/2)+"个奇数");
    }else{
        System.out.println("该整数包含了"+(num/2+1)+"个偶数");
        System.out.println("该整数包含了"+(num/2+1)+"个奇数");
    }sc.close();
}

T9.

public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    System.out.println("请输入您的身高,体重:");

    int height=sc.nextInt();
    int weight=sc.nextInt();
    int sum=(height-108)*2;
    if(sum>=weight-10){
        if(sum<=weight+10){
            System.out.println("你的体重合适");
        }else if(sum>=weight+10){
            System.out.println("你的体重不合适");
        }
    }else{
        System.out.println("你的体重不合适");
    }sc.close();
}

T10.

public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    System.out.println("请输入三个整数:");
    int a=sc.nextInt();
    int b=sc.nextInt();
    int c=sc.nextInt();
    double p=(a+b+c)/2;
    double s=(p*(p-a)*(p-b)*(p-c));
    if(a+b>c&a-b<c&a+c>b&a-c<b&b+c>a&b-c<a){
        if(a==b&b==c){
            System.out.println("这个三角形为等边三角形");
            System.out.println("这个三角形的面积为"+Math.sqrt(s));
        }else if((a==b&b!=c)|a==c&c!=b|b==c&c!=a){
            System.out.println("这个三角形为等腰三角形");
            System.out.println("这个三角形的面积为"+Math.sqrt(s));
        }else if((a*a+b*b==c*c)|(a*a+c*c==b*b)|(b*b+c*c==a*a)){
            System.out.println("这个三角形为直角三角形");
            System.out.println("这个三角形的面积为"+Math.sqrt(s));
        }else{
            System.out.println("这个三角形为普通三角形");
            System.out.println("这个三角形的面积为"+Math.sqrt(s));
        }
    }
    else{
        System.out.println("这三个数不能构成三角形");
    }sc.close();
}

T11.

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入一个四位以内整数:");
    int num=sc.nextInt();
    int a,b,c,d,e;
    if (num/1000 <1){
        if (num/100<1){
            c=num/10;
            d=num%10;
            e=d*10+c;
            System.out.println(e);
        }else{
            b=num/100;
            c=num%100/10;
            d=num%10;
            e=d*100+c*10+b;
            System.out.println(e);
        }
    }else{
        a=num/1000;
        b=num%1000/100;
        c=num%100/10;
        d=num%10;
        e=d*1000+c*100+b*10+a;
        System.out.println(e);
    }
}

T12.

public static void main(String[] args) {
    float PRICE1=0.4463f;
    float PRICE2=0.4663f;
    float PRICE3=0.5663f;
    System.out.println("请输入月用电量(千瓦):");
    Scanner scan = new Scanner(System.in);
    float electricityConsumption = scan.nextFloat();
    float price =0f;
    if(electricityConsumption<=150){
        price = electricityConsumption*PRICE1;
    }
    if(electricityConsumption>150&&electricityConsumption<=400){
        price = 150*PRICE1+(electricityConsumption-150)*PRICE2;
    }
    if(electricityConsumption>400){
        price = 150*PRICE1+250*PRICE2+(electricityConsumption-400)*PRICE3;
    }
    System.out.println("你需要支付的电费为:"+price);
}

T13.

public static void main(String[] args) {
    Scanner dogAge= new Scanner(System.in);
    System.out.println("请输入狗年龄");
    int age=dogAge.nextInt();
    if(age>0 && age<=2){
        System.out.println("相当于人的年龄:" + age * 10.5);
    }else if(age>2){
        System.out.println("相当于人的年龄:" + (2* 10.5+(age-2)*4));
    }else{
        System.out.println("狗狗还没出生");
    }
}

T14.

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    char username; //首先声明char变量

    System.out.print("请输入您的用户名: ");
    username = scan.next().charAt(0);

    System.out.print("请输入您的密码: ");
    int password = scan.nextInt();//声明int变量存储密码

    if(username == '青' && password == 123){

        System.out.println("欢迎你,青");

    }else{

        System.out.println("对不起,你不是青");
    }
}

T15.

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入字母:");
    char ch=sc.next().charAt(0);//取输入的第一个字母
    if(ch>='A'&ch<='Z'){
        System.out.println((char)(ch+32));//强制转化
    }else if(ch>='a'&ch<='z'){
        System.out.println((char)(ch-32));
    }sc.close();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值