我需要在前面加上我不允许在课堂上使用IDE,我必须使用TextPad进行编译和运行。任何帮助将不胜感激。
这里有两个文件,一个名为Account,另一个名为Lab4B。分配是创建和使用以下文件: Lab4B.java =分配的驱动程序代码类(Lab4B)的源代码 Account.java = Account类的源代码
说明如下: 设计一个名为Account的类(包含
)我收到以下错误: 错误是在我编译“Lab4B”文件时。
F:\Java\Lab 4b\Lab4B.java:13: error: cannot find symbol
Account account = new Account(5544, 45000, 0.036);
^
symbol: class Account
location: class Lab4B
F:\Java\Lab 4b\Lab4B.java:13: error: cannot find symbol
Account account = new Account(5544, 45000, 0.036);
^
symbol: class Account
location: class Lab4B
2 errors
“帐户”的代码如下:
import java.util.Scanner;
import java.util.Date;
public class Account
{
int id = 0;
double balance = 0;
double annualInterestRate = 0;
Date dateCreated;
public Account()
{
}
public Account(int id, double balance, double annualInterestRate)
{
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
this.dateCreated = new Date();
}
public void setID (int id)
{
this.id = id;
}
public int getID()
{
return this.id;
}
public void setBalance (double balance)
{
this.balance = balance;
}
public double getbalance()
{
return this.balance;
}
public void setAnnualInterestrate (double annualInterestRate)
{
this.annualInterestRate = annualInterestRate;
}
public double getAnnualInterestrate()
{
return this.annualInterestRate;
}
public Date getDateCreated()
{
return this.dateCreated;
}
public double getMonthlyInterestRate()
{
return (this.annualInterestRate) / 12;
}
public void withdraw(double amount)
{
this.balance -= amount;
System.out.println ("After withdrawing $"+amount +" the balance in your account is: $ " +this.balance +"\n");
}
public void deposit(double amount)
{
this.balance += amount;
System.out.println(" After making a deposit of $"+amount +" the balance in your account is: $" +this.balance + "\n");
}
}
Lab4B文件的代码如下:
public class Lab4B
{
public static void main(String[] args)
{
// Creates an instance of Account
Account account = new Account(5544, 45000, 0.036);
// Sets default amounts
account.setID(5544);
account.setBalance(45000);
account.setAnnualInterestRate(0.036);
account.withdraw(3700);
account.deposit(2200);
// Prints output
System.out.println ("Account number: \n"+ +account.getID());
System.out.println ("The account balance is: $ \n" + +account.getbalance());
System.out.println ("The monthly interest earned is: $ \n" + +account.getbalance() * account.annualInterestRate());
System.out.println ("The account was created on: \n" + +account.getdateCreated());
}
}
以下是我从javac命令行编译器获得的错误:
C:\>javac Lab4B.java
Lab4B.java:5: error: package mypackage does not exist
import mypackage.Account;
^
Lab4B.java:15: error: cannot find symbol
Account account = new Account(5544, 45000, 0.036);
^
symbol: class Account
location: class Lab4B
Lab4B.java:15: error: cannot find symbol
Account account = new Account(5544, 45000, 0.036);
^
symbol: class Account
location: class Lab4B
3 errors
这是javac -verbose输出:
C:\Users\kschultz>cd\
C:\>javac -verbose Lab4B.java
[parsing started RegularFileObject[Lab4B.java]]
[parsing completed 12ms]
[search path for source files: .C:\Program Files (x86)\Java\jdk1.7.0_02\lib,C:\P
rogram Files (x86)\Java\jre6\lib\ext\QTJava.zip]
[search path for class files: C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\re
sources.jar,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\rt.jar,C:\Program Fi
les (x86)\Java\jdk1.7.0_02\jre\lib\sunrsasign.jar,C:\Program Files (x86)\Java\jd
k1.7.0_02\jre\lib\jsse.jar,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\jce.j
ar,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\charsets.jar,C:\Program Files
(x86)\Java\jdk1.7.0_02\jre\classes,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\
lib\ext\dnsns.jar,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\ext\localedata
.jar,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\ext\sunec.jar,C:\Program Fi
les (x86)\Java\jdk1.7.0_02\jre\lib\ext\sunjce_provider.jar,C:\Program Files (x86
)\Java\jdk1.7.0_02\jre\lib\ext\sunmscapi.jar,C:\Program Files (x86)\Java\jdk1.7.
0_02\jre\lib\ext\sunpkcs11.jar,C:\Program Files (x86)\Java\jdk1.7.0_02\jre\lib\e
xt\zipfs.jar,.C:\Program Files (x86)\Java\jdk1.7.0_02\lib,C:\Program Files (x86)
\Java\jre6\lib\ext\QTJava.zip]
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/lang/Object.class)]]
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/lang/String.class)]]
[checking Lab4B]
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/lang/AutoCloseable.class)]]
Lab4B.java:13: error: cannot find symbol
Account account = new Account(5544, 45000, 0.036);
^
symbol: class Account
location: class Lab4B
Lab4B.java:13: error: cannot find symbol
Account account = new Account(5544, 45000, 0.036);
^
symbol: class Account
location: class Lab4B
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/lang/System.class)]]
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/io/PrintStream.class)]]
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/io/FilterOutputStream.class)]]
[loading ZipFileIndexFileObject[C:\Program Files (x86)\Java\jdk1.7.0_02\lib\ct.s
ym(META-INF/sym/rt.jar/java/io/OutputStream.class)]]
[total 228ms]
2 errors
答案 0 :(得分:2)
您的两个错误都与同一问题有关。
Lab4B.java:5: error: package mypackage does not exist
Lab4B.java:15: error: cannot find symbol
<强>问题强>:
<强>解决方案强>:
包声明语法
package mypackage;
import java.util.Scanner;
import java.util.Date;
public class Account {
//rest of the code
声明顺序如下。
包导入语法
import mypackage.Account;
public class Lab4B
{
//rest of code
您可以通过3种方式指定包导入。
JOptionPane类位于swing包中,该包位于javax包中。通配符(*)用于指定程序可以使用该程序包的所有类。这是最常见的编程风格。
import javax.swing.*; // Make all classes visible altho only one is used.
class ImportTest {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Hi");
System.exit(0);
}
}
可以在导入时显式指定类,而不是使用通配符。
import javax.swing.JOptionPane; // Make a single class visible.
class ImportTest {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Hi");
System.exit(0);
}
}
或者,我们可以使用完全限定的类名而不导入。
class ImportTest {
public static void main(String[] args) {
javax.swing.JOptionPane.showMessageDialog(null, "Hi");
System.exit(0);
}
}
您可以阅读有关包here
的更多信息答案 1 :(得分:1)
当Java编译器编译Lab4B.java时,它希望能够找到(或编译)Account
类。编译器说它无法找到Account
类。
这个问题的几种可能性:
“Account.java”与“Lab4B.java”不在同一目录中 - 请注意,该名称区分大小写。如果文件名为“account.java”,则编译器将找不到Account
类。
“Account.java”不应与“Lab4B.java”位于同一目录中 - 如果是这种情况,则它位于不同的包中。您需要import
可以找到Account
类的包,或者在引用Account
类时需要指定完整的包名称}类。如果合适,您还需要确保Lab4B
配置正确。
根据编译器的CLASSPATH
输出进行更新:
问题看起来像是因为有些事情正在将Java编译器的源路径设置为不正确的值:
-verbose
问题是当前目录不在搜索路径中([search path for source files: .C:\Program Files (x86)\Java\jdk1.7.0_02\lib,C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip]
已与另一个没有分隔符的路径连接)。看起来你有一个错误的.
环境变量,它在类路径配置的CLASSPATH
部分之后没有分号。
修复此问题,编译器应该能够找到.
。然后,由于源文件中的拼写错误,你会得到一些编译器错误,这些错误应该很容易解决。
答案 2 :(得分:0)
您需要在Account
课程中导入Lab4B
课程。一些事情:
import mypackage.Account;
在任何其他代码之前将它放在Lab4B
类的顶部。
希望这有帮助。