您的位置:首页 > 其它

使用do...while循环简化程序

2016-06-21 11:05 316 查看
写Java小程序已有快10年了,一直觉得do...while循环语句没什么用,今天有了新发现:可以使用do...while简化程序


简化前:根据业务需求,一些操作在整个程序中需至少执行一次,于是将这部分代码放在了while循环体之前。但这部分代码和循环体内重复执行的代码基本相同

简化后:使用do...while循环,将需至少执行一次的代码放到循环体内,无需单独列出。

简化的优点:程序更为简洁(代码行数明显减少,便于后期维护)

不简化的优点:可将循环体外的代码作为程序初始化的division,可作一些定制,以区别于循环体内的部分

简化前代码(共94行):

//Java how to program, 10th edition, Exercise 4.17
//by Pandenghuang@163.com
/**
*问题陈述:
*(Gas Mileage) Drivers are concerned with the mileage their automobiles get. One driver has
*kept track of several trips by recording the miles driven and gallons used for each tankful. Develop
*a Java application that will input the miles driven and gallons used (both as integers) for each trip.
*The program should calculate and display the miles per gallon obtained for each trip and print the
*combined miles per gallon obtained for all trips up to this point. All averaging calculations should
*produce floating-point results. Use class Scanner and sentinel-controlled repetition to obtain the
*data from the user.
*/
import java.util.*;

public class GasMileage
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);

int miles=0;
int gallons=0;
int milesTotal=0;
int gallonsTotal=0;
int inputTimes=1;
int validInputTimes=0;

System.out.printf("开始第%d次输入\n",inputTimes);
System.out.print("请输入行驶里程(输入-1结束输入):");
miles=input.nextInt();
if (miles==-1)
{
System.out.println("您没有输入足够的有效数据。");
return;
}
System.out.println("本次行驶里程为:"+miles);
milesTotal+=miles;

System.out.print("请输入油耗(输入-1退出程序):");
gallons=input.nextInt();
if (gallons==-1)
{
System.out.println("您没有输入足够的有效数据。");
return;
}
System.out.println("本次油耗为:"+gallons+"加仑");
gallonsTotal+=gallons;
System.out.printf("本次行程每加仑行驶里程为:%.2f\n\n",(double)miles/gallons);
++inputTimes;
++validInputTimes;

while (miles!=-1 && gallons!=-1)
{
System.out.printf("开始第%d次输入\n",inputTimes);
System.out.print("请输入行驶里程(输入-1结束输入):");
miles=input.nextInt();
if (miles!=-1)
{
System.out.println("本次行驶里程为:"+miles);
System.out.println("请输入油耗(输入-1退出程序):");
gallons=input.nextInt();
if (gallons!=-1)
{
System.out.println("本次行程油耗为:"+gallons+"加仑");
milesTotal+=miles;
gallonsTotal+=gallons;
++inputTimes;
++validInputTimes;
System.out.printf("本次行程每加仑行驶里程为:%.2f\n\n",(double)miles/gallons);
}
else
{
System.out.println("\n\n您已结束数据输入。");
break;
}

}
else
{
System.out.println("\n\n您已结束数据输入。");
break;
}

}

System.out.print("\n*******************************************************************\n");
System.out.printf("您总共输入了%d组有效数据\n",validInputTimes);
System.out.printf("您的行驶总里程为:%d 公里,总油耗为:%d 加仑\n",milesTotal,gallonsTotal);
System.out.printf("平均每加仑行驶里程为:%.2f\n",(double)milesTotal/gallonsTotal);
System.out.print("*******************************************************************\n\n\n");

}
}


简化后代码(共75行代码,减少近20行代码):

package example;

//Java how to program, 10th edition, Exercise 4.17
//by Pandenghuang@163.com
/**
*问题陈述:
*(Gas Mileage) Drivers are concerned with the mileage their automobiles get. One driver has
*kept track of several trips by recording the miles driven and gallons used for each tankful. Develop
*a Java application that will input the miles driven and gallons used (both as integers) for each trip.
*The program should calculate and display the miles per gallon obtained for each trip and print the
*combined miles per gallon obtained for all trips up to this point. All averaging calculations should
*produce floating-point results. Use class Scanner and sentinel-controlled repetition to obtain the
*data from the user.
*/
import java.util.*;

public class GasMileage
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);

int miles=0;
int gallons=0;
int milesTotal=0;
int gallonsTotal=0;
int inputTimes=1;
int validInputTimes=0;

do
{
System.out.printf("开始第%d次输入\n",inputTimes);
System.out.print("请输入行驶里程(输入-1结束输入):");
miles=input.nextInt();
if (miles!=-1)
{
System.out.println("本次行驶里程为:"+miles);
System.out.println("请输入油耗(输入-1退出程序):");
gallons=input.nextInt();
if (gallons!=-1)
{
System.out.println("本次行程油耗为:"+gallons+"加仑");
milesTotal+=miles;
gallonsTotal+=gallons;
++inputTimes;
++validInputTimes;
System.out.printf("本次行程每加仑行驶里程为:%.2f\n\n",(double)miles/gallons);
}
else
{
System.out.println("\n\n您已结束数据输入。");
break;
}

}
else
{
System.out.println("\n\n您已结束数据输入。");
break;
}

} while (miles!=-1 && gallons!=-1);

System.out.print("\n*******************************************************************\n");
System.out.printf("您总共输入了%d组有效数据\n",validInputTimes);
System.out.printf("您的行驶总里程为:%d 公里,总油耗为:%d 加仑\n",milesTotal,gallonsTotal);
System.out.printf("平均每加仑行驶里程为:%.2f\n",(double)milesTotal/gallonsTotal);
System.out.print("*******************************************************************\n\n\n");

}
}


简化后运行结果(代码行数减少了,但依然很健壮:-D):

Test round 1:

开始第1次输入

请输入行驶里程(输入-1结束输入):100

本次行驶里程为:100

请输入油耗(输入-1退出程序):

-1

您已结束数据输入。

*******************************************************************

您总共输入了0组有效数据

您的行驶总里程为:0 公里,总油耗为:0 加仑

平均每加仑行驶里程为:NaN

*******************************************************************

Test round 2:

开始第1次输入

请输入行驶里程(输入-1结束输入):100

本次行驶里程为:100

请输入油耗(输入-1退出程序):

20

本次行程油耗为:20加仑

本次行程每加仑行驶里程为:5.00

开始第2次输入

请输入行驶里程(输入-1结束输入):200

本次行驶里程为:200

请输入油耗(输入-1退出程序):

40

本次行程油耗为:40加仑

本次行程每加仑行驶里程为:5.00

开始第3次输入

请输入行驶里程(输入-1结束输入):10

本次行驶里程为:10

请输入油耗(输入-1退出程序):

-1

您已结束数据输入。

*******************************************************************

您总共输入了2组有效数据

您的行驶总里程为:300 公里,总油耗为:60 加仑

平均每加仑行驶里程为:5.00

*******************************************************************


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: