奇怪的JAVA编译错误?
我需要先说明我不允许在课堂上使用IDE,我必须使用TextPad来编译和运行。任何帮助都将不胜感激 有三个文件,一个驱动程序文件和另外两个类文件。Lab4文件是驱动程序文件,类文件是ITEM和NAME 编译驱动程序Lab4文件时,出现以下错误 Lab4文件的代码如下:奇怪的JAVA编译错误?,java,Java,我需要先说明我不允许在课堂上使用IDE,我必须使用TextPad来编译和运行。任何帮助都将不胜感激 有三个文件,一个驱动程序文件和另外两个类文件。Lab4文件是驱动程序文件,类文件是ITEM和NAME 编译驱动程序Lab4文件时,出现以下错误 Lab4文件的代码如下: import java.util.Scanner; public class Lab4 { public static void main (String[] args) {
import java.util.Scanner;
public class Lab4
{
public static void main (String[] args)
{
String firstName = "";
String lastName = "";
String manName = "";
float cost = 0.00f;
int itemNum = 0;
// Instantiate class files
// Variables are declared because variables are expected in name.java file
Name name = new Name("Bob", "Jones");
Item item = new Item(985534, 9.99f, "Lewis Mfg");
// Create a scanner
Scanner input = new Scanner(System.in);
// Recieve user input for variables
System.out.println ("Please enter the customer's first name: ");
firstName = input.next();
while (firstName.length() < 1)
{
System.out.println ("Enter a valid first name");
firstName = input.next();
}
name.setfirstName(firstName);
firstName = "";
System.out.println ("Please enter the customer's last name: ");
lastName = input.next();
while (lastName.length() < 1)
{
System.out.println ("Enter a valid last name: ");
lastName = input.next();
}
name.setlastName(lastName);
lastName = "";
System.out.println ("Please enter the item number: ");
itemNum = input.nextInt();
while (itemNum < 1)
{
System.out.println ("Enter a valid item number: ");
itemNum = input.nextInt();
}
item.setitemNum(itemNum);
itemNum = 0;
System.out.println ("Please enter the item's cost (including decimal): ");
cost = input.nextFloat();
while (cost < 1)
{
System.out.println ("Enter a valid cost amount: ");
cost = input.nextFloat();
}
item.setcost(cost);
cost = 0;
System.out.println ("Please enter the manufacturer's name: ");
manName = input.next();
while (manName.length() < 1)
{
System.out.println ("Enter a valid manufacturer's name");
manName = input.next();
}
item.setmanName(manName);
manName = "";
// Outputs the data entered by the user and stored in the other classes, Name and Item
System.out.println ("First Name: " + name.getfirstName() + "\n");
System.out.println ("Last Name: " + name.getlastName() + "\n");
System.out.println ("Item Number: " + item.getitemNum() + "\n");
System.out.println ("Item Cost: " + item.getcost() + "\n");
System.out.println ("Manufacturer's Name: " + item.manName());
System.out.println ("\n\nThe customer and item information have been entered.\n");
}
}
错误中引用的项目文件的代码如下:
public class Item
{
// Create private variables that cannot be accessed directly
private int itemNum;
private float cost;
private String manName;
// Create instances of the private variables that can be set by the methods
public Item (int itemNum, float cost, String manName)
{
this.itemNum = itemNum;
this.cost = cost;
this.manName = manName;
}
// Gets the variable values from the user via the driver class
public int getitemNum()
{
return itemNum;
}
public float getcost()
{
return cost;
}
public String getmanName()
{
return manName;
}
// Sets the variable amounts into the private variables after validating the input form
public boolean setitemNum(int itemNum)
{
if (itemNum < 0)
return false;
else
this.itemNum = itemNum;
return true;
}
public boolean setcost(float cost)
{
if (cost < 0)
return false;
else
this.cost = cost;
return true;
}
public boolean setmanName(String manName)
{
if (manName.length() < 0)
return false;
else
this.manName = manName;
return true;
}
}
任何帮助都会很棒 您将方法命名为getmanName,但在错误行中调用了manName。您将方法命名为getmanName,但在错误行中调用了manName。您遇到了简单的打字错误:
您需要item.getmanName而不是manName您遇到了简单的打字错误: 您需要item.getmanName而不是manNamemanName是一个属性,您将其作为方法manName引用 由于manName是私有的,因此必须使用调用getmanName的访问器 给定代码的正确参考是item.getmanName 该方法更好的惯用名称是getManName,它遵循适当的Java大写命名约定。manName是一个属性,您将其作为方法manName引用 由于manName是私有的,因此必须使用调用getmanName的访问器 给定代码的正确参考是item.getmanName
该方法更好的惯用名称是getManName,遵循Java的正确大写命名约定。您正在调用manName,但您的方法是getManName您正在调用manName,但您的方法是getManName据我所知,Item类中没有manName方法。Getter方法称为getmanName。通常,有一条规则告诉我们getter方法应该以get单词开始,然后您应该放置希望获取其值的类属性名称 更改导致编译错误的行:
System.out.println ("Manufacturer's Name: " + item.getmanName());
现在它应该可以工作了。据我所知,Item类中没有manName方法。Getter方法称为getmanName。通常,有一条规则告诉我们getter方法应该以get单词开始,然后您应该放置希望获取其值的类属性名称 更改导致编译错误的行:
System.out.println ("Manufacturer's Name: " + item.getmanName());
现在应该可以了。您可能想要item.getmanName,而不是item.manName。您可能想要item.getmanName,而不是item.manName。