我遇到了一个我必须为课程制作的java程序的问题。我要把两个字母转换成一个位置。然后我接收一条消息并使用基本字符交换对其进行加密。我似乎遇到一个问题,即我的驱动程序文件中的调用无法看到我试图在我的Actions文件中调用的方法。我确定我错过了一些简单而做错的事情但是我阅读的文档越多,我就越困惑。有人可以向我解释我做错了什么。我试图从我的驱动程序文件中调用我的动作文件中的getCountry方法。错误在
行locH = loc.getCountry(locationLetters);
Driver.java
/**
Program Name: Action
Date:4/14/2016
Program Description: This program is going to handle the window where the user enters data.
It is also going to be what is going to call the methods of the Actions class
Methods: Driver(),
*/
import javax.swing.*; // For the Swing classes
import java.awt.event.*; // For the ActionListener Interface
import java.util.Scanner; //for the keyboard
public class Driver extends JFrame
{
//delcare
private String locationLetters; //this is going hold the users letter selection
private String message; //this is going to hold the users message
private String locH; //for the holder of location selection
private String messH; //to hold the message before change it to an array
Scanner keyboard = new Scanner (System.in);//to make the keyboard
/**
This method is the constuctor that is going to make the window for the program to use.
*/
public Driver()
{
System.out.println("sup nerd");
yourmom();
}//end of Driver()
public String yourmom()
{
System.out.println("Please entner the two charater key for the location you want to message");
locationLetters = keyboard.next();
System.out.println("Please enter the message you would like to have encrypted.");
message = keyboard.next();
locH = loc.getCountry(locationLetters);
return locH;
}//end of yourmom()
public static void main(String[] arg)
{
Actions loc = new Actions();
Actions mess = new Actions();
new Driver();
}//end of main
}//end of Driver class
Actions.java
/**
Program Name: Action
Date:4/14/2016
Program Description: This program is going to handle all the encryption actions as well
loction where the message is being sent.
Methods:Location(),
*/
public class Actions
{
//decare
public String messE; //this is for the message that is going to be ecrypted
public String getCountry(String locHA)
{
locHA.toUpperCase();
if(locHA == "FR")
locHA = "France";
if(locHA == "GB")
locHA = "Great Britain";
if(locHA == "CA")
locHA = "Canada";
if(locHA == "JA")
locHA = "Japan";
if(locHA == "RU")
locHA = "Russia";
if(locHA == "GE")
locHA = "Germany";
if(locHA == "AU")
locHA = "Australia";
if(locHA == "MX")
locHA = "Mexico";
return locHA;
}
}//end of action class
我知道这可以在一个文件中完成,但我的老师想要一两个。我知道我遗漏了一些简单但我不理解我在阅读有关使用对象的文档。你能指出我做错了什么吗?我将非常感激。谢谢。
答案 0 :(得分:0)
更改主要内容,以便将重要数据传递到Driver:
public static void main(String[] arg) {
Actions loc = new Actions();
Actions mess = new Actions();
new Driver(loc, mess);
}
然后在主构造函数中使用那些Actions:
public Driver(Actions loc, Actions mess) {
// use parameters to set fields so that the parameter references can be
// used elsewhere in the class
this.loc = loc;
this.mess = mess;
System.out.println("sup nerd");
yourmom(loc); // and pass references where needed
}
然后类似地使用yourmom()方法中的loc引用
另外,请勿使用==
或!=
比较字符串。请改用equals(...)
或equalsIgnoreCase(...)
方法。理解==
检查两个对象引用是否相同而不是您感兴趣的内容。另一方面,这些方法检查两个字符串是否具有相同的字符。同样的顺序,这就是重要的事情。而不是
if(locHA == "FR")
locHA = "France";
DO
// always use curly braces too!
if("FR".equals(locHA)) {
locHA = "France";
}
或
// if you want to allow more liberal capitalization
if("FR".equalsIgnoreCase(locHA)) {
locHA = "France";
}
答案 1 :(得分:0)
放
Actions.getCountry();
在你的主要课程中。 你可以把公众排除在第二类之外,因为它不容易获得。