第五章 7-2 水果饮料滋味如何(本题目要求读入1个整数,代表某种水果或饮料,然后输出它的味道。)

本题目要求读入1个整数,代表某种水果或饮料,然后输出它的味道。 测试类已写好(参见Main.java),不得修改main方法,补充完善相关定义。

Main.java.pdf

输入格式:

输入在一行中给出1个绝对值不超过3整数。(1-苹果,2-葡萄,3-咖啡)

输出格式:

对每一组输入,在一行中输出其滋味,参见样例。

输入样例:

在这里给出一组输入。例如:

1
2
3

输出样例:

在这里给出相应的输出。例如:

Apple:is sweet
Grape:is sour
Coffe:is bitter

import java.util.Scanner;


public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int a;
        ITaste s = null;
        while (sc.hasNext()) {
            a = sc.nextInt();
            switch (a) {
                case ITaste.APPLE:
                    s = new Apple();
                    break;
                case ITaste.GRAPE:
                    s = new Grape();
                    break;
                case ITaste.COFFE:
                    s = new Coffe();
                    break;
            }
            System.out.println(s + ":" + s.tell());
        }
    }
}

class ITaste{
    static final int APPLE = 1;
    static final int GRAPE = 2;
    static final int COFFE = 3;

    public ITaste() {
    }
    
    public String tell(){
        return null;
    }
}

class Apple extends ITaste{


    public  String tell(){
        return "is sweet";
    }
    @Override
    public String toString() {
        return "Apple";
    }
}

class Grape extends ITaste{
    public  String tell(){
        return "is sour";
    }
    @Override
    public String toString() {
        return "Grape";
    }
}

class Coffe extends ITaste{
    public String tell(){
        return "is bitter";
    }
    @Override
    public String toString() {
        return "Coffe";
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值