Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单的eclipseidejava程序_Java - Fatal编程技术网

简单的eclipseidejava程序

简单的eclipseidejava程序,java,Java,将完整的Java程序编码为以下内容: 如果违反红灯,则罚款为司机年薪的1.75% 如果是超速驾驶,则罚款为年薪的1.2%,再加上每超过限速1英里/小时工资的0.5% 你需要做什么 提示用户/官员输入驾驶员的全名 提示官员输入其全名 提示用户/官员输入驾驶员的年薪 提示警官输入违规类型(红灯1例,超速2例) 如果超速,则提示用户输入超出限速的英里/小时数 使用一套“如果……否则”评估应评估多少罚款,然后打印一份完整的报告,包括: 官员姓名 司机姓名 年薪 违规类型(你需要说明的不仅仅是1或

将完整的Java程序编码为以下内容:

  • 如果违反红灯,则罚款为司机年薪的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美元“
    如何确保程序保存我输入的值?

  • 当我按1键继续时,当它询问官员的名字时,它跳过了第一行。它不允许我输入变量的名称。 如何确保我的程序不会跳过这一行?

  • 我的“return 0;”语句有问题吗?如果我没有包含该语句,那么我的程序将无法运行,但我担心包含它会使我的程序将所有值都设为0


  • 虽然让所有的
    都是静态的
    并不是一个好的做法,但要让代码正常工作,请按以下方式更改所有方法-

    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();
    }