此代码应该在输入对话框中接收全名字符串示例“Billy Bob Smith”,并在消息对话框中将首字母作为字母组合示例“BBS”输出。但由于某种原因,主要方法不会让我访问 fullName 变量。
import javax.swing.*;
public class HardMonogram {
//---------- ATTRIBUTES ----------//
private String fullName;
private String monogram;
private String first;
private String middle;
private String last;
//---------- METHODS ----------//
public String getInitial(String seperateName) {
return seperateName.substring(0, 1);
}
public void getSeperateName(String fullName) {
first = fullName.substring(0, fullName.indexOf(" "));
middle = fullName.substring(fullName.indexOf(" ") + 1, fullName.length());
last = middle.substring(middle.indexOf(" ") + 1, middle.length());
middle = middle.substring(0, middle.indexOf(" "));
}
public void setMonogram() {
monogram = getInitial(first) +
getInitial(middle) +
getInitial(last);
JOptionPane.showMessageDialog(null, monogram);
}
public static void main(String[] args) {
myMono.fullName = JOptionPane.showInputDialog(null, "Type in you full name");
HardMonogram myMono = new HardMonogram();
myMono.getSeperateName(myMono.fullName);
myMono.setMonogram();
}
}
给了我这个构建错误
/Users/aaron/School/Fall 2012/CSCI-C 201/Labs/LB08/HardMonogram.java:33: error: cannot find symbol
myMono.fullName = JOptionPane.showInputDialog(null, "Type in you full name");
^
symbol: variable myMono
location: class HardMonogram
1 error
[Finished in 1.2s with exit code 1]
这是我对 java 类的介绍,但我不知道为什么我不能访问该变量。我显然忽略了一些东西。有任何想法吗?