永发信息网

java do...while 菜鸟问题,请高手指教!!!

答案:3  悬赏:20  手机版
解决时间 2021-02-23 20:14
老师要求用do while做一个交通罚款的练习,研究了很久总是存在一些问题请高手指正:
先显示欢迎页面,换页继续
要求输入驾照 退出输入quit 不区分大小写
输入之前违章次数 不能小于0 如果小于零要求重新输入
输入违章类型S或M 不能是其他字符 如果不是S或M要求重新输入
如果是S类 罚款为250, 如果是M类且次数为0,罚款60.95, 次数大于0,罚款80

显示驾照、违章次数、罚款金额

循环到这里,直到用户输入驾照为QUIT,换页显示结束语以及M数量和S数量和罚款总金额

import java.util.Scanner;

public class calculateFine
{

public static void main(String[] args)
{
String driverLisence, offenceType, quit = "quit";
float fine = 0.0f, totalFine = 0.0f;
float MINOR_FINE_FIRST = 60.95f, MINOR_FINE = 80, SERIOUS_FINE = 250;
int numberOfMinor = 0, numberOfSerious = 0, numberOfOffences, loopctr;

Scanner input = new Scanner(System.in);

System.out.println("Welcome to the Traffic Fine Reporting System");
System.out.println("Please Enter the Requested Data for Each Fine");

System.out.println("\fEnter the drivers lisence number - \"quit\" to exit:");
driverLisence = input.next();
do{
System.out.println("Enter the number of previous offences - number greater of equal to zero:");
numberOfOffences = input.nextInt();

{do
{
System.out.println("Enter the number of previous offences - number greater of equal to zero:");
numberOfOffences = input.nextInt();
}
while (numberOfOffences < 0);}

System.out.println("Enter the type of offence - M (Minor) or S (Serious):");
offenceType = input.next();
{if ((offenceType.equalsIgnoreCase("s")) || (offenceType.equalsIgnoreCase("m")))
{

if (offenceType.equalsIgnoreCase("M"))
{
if (numberOfOffences == 0)
{
fine = MINOR_FINE_FIRST;
numberOfMinor++;
}
else
{
fine = MINOR_FINE;
numberOfMinor++;
}
}
if (offenceType.equalsIgnoreCase("S"))
{
fine = SERIOUS_FINE;
numberOfSerious++;}
else
{
System.out.println("Enter the type of offence - M (Minor) or S (Serious):");
offenceType = input.next();
}

System.out.println("\n\n\nDrivers License: " + driverLisence);
System.out.println("Number of Offences: "+ numberOfOffences);
System.out.println("Fine is: " + fine);}

else
{System.out.println("Enter the type of offence - M (Minor) or S (Serious):");
offenceType = input.next();}
}
System.out.println("\fEnter the drivers lisence number - \"quit\" to exit:");
loopctr = input.nextInt();
}
while (loopctr != 1);

totalFine = totalFine + fine;

System.out.println("\n\n***Processing completed***");
System.out.println("Traffic Fine Totals");
System.out.println("\nNumber of minor violations: " + numberOfMinor);
System.out.println("Number of serious violations: " + numberOfSerious);
System.out.println("Total fines: " + totalFine);

}
}
最佳答案
改了几个地方,都注释了,感觉用while更好,因为当用户第一次就输入quit时,后面还是执行。
import java.util.Scanner;
public class calculateFine
{

public static void main(String[] args)
{
String driverLisence, offenceType, quit = "quit";
float fine = 0.0f, totalFine = 0.0f;
float MINOR_FINE_FIRST = 60.95f, MINOR_FINE = 80, SERIOUS_FINE = 250;
int numberOfMinor = 0, numberOfSerious = 0, numberOfOffences;
String loopctr; //原来是整型,不知道用还干嘛的,现用于取用户是否输入quit
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Traffic Fine Reporting System");
System.out.println("Please Enter the Requested Data for Each Fine");

System.out.println("\fEnter the drivers lisence number - \"quit\" to exit:");
driverLisence = input.next();
do{
//删除了两句
do
{
System.out.println("Enter the number of previous offences - number greater of equal to zero:");
numberOfOffences = input.nextInt();
}
while (numberOfOffences < 0);

System.out.println("Enter the type of offence - M (Minor) or S (Serious):");
offenceType = input.next();
if ((offenceType.equalsIgnoreCase("s")) || (offenceType.equalsIgnoreCase("m")))
{

if (offenceType.equalsIgnoreCase("M"))
{
if (numberOfOffences == 0)
{
fine = MINOR_FINE_FIRST;
numberOfMinor++;
}
else
{
fine = MINOR_FINE;
numberOfMinor++;
}
}
if (offenceType.equalsIgnoreCase("S"))
{
fine = SERIOUS_FINE;
numberOfSerious++;
}
//删除了一个ELSE语句
System.out.println("\n\n\nDrivers License: " + driverLisence);
System.out.println("Number of Offences: "+ numberOfOffences);
System.out.println("Fine is: " + fine);
}

else
{
System.out.println("Enter the type of offence - M (Minor) or S (Serious):");
offenceType = input.next();
}

System.out.println("\fEnter the drivers lisence number - \"quit\" to exit:");
loopctr = input.next(); //应取字符
}
while (!loopctr.equalsIgnoreCase("quit")) ;//若为quit则退出

totalFine = totalFine + fine;

System.out.println("\n\n***Processing completed***");
System.out.println("Traffic Fine Totals");
System.out.println("\nNumber of minor violations: " + numberOfMinor);
System.out.println("Number of serious violations: " + numberOfSerious);
System.out.println("Total fines: " + totalFine);

}
}
全部回答
晕,你都不会问问题,你连出了什么问题都不说,谁闲着没事能一行一行仔仔细细的看,麻烦楼主先贴出来什么问问题,再让大家解决。
public static void main(string[] args) { scanner input = new scanner(system.in); system.out.println("欢迎使用myshopping购物管理"); int num; boolean flag = false; do { system.out .println("**********************************************"); system.out.println("1.客户信息管理\n2.购物结算\n3.真情回顾\n4.注销"); system.out .println("**********************************************"); system.out.print("请输入数字:"); num = input.nextint(); switch (num) { case 1: system.out.println("1.进入客户信息管理"); flag = false; break ; case 2: system.out.println("2.执行购物结算"); flag = false; break ; case 3: system.out.println("3.查看真情回顾"); flag = false; break ; case 4: system.out.println("4.已经注销"); flag = false; break ; default: system.out.println("输入错误 提示输入错误 请重新输入"); flag = true; } } while (flag); }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
下面哪一个选项不是网络中的结点.a.计算机 b.
足信平价鞋业沈巷店地址有知道的么?有点事想
世界上什么动物最机灵问
sofe是什么意思
QQ黄钻怎么取消
汽车空调冬天压缩机不启动是怎么回事
《孟子》读后感
南通狼山风景名胜区东门售票处这个地址在什么
06年奥迪A6L,2.0T,手动挡,家用,跑了大概6
哑巴发出的声音是不是很奇怪
有200块长6厘米 宽4厘米 高3厘米的长方体木块
岳麓区长沙红嗲米嗲云南过桥米线(涉外店)地址
后面有冷酷少爷四个字的小说名
无糖拉咖啡哪个牌子比较好喝??
迅雷为什么总是自动下载XMPSetup_3.exe文件
推荐资讯
大锅盖看着看着就没信号,敲打敲打又有了,有
高处作业指的是在坠落高度基准面()m及以上有
金三角品牌特价鞋地址在哪,我要去那里办事
如何在苹果电脑中安装MT4
什么相机软件好用
【保密条款】保密条款或者保密协议约定的主要
长乐(六村农家店)地址有知道的么?有点事想过
在百度凤红阁他家挑了个战国红,有
1米2的缸养10条鹦鹉要加泵吗
搞笑王者荣耀:这应该是哪吒飞最久一次吧
我想买人身意外保险,大家推荐下哪个好
小天鹅XPB35-3588怎么样?小天鹅XPB35-3588好
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?