加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

一个模拟汽车里程表的Java程序

(2008-02-28 20:42:14)
标签:

java

里程表

杂谈

分类: Java学习
 一个模拟汽车里程表的Java程序

一.程序说明:
主类类名:Drive
参数:
 距离distance
 用油oil
 每升里程数peroil
 平均里程数average

测试类名:DriveTest
测试数据:
driver1:
        distance:123    
 oil:12    
driver2:
        distance:200  
 oil:26 

测试结果: 
Enter the distance for driver:123
Enter the oil for driver:12

driver1:
        distance:123km    oil:12L   peroil:10.25km
    So far,the average is:10.25km.

Pay attetion:Enter -1 for distance to quit!

二.程序源码:

1.主类源码:

public class Drive
{
private int distance;
private int oil;
private double peroil;

 public void setDistance(int imDistance)
 {
 distance=imDistance;
 }
 
 public int getDistance()
 {
 return distance;
 }

        public void setOil(int imOil)
 {
 oil=imOil;
 }
 
 public int getOil()
 {
 return oil;
 }

 
 public double getPeroil()
 {
 peroil=(double)distance/oil;
 return peroil;
 }
}

2.测试类名:

import java.util.Scanner;

public class DriveTest
{
public static void main(String args[])
{
Drive drive=new Drive();

Scanner input=new Scanner(System.in);

int depositDistance;
int depositOil;
int addDistance=0;
int addOil=0;
double average;
int i=1;

System.out.printf("Enter the distance for driver:");
depositDistance=input.nextInt();
System.out.printf("Enter the oil for driver:");
depositOil=input.nextInt();
System.out.println();

while(depositDistance!=-1)
{
 drive.setDistance(depositDistance);
 System.out.printf("driver%d:\n        distance:%dkm",i,drive.getDistance());
 drive.setOil(depositOil);
 System.out.printf("    oil:%dL   peroil:%.2fkm\n",drive.getOil(),drive.getPeroil());
 ++i;
 addDistance+=depositDistance;
 addOil+=depositOil;
 average= (double) addDistance/addOil;
 System.out.printf("    So far,the average is:%.2fkm.",average);
 
 System.out.println();
 System.out.println();
 System.out.printf("Pay attetion:Enter -1 for distance to quit!\n");
 
 System.out.println();
 System.out.printf("Enter the distance for driver:");
 depositDistance=input.nextInt();
 if(depositDistance==-1);
 else
 {System.out.printf("Enter the oil for driver:");
 depositOil=input.nextInt();
 System.out.println();
 }
}
}
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有