【JAVA-C】流程控制 if-else 编程题

发布于:2022-12-24 ⋅ 阅读:(364) ⋅ 点赞:(0)

目录

1.if-else 成绩判断 

2.if-else排序-从大到小

3.编写程序,判断两数之和

4.计算狗狗年龄


1.if-else 成绩判断 

岳小鹏参加Java考试,他和父亲岳不群达成承诺:
如果:
成绩为100分时,奖励一辆BMW;
成绩为(80,99]时,奖励一台iphone xs max;
当成绩为[60,80]时,奖励一个 iPad;
其它时,什么奖励也没有。
请从键盘输入岳小鹏的期末成绩,并加以判断。

import java.util.Scanner;
class IfTest {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		System.out.println("请输入岳小鹏期末成绩:(0-100)");
		int score = scan.nextInt();

		if(score == 100){
			System.out.println("奖励一辆BMW");//be my wife!  BMW <---> MSN
		}else if(score > 80 &&  score <= 99){
			System.out.println("奖励一台iphone xs max");
		}else if(score >= 60 && score <= 80){
			System.out.println("奖励一个 iPad");
		}else{
			System.out.println("什么奖励也没有");
		}
	}
}

2.if-else排序-从大到小

由键盘输入三个整数分别存入变量num1、num2、num3,对它们进行排序(使用 if-else if-else),并且从大到小输出。

import java.util.Scanner;
class GoT{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入第一个整数: ");
        int num1 = scanner.nextInt();
        System.out.println("请输入第二个整数:");
		int num2 = scanner.nextInt();
		System.out.println("请输入第三个整数:");
		int num3 = scanner.nextInt();

        if(num1 >= num2){
            if(num2 >= num3){
                  System.out.println(num1 + "," + num2 + "," + num3);}
            else if (num1 >= num3){
                  System.out.println(num1 + "," + num3 + "," + num2);}
            else {System.out.println(num3 + "," + num1 + "," + num2);}                        
        }
        else 
        {if(num2 <= num3){
            System.out.println(num3 + "," + num2 + "," + num1);}
        else if(num3 <= num1){
            System.out.println(num2 + "," + num1 + "," + num3);}
        else {System.out.println(num2 + "," + num3 + "," + num1);}
        }
	}
}

3.编写程序,判断两数之和

声明2int型变量并赋值。判断两数之和,如果大于等于50,打印“hello world!

import java.util.Scanner;
class GoT50{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入第一个整数: ");
        int num1 = scanner.nextInt();
        System.out.println("请输入第二个整数:");
        int num2 = scanner.nextInt();
        int num3 = num1 + num2;

        if(num3 >= 50){        
            System.out.println("hellow world!");}          
        else 
        { System.out.println("Bye");}
        }
}

4.计算狗狗年龄

我家的狗 X 岁了, X 岁的狗相当于人类多大呢?其实,狗的前两年每一年相当于人类的10.5 岁,之后每增加一年就增加四岁。那么 5 岁的狗相当于人类多少年龄呢?应该是:10.5 + 10.5 + 4 + 4 + 4 = 33 岁。
import java.util.Scanner;
class DogAge{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入狗狗年龄: ");
        double num1 = scanner.nextDouble();
		double num2;

        if(num1 <= 2){       
			num2 = num1 * 10.5 ;
            System.out.println(num1+"岁的狗狗相当于"+num2 + "岁的人类");}          
        else 
        {   num2 = 2 * 10.5 + (num1 - 2) * 4 ;
			System.out.println(num1+"岁的狗狗相当于"+num2 + "岁的人类");}
        }
}

本文含有隐藏内容,请 开通VIP 后查看