为了账号安全,请及时绑定邮箱和手机立即绑定

重新来下

import java.util.Scanner;


public class Car {
       public String name;
       public int money;
       public int people;
       public int things;
       public double thing;
       public Car(String name, int money, int people){
    	   this.name=name;
    	   this.money=money;
    	   this.people=people;
    	   System.out.println(   name  + "      "  + money  + "元/天"  + "         " + "载人:" + people + "人");
       }
       public Car (String name, int money, int people, int things){
    	   
    	   this.name=name;
    	   this.money=money;
    	   this.people=people;
    	   this.things=things;
    	   System.out.println( name +  "                             "  + money + "元/天" + "         " + "载人:" + people + "人" + "         " + "载货" + things + "吨" );
       }
       
    public Car (String name, int money,  double thing){
    	   
    	   this.name=name;
    	   this.money=money;
    	   this.thing=thing;
    	   System.out.println(  name +  "                       "  + money + "元/天" + "         " + "载货:" + thing + "吨" );
       }
       
       
}





import java.util.Scanner;

public class InitialCar {


public static void main(String[] args) {

// TODO Auto-generated method stub

  System.out.println("欢迎使用嗒嗒租车系统");

    System.out.println("你是否想要租车:1是    0否");

    Scanner scanner =new Scanner(System.in);

    int a=scanner.nextInt();

    if(a==1){

System.out.println("你可租车的类型及其价目表:"); 

System.out.println("序号         " + "汽车名称                   " + "租金                      " + "容量 "); 

Car car1=new Car("1.   奥迪A4", 500, 4);

Car car2=new Car("2.   马自达6", 400, 4);

Car car3=new Car("3.   皮卡", 450, 4,2);

Car car4=new Car("4.   金龙32", 800, 20);

Car car5=new Car("5.   松花江", 400, 4.0);

Car car6=new Car("6.   依擒科", 1000, 20.0);

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

Scanner sc =new Scanner(System.in);

  int b=sc.nextInt();

  

  int price=0;

  int sum=0;

  int weight=0;

  String logo=" ";

  String logo1=" ";

 for(int i=1; i<=b; i++){

System.out.println("请输入第" + i + "辆车的序号"); 

Scanner scnn =new Scanner(System.in);

  int c=scnn.nextInt();

  switch (c){

  case 1:

  logo="奥迪A4";

      price=500;

      sum=4;

      break;

 case 2:

  logo="马自达6";

      price=400;

      sum=4;

     break;

case 3:

  logo="皮卡";

      price=450;

      sum=4; 

      weight=2;

      logo1="皮卡";

      break;

case 4:

  logo="金龙32";

      price=800;

      sum=20; 

      break;

case 5:

  logo1="松花江";

      price=400;

      weight=4;

      break;

case 6:

  logo1="依擒科";

      price=1000;

      weight=20; 

      break;

      

      

  }

  logo += logo; 

  sum += sum ;

  weight +=weight;

  price +=price;

  logo1 += logo1;  

           

    

 }

 

System.out.println("请输入租车天数");

Scanner day =new Scanner(System.in);

  int days=day.nextInt();

 

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

System.out.println("*****可载人的车有:");  

System.out.println(logo + "   共载人:" + sum);

System.out.println("*****可载货的车有:" );

System.out.println(logo1 + "   共载货:" + weight); 

System.out.println("租车总价格:   " + price*days + "元"); 

  

   }else{

  System.out.println("你已退出!");

   }

        

}


}



运行结果是乱的,不知道哪里出了问题

望高手指点


正在回答

1 回答

  logo += logo; 

  sum += sum ;

  weight +=weight;

  price +=price;

  logo1 += logo1;  

这里出了问题,看上去像是没有问题,但是测试过后,你会发现结果会比正确值多一半。。比如sum+=sum,假设case:1时,结果会是sum=4+4,但是我们是需要它的结果是sum=0+4;所以需要改成sum1+=sum,重新定义一个int sum1=0,就可以了,同理另外四个也需要改哈。。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第二季 升级版
  • 参与学习       530714    人
  • 解答问题       6091    个

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

进入课程
意见反馈 帮助中心 APP下载
官方微信