[英]How can I use a Variable from a Class Runner to use in a Method in another Class? (BlueJ, Java)
I'm taking a coding class using BlueJ, and decided to surprise my teacher with a Text Adventure that uses a Class Runner and another class to have the methods to call. 我正在使用BlueJ上编码课,并决定通过Text Adventure使我的老师感到惊讶,Text Adventure使用Class Runner和另一个类来调用方法。 The problem is, I don't know how to use a variable that I established in the Runner, into a method in the Method Class, which I will then call into the Runner.
问题是,我不知道如何将在Runner中建立的变量用于Method类中的方法,然后将其调用到Runner中。 Here is my code:
这是我的代码:
(This is the runner) (这是跑步者)
import java.util.*;
public class TextAdventureRunner
{
public static void main (String[]Args)
{
TextAdventureCode run = new TextAdventureCode();
Scanner kb = new Scanner(System.in);
String x = "";
System.out.print("Enter Your Name: : ");
x = kb.nextLine();
System.out.println(x);
run.Hi();
run.HiTwo();
}
}
(This is the code that contains the methods) (这是包含方法的代码)
import java.util.*;
public class TextAdventureCode extends TextAdventureRunner
{
Scanner kb = new Scanner(System.in);
public static void Hi()
{
System.out.println("Hi" + x);
}
public static void HiTwo()
{
System.out.println("");
}
}
You see, In my method Hi(), there is an error where the x should be. 您会看到,在我的方法Hi()中,x应该存在一个错误。 the error reads "cannot find symbol - variable x" even though i extended the class and declared an object in the other class... any help?
即使我扩展了类并在另一个类中声明了一个对象,该错误也会显示“找不到符号-变量x”……有什么帮助吗?
You can declare your TextAdventureCode like this: 您可以这样声明您的TextAdventureCode:
import java.util.*;
public class TextAdventureCode extends TextAdventureRunner
{
Scanner kb = new Scanner(System.in);
public static void Hi(String x) //Modification
{
System.out.println("Hi" + x);
}
public static void HiTwo()
{
System.out.println("");
}
}
And declare TextAdventureRunner like this: 并这样声明TextAdventureRunner:
import java.util.*;
public class TextAdventureRunner
{
public static void main (String[]Args)
{
TextAdventureCode run = new TextAdventureCode();
Scanner kb = new Scanner(System.in);
String x = "";
System.out.print("Enter Your Name: : ");
x = kb.nextLine();
System.out.println(x);
run.Hi(x); // Modification
run.HiTwo();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.