简单的eclipseidejava程序
将完整的Java程序编码为以下内容:简单的eclipseidejava程序,java,Java,将完整的Java程序编码为以下内容: 如果违反红灯,则罚款为司机年薪的1.75% 如果是超速驾驶,则罚款为年薪的1.2%,再加上每超过限速1英里/小时工资的0.5% 你需要做什么 提示用户/官员输入驾驶员的全名 提示官员输入其全名 提示用户/官员输入驾驶员的年薪 提示警官输入违规类型(红灯1例,超速2例) 如果超速,则提示用户输入超出限速的英里/小时数 使用一套“如果……否则”评估应评估多少罚款,然后打印一份完整的报告,包括: 官员姓名 司机姓名 年薪 违规类型(你需要说明的不仅仅是1或
- 如果违反红灯,则罚款为司机年薪的1.75%
- 如果是超速驾驶,则罚款为年薪的1.2%,再加上每超过限速1英里/小时工资的0.5%
- 提示用户/官员输入驾驶员的全名
- 提示官员输入其全名
- 提示用户/官员输入驾驶员的年薪
- 提示警官输入违规类型(红灯1例,超速2例)
- 如果超速,则提示用户输入超出限速的英里/小时数
- 使用一套“如果……否则”评估应评估多少罚款,然后打印一份完整的报告,包括:
- 官员姓名
- 司机姓名
- 年薪
- 违规类型(你需要说明的不仅仅是1或2。(红灯或超速)
- 评估罚款金额(打印一个$符号并四舍五入到两位小数)
也必须考虑以下事项:
- 对于违规类型,您的程序不得接受除1或2以外的任何代码
- 您的输入必须通过不同的方法处理,而不是在main()中
- 对罚款的评估必须采用自己的方法
- 报告必须用不同的方法处理
- 对于新的驱动程序,必须重复整个程序
这是我的密码:
package FinFines_
import java.util.Scanner;
/*
* Description: This program assesses fines for traffic violations.
*/
public class FinFines_
{
static Scanner get = new Scanner(System.in);
//declarations:
static double mph = 0;
static double salary = 0;
static double fines = 0;
static int violation = 0;
static int answer = 1; //1 to continue or 0 to quit
static String officer = " ";
static String driver = " ";
public static void main(String[] args)
{
//input:
while (answer != 0) //while will allow the program to repeat for a new driver
{
input(officer, driver, salary, violation); //call input method
//calculations:
fines = calculateFines(violation, fines, salary, mph); //call calculateFines method
//Output:
disp(officer, driver, salary, violation,fines); //call disp method
//ask user if they would like to continue
System.out.println("Would you like to write a new ticket? Press 1 for yes or 0 for no: ");
answer = get.nextInt();
}//end while
System.out.println("Goodbye!");
}//end main
//=================================================================
public static void input(String officer, String driver, double salary, int violation) //Input Method
{
System.out.println("Officer, please enter your first and last name: ");
officer = get.nextLine();
System.out.println("Please the driver's first and last name: ");
driver = get.nextLine();
System.out.println("Please enter the driver's salary: ");
salary = get.nextDouble();
System.out.println("Press 1 if the driver ran a red light. Press 2 if the driver was speeding. Press 3 if the driver is subject to both violations. ");
violation = get.nextInt();
System.out.println("How many miles per hour was the driver going over the speed limit?: ");
mph = get.nextDouble();
}//end input
//=================================================================
public static double calculateFines(int violation, double fines, double salary, double mph) //calculates the amount of fines for either violation
{
{
if (violation == 1)
return (salary * 1.75);
else if (violation == 2)
return (salary * 1.2) + (mph * .5 * salary);
else if (violation == 3)
return (salary * 1.75) + ((salary * 1.2) + (mph * .5 * salary));
return 0;
}
}//end calculateFines
//=================================================================
public static void disp(String officer, String driver, double salary, int violation, double fines) //Display Method
{
System.out.println ("Your name is Officer " + officer + "\nThe name of the driver you pulled over is: " + driver);
System.out.println ( String.format( "The driver's annual salary is $%.2f", + salary) );
{
if (violation == 1)
System.out.println("\nThe type of traffic violation the driver received: Red Light");
else if (violation == 2)
System.out.println("\nThe type of traffic violation the driver received: Speeding");
else if (violation == 3)
System.out.println("\nThe type of traffic violation the driver received: Red Light & Speeding");
}
System.out.println ( String.format( "The amount of fines that will be assessed is $%.2f", + fines) );
}//end disp
}//end class FinFines_
这是我的问题:
“你的名字是警官
你停车的司机的名字是:
司机的年薪为0.00美元
将评估的罚款金额为0.00美元“
如何确保程序保存我输入的值?
虽然让所有的
都是静态的并不是一个好的做法,但要让代码正常工作,请按以下方式更改所有方法-
public static void input(String officer, String driver, double salary, int violation) //Input Method {
到
到
到
这将确保在使用i定义中的相同参数调用这些方法时更新所有静态字段,现在您的main()
将包括
while (answer != 0) {
input();
fines = calculateFines(); //call calculateFines method
disp();
System.out.println("Would you like to write a new ticket? Press 1 for yes or 0 for no: ");
answer = get.nextInt();
}
注意-我已经缩短了文本并删除了注释,请务必将它们保留在代码中。虽然将所有内容都设置为静态
不是一个好的做法。但要使代码正常工作,请按以下方式更改所有方法-
public static void input(String officer, String driver, double salary, int violation) //Input Method {
到
到
到
这将确保在使用i定义中的相同参数调用这些方法时更新所有静态字段,现在您的main()
将包括
while (answer != 0) {
input();
fines = calculateFines(); //call calculateFines method
disp();
System.out.println("Would you like to write a new ticket? Press 1 for yes or 0 for no: ");
answer = get.nextInt();
}
注意-我已经缩短了文本并删除了注释,请将它们保存在代码中。我注意到在calculateFines()
方法中,您将值乘以1.75。这将得到原始值的175%。要找到1.75%,您需要乘以0.0175。这也适用于所有其他值
希望这有帮助我注意到在calculateFines()
方法中,您将值乘以1.75。这将得到原始值的175%。要找到1.75%,您需要乘以0.0175。这也适用于所有其他值
希望这有帮助
您需要阅读关于新行字符的nextLine()
、nextInt()
和nextDouble()
行为。如果其中任何一个在输入缓冲区中留下了换行字符,那么下面的一个将看到该字符,并且不会使用任何进一步的输入。这就是您看到的
要解决此问题,您需要从扫描仪
中读取换行符并忽略它。您应该查看以找到适当的方法来执行此操作
这与#1是同一个问题
只要if
语句说明了违规
的所有可能值,此返回很可能没有问题。事实上,这是一个非常好的解决方案,因为它将立即让您看到违规
的无效值。您还应该了解允许您定义fi的枚举变量的选择数
您需要阅读关于新行字符的nextLine()
、nextInt()
和nextDouble()
行为。如果其中任何一个在输入缓冲区中留下了换行字符,那么下面的一个将看到该字符,并且不会使用任何进一步的输入。这就是您看到的
要解决此问题,您需要从扫描仪
中读取换行符并忽略它。您应该查看以找到适当的方法来执行此操作
这与#1是同一个问题
只要if
语句说明了违规
的所有可能值,此返回很可能没有问题。事实上,这是一个非常好的解决方案,因为它将立即让您看到违规
的无效值。您还应该了解允许您定义fi的枚举变量的选择数
这就解决了我的问题#1,谢谢!为什么一个人永远都不能成功
public static void disp(String officer, String driver, double salary, int violation, double fines) //Display Method {
public static void disp() {
while (answer != 0) {
input();
fines = calculateFines(); //call calculateFines method
disp();
System.out.println("Would you like to write a new ticket? Press 1 for yes or 0 for no: ");
answer = get.nextInt();
}