import java.util.Scanner;
public class CarTest {
/**
* @param args
*/
public static void main(String[] args) {
//create a Scanner object
Scanner input = new Scanner(System.in);
/**
*Creates a car! object from the Car class
*with the passing of these arguments
*year, make, model, price
*/
Car car1 = new Car("2008", "Nissan", "Pathfinder", "10,000");
//display toString using car1 object
System.out.println(car1.toString());
//user input for year
System.out.println("Please list your automobile for sale: " +
"\nPlease enter the year? ");
//String newYear variable is created for CarTest class
String newYear = input.nexLine();
//set year in car class for Car1 object to String newYear
car1.setYear(newYear);
//user input for make
System.out.println("Please enter the make? ");
//String newMake variable is created for CarTest class
String newMake = input.nextLine();
//set make in car class for Car1 object to String newMake
car1.setMake(newMake);
//user input for model
System.out.println("Please Enter the model?");
//String newModel variable is created for CarTest class
String newModel = input.nextLine();
//set model in car class for Car1 object to String newModel
car1.setModel(newModel);
//user input for price
System.out.println("How much would you sell your car for?");
//String newPrice variable is created for CarTest class
String newPrice = input.nextLine();
//set price in car class for Car1 object to String newPrice
car1.setPrice(newPrice);
//displays the new info to the screen
System.out.println(car1.toString());
}
}
来自
public class Car {
//class variables are created
private String year;
private String make;
private String model;
private String price;
//default constructor
public Car() {
}
//constructor with arguments
public Car(String year, String make, String model, String price) {
this.year = year;
this.model = model;
this.make = make;
this.price = price;
}
//get make of the vehicle
public String getMake() {
return make;
}
//set make for vehicle
public void setMake(String make) {
this.make = make;
}
//get model of the vehicle
public String getModel(){
return model;
}
//set model for vehicle
public void setModel(String model) {
this.model = model;
}
//get price of the vehicle
public String getPrice() {
return price;
}
//set price for vehicle
public void setPrice(String price) {
this.price = price;
}
//get year of the vehicle
public String getYear() {
return year;
}
//set year for vehicle
public void setYear(String year) {
this.year = year;
}
public String toString() {
return "For Sale By Owner: " + year + " " + make + " " + model +
"\nSelling Price: $" + price + "\n ";
}
}
当我上周递交作业时,它工作正常,但是今天,当我来到我的电脑上完成新的作业时,我在我的car.java
和{{1}上找到了红色的x }}
我的日食说:来自carTest.java
的错误充满了:
carTest.java
错误是我定义CAR1的方式:
error: main method not found in class homework.cartest. please define the main method
public static void main(String[] args)
以及我获得输入的方式
Car car1 = new Car("2008", "Nissan", "Pathfinder", "10,000");
将String newYear = input.nexLine();
String newMake = input.nextLine();
String newModel = input.nextLine();
String newPrice = input.nextLine();
加下划线为eclipse中的代码中的错误。
也在input.nextLine();
。方法Car.java
会导致错误说明String toSTring()
当我递给它并获得好成绩时,它工作得很好,但今天我注意到我的代码是错误的,昨天一切都好吗?
答案 0 :(得分:0)
清理并构建项目。可能是您的Java构建路径的异常
答案 1 :(得分:0)
文件是否列在包/项目作业下?
如果没有,请正确阅读您的错误消息,您正在尝试运行不存在的内容。
与此同时,如上所述,您可能遇到了文件路径问题。创建一个新项目并复制粘贴代码(重构的副本)通常是最快的解决方案。
答案 2 :(得分:0)
复制您的项目并运行它。唯一有问题的是:
String newYear = input.nexLine();
应该是:
String newYear = input.nextLine();
其余的,它运作正常。
在toString
方法中,您应添加@override
注释
@Override
public String toString() {
return "For Sale By Owner: " + year + " " + make + " " + model
+ "\nSelling Price: $" + price + "\n ";
}
还要确保您的项目名为CarTest
,源包package cartest