有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

另一个文件中的java方法不可见

我有一个java程序的问题,我必须使类。我要把两个字母转换成一个位置。然后,我将获取一条消息,并使用基本字符交换对其进行加密。我似乎有一个问题,我的驱动程序文件中的调用无法看到我试图在操作文件中调用的方法。我肯定我遗漏了一些简单的东西,而且做得不对,但是我读的文档越多,我就越困惑。有人能给我解释一下我做错了什么吗。我正在尝试从驱动程序文件调用操作文件中的getCountry方法。错误就在这条线上

locH = loc.getCountry(locationLetters); 

司机。爪哇

/**
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

行动。爪哇

/**
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 

我知道这可以在一个文件中完成,但我的老师想要在两个文件中完成。我知道我遗漏了一些简单的东西,但我不理解我一直在阅读的关于使用对象的文档。你能指出我做错了什么吗?我将非常感激。多谢各位


共 (2) 个答案

  1. # 1 楼答案

    更改main,以便将重要数据传递到驱动程序:

    public static void main(String[] arg) {
        Actions loc = new Actions();
        Actions mess = new Actions();
        new Driver(loc, mess);
    }
    

    然后在主构造函数中使用这些操作:

    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";
    

    // 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";
    }
    
  2. # 2 楼答案

    Actions.getCountry();
    

    在你的主课上。 你可以把公众带出二等舱,因为二等舱不那么容易到达