-2

So, I'm quite new to Java. I've learned quite a bit for being new. But...of course I don't understand everything.

I have 2 classes. One named "Random" and one name "ananas" (Ananas is French for Pineapple)

Random is my main class...but for some reason my main class (Random) is not detecting ananas.

Here is my script in ananas:

public class ananas {
    public String a(String PackageA){
        PackageA = "This file shall remain TOP SECRET! The ultimate universal secret code is...'Ananas'"; 
        return PackageA;
    }
    public String b(String PackageB){
        PackageB = "File not created yet";
        return PackageB;
    }
    public String c(String PackageC){
        PackageC = "File not created  yet";
        return PackageC;
    }

}

And here is my code in "Random":

import java.util.Scanner;
public class Random {
    public static void main(String ars[]){
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome, Please enter the code: "); 
        String hey = input.nextLine();
            if(hey .equals("The sandman ate my dollar"))

                System.out.println("Welcome! Please choose one: A), B), C)");
                    Scanner input2 = new Scanner(System.in);
                    String heyy = input2.nextLine();
                    if(heyy .equals("A)"))
                        System.out.println("File  not created yet");

                    else if(heyy .equals("B)"))
                        System.out.println("Flid not created yet");

                    else if(heyy .equals("C)"))
                        System.out.println("File not created yet");
                    else 
                        System.out.println("Access Denied"); 

I tried to do: "ananas abc = new ananas();"

But even when I go to run my code, it only detects "Random"

Please help?

4

2 回答 2

1

如果这就是您在 Random 中拥有的所有代码,那么您永远不会构建 ananas 的实例。由于您在 Ananas 中的方法不是静态的,因此您需要创建该类的一个实例。

Ananas a = new Ananas(); // Construct new instance calling the default constructor

// Note that you have named your methods so that nobody can really understand what they do!
// Now, to call methods from this class, you would do it like this
//First a = the instance of ananas class we just built. The second a is the method in the class we wish to call. String is the parameter the method requires.
a.a(string);

看起来您想根据用户提供的输入从类 Ananas 调用方法,您可以修改代码以执行此操作。

if(heyy.equals("A)"){
      a.a(yourString); // You need to create the ananas instance before this, and have a string called yourString that you pass on to the method
}

在这种情况下,更好的解决方案是不要求 ananas 中的方法需要 String 参数。还要考虑命名该方法,以便它描述它在做什么!所需的更改就像这样简单:

public String a(){ // a could be something like 'getStringA'
        String PackageA = "This file shall remain TOP SECRET! The ultimate universal secret code is...'Ananas'"; 
        return PackageA;
    }
于 2012-05-26T14:00:39.527 回答
0

我在凤梨中没有看到构造函数。

它应该有类似的东西:

public ananas(){
System.out.println("I like pineapples");
}

我希望这有帮助 :)

于 2012-05-26T13:56:22.633 回答