本题目要求读入1个整数,代表某种水果或饮料,然后输出它的味道。 测试类已写好(参见Main.java),不得修改main方法,补充完善相关定义。
输入格式:
输入在一行中给出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";
}
}