编写程序实现软料购买:编写程序,接收用户输入的信息,选择购买的饮料。可供选择的饮料有:咖啡、矿泉水和可乐。其中,购买咖啡时可以选择:加糖、加奶还是什么都不加。购买可乐时可以选择:买可口可乐还是百事可乐。
程序:
package com.qf.a_homework;
import java.util.Scanner;
class Drink {
String name;
String taste;
public void info () {
System.out.println("当前购买的是:" + name + ", 口味是:" + taste);
}
}
class Coffee extends Drink{
public void info () {
System.out.println("当前购买的是:" + name + ", 口味是:" + taste);
}
}
class Water extends Drink {
public void info () {
System.out.println("当前购买的是:" + name + ", 口味是:" + taste);
}
}
class Colar extends Drink{
public void info () {
System.out.println("当前购买的是:" +taste+ name);
}
}
public class Demo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要购买的饮料:1.咖啡 2.矿泉水 3.可乐");
switch (scanner.nextInt()) {
case 1:
Coffee coffee = new Coffee();
coffee.name = "咖啡";
System.out.println("请选择配料:1. 加糖 2.加奶 3.什么都不加" );
switch (scanner.nextInt()) {
case 1:
coffee.taste = "加糖";
break;
case 2:
coffee.taste = "加奶";
break;
case 3:
coffee.taste = "什么也不加";
break;
default:
break;
}
coffee.info();
break;
case 2:
Water water = new Water();
water.name = "矿泉水";
System.out.println("当前购买的是" + water.name);
break;
case 3:
Colar colar = new Colar();
colar.name = "可乐";
System.out.println("请输入:1.可口 2.百事");
switch (scanner.nextInt()) {
case 1:
colar.taste = "可口";
colar.info();
break;
case 2:
colar.taste = "百事";
colar.info();
break;
default:
System.out.println("请输入正确的口味");
break;
}
break;
default:
System.out.println("没有此商品");
break;
}
}
}