永发信息网

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

答案:3  悬赏:20  手机版
解决时间 2021-02-23 20:14
  • 提问者网友:人生佛魔见
  • 2021-02-23 13:46
老师要求用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);

}
}
最佳答案
  • 五星知识达人网友:毛毛
  • 2021-02-23 14:05
改了几个地方,都注释了,感觉用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);

}
}
全部回答
  • 1楼网友:平生事
  • 2021-02-23 16:44
晕,你都不会问问题,你连出了什么问题都不说,谁闲着没事能一行一行仔仔细细的看,麻烦楼主先贴出来什么问问题,再让大家解决。
  • 2楼网友:骨子里都是戏
  • 2021-02-23 15:36
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); }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯