gpt4 book ai didi

Java错误: Could not find or load main class undefined

转载 作者:行者123 更新时间:2023-12-01 10:56:07 24 4
gpt4 key购买 nike

问题如下。我正在为一个名为“myprogramminglab”的在线编程网站做作业。它要求我做以下练习:

Design a class named Person with fields for holding a person's name , address, and telephone number (all as Strings ). Write a constructor that initializes all of these values, and mutator and accessor methods for every field.

Next, design a class named Customer, which inherits from the Person class . The Customer class should have a String field for the customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write a constructor that initializes these values and the appropriate mutator and accessor methods for the class 's fields.

Demonstrate the Customer class in a program that prompts the user to enter values for the customer's name, address, phone number, and customer number, and then asks the user whether or not the customer wants to receive mail. Use this information to create a customer object and then print its information.

Put all of your classes in the same file. To do this, do not declare them public.

Instead, simply write:

class  Person { ... }
class Customer { ... }

但是,我首先在 Eclipse 中编写了该程序。我将每个类放在一个单独的文件中,就像您通常所做的那样。完成此操作后,我将每个类(class)移至我应该上传的网站。我根据问题删除了“public”关键字,以便我能够上传它。当我这样做时,它不会给我任何结果。就像它甚至不运行该程序一样。它可以编译,但什么也不做。我查了一个在线java编译器,提示如下:

错误:无法找到或加载未定义的主类

可能是什么错误,我应该如何修复它?

这是我的代码:

import java.util.Scanner;
class Person {

private String name;
private String address;
private String number;

public Person(String name, String address, String number) {
super();
this.name = name;
this.address = address;
this.number = number;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}

}


class Customer extends Person {

private String num;
private boolean wants;

// TODO Auto-generated constructor stub
public Customer(String name, String address, String number, String num, boolean wants) {
super(name, address, number);
this.num = num;
this.wants = wants;
}

public String getNum() {
return num;
}

public void setNum(String num) {
this.num = num;
}

public boolean isWants() {
return wants;
}

public void setWants(boolean wants) {
this.wants = wants;
}

}


class demo {

public static void main(String[] args) {

String name, address, number;
String num;
String decide;
boolean wants;

Scanner get = new Scanner(System.in);

System.out.println("Enter name of customer: ");
name = get.nextLine();
System.out.println("Enter address of customer: ");
address = get.nextLine();
System.out.println("Enter phone number of customer: ");
number = get.nextLine();
System.out.println("Enter customer number: ");
num = get.nextLine();
System.out.println("Enter yes/no -- does the customer want to recieve·mail?: ");
decide = get.nextLine();
if (decide.equals("yes"))
wants = true;
else
wants = false;

Customer one = new Customer(name, address, number, num, wants);
System.out.println("Customer: ");
System.out.println("Name: " + one.getName());
System.out.println("Address: " + one.getAddress());
System.out.println("Phone Number: " + one.getNumber());
System.out.println("Receive Mail?: " + one.isWants());
}
}

最佳答案

您需要有一个公共(public)类,其中包含公共(public)静态主方法(在您的情况下为演示)。文件名应该与公共(public)类相同(在您的例子中是 demo.java),其他类可以保持这样(它们实际上具有包默认访问权限)。

正如其他人也指出的那样,您最好遵循命名约定将类名也更改为 Demo。

关于Java错误: Could not find or load main class undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33602025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com