Java编程——一个景区根据游人的年龄收取不同价格的门票。请编写游人类,根据年龄段决定能够购买的门票价格并输出,然后写出测试类测试该类(类的基本实现)
1 package tourist; 2 public class Tourist { 3 int age; 4 int ticketPrice; 5 6 public void setAge(int age) 7 { 8 this.age=age; 9 } 10 public void ticket() 11 { 12 if(age>0&&age<12) 13 ticketPrice=20; 14 else if(age<20) 15 ticketPrice=40; 16 else if(age<50) 17 ticketPrice=80; 18 else 19 ticketPrice=35; 20 System.out.println("门票价格:"+ticketPrice); 21 } 22 }
1 package tourist; 2 import java.util.Scanner; 3 public class Test extends Tourist{ 4 public static void main(String[] args) { 5 Scanner input=new Scanner(System.in); 6 Test t1=new Test(); 7 System.out.print("请输入年龄:"); 8 t1.setAge(input.nextInt()); 9 t1.ticket(); 10 } 11 }