我觉得自己思维还是跟不上
没有什么思路,老师,能不能给一份完整的代码作为参考呀。
没有什么思路,老师,能不能给一份完整的代码作为参考呀。
2016-09-10
package cc.candydoll.yun;
import java.util.Scanner;
public class Initail {
/**
* 做一个答答租车系统,显示车的种类供用户选择
* 车的种类分为三种,载人、载货、即可以载人又可以载货
* 显示出用户选择的车名,同时显示出用户选择的车名
* 最后再计算总金额
* @param args
*/
static Scanner in=new Scanner(System.in);
static int sl;//保存用户租车的
static int n;
static int count=0;//统计输入序号的次数
static int d;//获取天数
static int allMoney=0;//统计总金额
static int allPeople=0;//统计总人数
static double allGoods=0;//统计货物重量
public static void main(String[] args) {
System.out.println("请问是否进入答答租车系统:1是 2否");
n=in.nextInt();//获取用户输入的值来确认是否进入系统
if(n==1){
Car cars[]={new People(1,"奥迪A4",500,4),//多态引用多个类 用数组遍历多个对象
new People(2,"马自达6",400,4),
new Pika(3,"皮卡雪6",450,4,2),
new People(4,"金龙",800,20),
new Goods(5,"松花江",400,4),
new Goods(6,"依维柯",1000,20)};
System.out.println("请输入租车的数量:");
sl=in.nextInt();//获取用户租车的数量
int[] xh=new int[sl];//通过数组保存输入的序号
while(count<sl){
System.out.println("请输入第"+(count+1)+"辆车的序号:");
int x=in.nextInt();//输入的序号
xh[count]=x;//将序号存储在数组中
count++;//通过累加的形式,将序号一一存储在数组中
continue;
}
System.out.println("请输入租车天数:");
d=in.nextInt();
//此部分计算
//计算人数
for(int i=0;i<cars.length;i++){//循环cars数组。
for(int j=0;j<sl;j++){//循环用户想租车的序号
if(cars[i].no==xh[j]&&cars[i].people>0&&cars[i].good>0){//此处判定pika类型的车辆,计算结果
System.out.print(cars[i].name+" ");//获取pika车辆的名称
allGoods=allGoods+cars[i].good;//获取pika类型车辆的货物重量,进行计算
allPeople=allPeople+cars[i].people;//获取pika类型的人数,进行计算
allMoney=allMoney+cars[i].money;//获取pika类型的租金,进行计算
}
}
}
System.out.println("***载人的车:");
for(int i=0;i<cars.length;i++){
for(int j=0;j<sl;j++){
if(cars[i].no==xh[j]&&cars[i].people>0){//此处判定载人类型的车辆,计算结果载人数
System.out.print(cars[i].name+" ");
allPeople=allPeople+cars[i].people;
allMoney=allMoney+cars[i].money;
}
}
}
System.out.println("总人数:"+allPeople);
System.out.println("***可载物的车有:");
for(int i=0;i<cars.length;i++){//此处判定载货类型的车辆,计算结果载物重量
for(int j=0;j<sl;j++){
if(cars[i].no==xh[j]&&cars[i].good>0){
System.out.println("***载货的车:");
System.out.print(cars[i].name+" ");
allGoods=allGoods+cars[i].good;
allMoney=allMoney+cars[i].money;
}
}
}
System.out.println("总载货量:"+allGoods+"吨");
System.out.println("总金额"+allMoney*d);
}else{
System.out.println("即将退出答答租车系统");
}
}
}package cc.candydoll.yun;
class Car {
int no;
String name;
int money;
int people;
double good;
public Car(int no,String name,int money,double good){
this.no=no;
this.name=name;
this.money=money;
this.good=good;
}
public Car(int no,String name,int money,int people){
this.no=no;
this.name=name;
this.money=money;
this.people=people;
}
public Car(int no,String name,int money,int people,double good){
this.no=no;
this.name=name;
this.money=money;
this.people=people;
this.good=good;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
public double getGood() {
return good;
}
public void setGood(double good) {
this.good = good;
}
}举报