我是Java的新手,在Eclipse上的一个简单程序[处于保留状态]收到许多奇怪的错误

问题描述 投票:-6回答:1

[突然,我以为我正在“完成”这个简单的程序,所以我尝试在许多小时内第一次运行它,但遇到了各种各样的错误。它曾经在没有声明的构造函数和只有几个方法的情况下运行。现在什么都没有了。而且我不明白什么是错的,现在看来这都是错的。

import java.util.Scanner;

public class Car {
    private static final String String = null;

    public static void main(String[] args) {

        String plate;
        int time;
        double velocity;
        double acceleration;

        //default constructor
        Car(){
            plate = "AAAA 111";
            time = 10;
            velocity = 98;
            calcAcceleration();
        );              

        public Car (String plate, int time, double velocity) {
            //  initial constructor
            plate = "ABCD 123";
            time = 10;
            velocity = 50.0;
            calcAcceleration();
        }

        // copy constructor
        public Car (Car car) {
        };


        Car defaultCar=new Car();
        System.out.print("Default car attributes are: ");
        System.out.print("Plate is " + defaultCar.plate + " with time of " + defaultCar.time + ", velocity of " + defaultCar.velocity);
        System.out.println(" and acceleration of ");
        defaultCar.calcAcceleration());
        System.out.println(acceleration);

        Car initialCar=new Car("ABCD 123",10,50);
        System.out.print("Initial car attributes are: ");
        System.out.print("Plate is " + defaultCar.plate + " with time of " + defaultCar.time + ", velocity of " + defaultCar.velocity);
        System.out.println(" and acceleration of ");
        defaultCar.calcAcceleration());
        System.out.println(acceleration);

        Car copyCar=new Car(initialCar);
        System.out.print("Copied car attributes are: ");
        System.out.print("Plate is " + defaultCar.plate + " with time of " + defaultCar.time + ", velocity of " + defaultCar.velocity);
        System.out.println(" and acceleration of ");
        defaultCar.calcAcceleration());
        System.out.println(acceleration);

        // change values based on user input
        System.out.println("\nUpdate car attributes: ");
        initialCar.inputPlate();
        initialCar.inputTime();
        initialCar.inputVelocity();
        System.out.print("Updated car attributes are: ");
        initialCar.displayCar();
        System.out.println(" and acceleration of ");
        initialCar.calcAcceleration();
    }

    private static inputPlate() {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the plate: ");
        plate = input.nextLine();
        }

    private static void inputTime() {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the time: ");
        time = input.nextInt();
        }

    private static void inputVelocity() {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the velocity: ");
        velocity = input.nextDouble();
        }

    private static void displayCar() {
        System.out.println( "Plate is " + plate + " with time of " + time + ", velocity of " + velocity );
        }

    private static void calcAcceleration() {
        acceleration = velocity / time;
        } // end of main

} // end of class

java
1个回答
-1
投票

检查花括号,它们非常混乱(您的复制构造函数代码在您的复制构造函数方法之外,您的最后一个方法没有右括号,等等。]

并且附带说明,您不应将字符串称为String,因为它被认为是不正确的做法。

private static final String String = null; //call it str instead for example
© www.soinside.com 2019 - 2024. All rights reserved.