问答详情
源自:12-1 综合练习

帮忙看看这样编程思路对不对

//已经创建了单独的keche,kache,paka类,这个zuche类中主要是实例化对象。

package chexing;


import java.util.Scanner;


public class zuche {


public static void main(String[] args) {

// 创建小客车对象

keche xiaokeche = new keche();

xiaokeche.xuhao = 1;

xiaokeche.cheming = "宝马510";

xiaokeche.zaike = 5;

xiaokeche.zujin = 800;

// 创建大客车对象

keche dakeche = new keche();

dakeche.xuhao = 2;

dakeche.cheming = "丰田考斯拉";

dakeche.zaike = 40;

dakeche.zujin = 2000;

// 创建轻货车

kache qinghuoche = new kache();

qinghuoche.xuhao = 3;

qinghuoche.cheming = "松花江";

qinghuoche.zaihuo = 5;

qinghuoche.zujin = 200;

// 创建重货车

kache zhonghuoche = new kache();

zhonghuoche.xuhao = 4;

zhonghuoche.cheming = "陕汽重卡";

zhonghuoche.zaihuo = 20;

zhonghuoche.zujin = 500;

// 创建皮卡

pika pika = new pika();

pika.xuhao = 5;

pika.cheming = "长城";

pika.zaihuo = 2;

pika.zujin = 300;

pika.zaike = 5;

// 询问用户是否租车;

System.out.println("欢迎光临哒哒租车!");

System.out.println("请问您是否要租车:1是0否");


@SuppressWarnings("resource")

// 获取用户输入值,并展示车辆性息;

int i = new Scanner(System.in).nextInt();

if (i != 1 && i != 0) {

for (; i != 1 && i != 0;) {

System.out.println("您出入的信息有误,请重新输入:");

i = new Scanner(System.in).nextInt();

}

}

if (i == 1) {

System.out.println("您可租车的类型和价目表:");

System.out.println("序号                               "

+ "车名                               "

+ "容量                                             "

+ "租金          ");

xiaokeche.shuchu();

dakeche.shuchu();

qinghuoche.shuchu();

zhonghuoche.shuchu();

pika.shuchu();


// 询问用户租车数量;

System.out.println("请输入您要租车的数量:");

int j = new Scanner(System.in).nextInt();

// 定义总租金;

int zongzujin = 0;

int zairenshu = 0;

int zaihuoshu = 0;

String xuanzechexing = "";

String xuanzehuoche = "";

// 依次询问用户选择车型;

for (int n = 0; n < j; n++) {

System.err.println("请您输入要租的第" + (n + 1) + "辆车的序号:");

int shuruxuhao = new Scanner(System.in).nextInt();

// 判断用户每次选择车型,并累加租金,车名,载货,载客量;

switch (shuruxuhao) {

case 1:

zongzujin = zongzujin + xiaokeche.zujin;

xuanzechexing = xuanzechexing + "   " + xiaokeche.cheming;

zairenshu = zairenshu + xiaokeche.zaike;

break;

case 2:

zongzujin = zongzujin + dakeche.zujin;

xuanzechexing = xuanzechexing + "   " + dakeche.cheming;

zairenshu = zairenshu + dakeche.zaike;

break;

case 3:

zongzujin = zongzujin + qinghuoche.zujin;

xuanzehuoche = xuanzehuoche + "   " + qinghuoche.cheming;

zaihuoshu = zaihuoshu + qinghuoche.zaihuo;

break;

case 4:

zongzujin = zongzujin + zhonghuoche.zujin;

xuanzehuoche = xuanzehuoche + "   " + zhonghuoche.cheming;

zaihuoshu = zaihuoshu + zhonghuoche.zaihuo;

break;

case 5:

zongzujin = zongzujin + pika.zujin;

xuanzechexing = xuanzechexing + "   " + pika.cheming;

xuanzehuoche = xuanzehuoche + "   " + pika.cheming;

zaihuoshu = zaihuoshu + pika.zaihuo;

zairenshu = zairenshu + pika.zaike;

break;

default:

System.out.println("请您输入正确车辆序号:");

}

}

// 询问租车总天数;

System.out.println("请您输入要租车的天数:");

int zuchetianshu = new Scanner(System.in).nextInt();

// 计算总租金;

zongzujin = zongzujin * zuchetianshu;

// 展示总账单;

System.out.println("您的账单为:");

System.out.println("***您选择的客车有:");

System.out

.println(xuanzechexing + "       总载客数:" + zairenshu + "人");

System.out.println("***您选择的货车有:");

System.out.println(xuanzehuoche + "       总载货数:" + zaihuoshu + "吨");

System.out.println("***您的总租金是:" + zongzujin + "元");

} else {

System.out.println("谢谢光临哒哒租车,祝您生活愉快!");

}

}

}


提问者:石三春 2017-07-14 01:04

个回答

  • 石三春
    2017-07-14 01:23:44

    default:

    System.out.println("请您输入正确车辆序号:");

    //应该再加一句

    n-=1;

    break;

  • 石三春
    2017-07-14 01:16:53

    //这是keche类,kache,pika类和这个类似;

    package chexing;


    public class keche extends Che {

    int xuhao;

    String cheming;

    int zaike;

    int zujin;


    @Override

    public void shuchu() {

    // TODO Auto-generated method stub

    System.out.println(xuhao + "    " + cheming + "            " + "载客"

    + zaike + "人" + "              " + "租金" + zujin + "元/天");


    }


    }