使用 Java 中的其他 类 从 Main 方法更改变量?
Change variables from Main method using oither classes in Java?
我正在制作剪刀石头布游戏。我想使用其他 类 更改 Main 方法中的变量。我该怎么做?这是我的代码。
主要方法
import java.util.Scanner;
public class RPSApp
{
public static void main(String[] args)
{
int userThrow, cpuThrow;
final int ROCK = 1, PAPER = 2, SCISSORS = 3;
String rockName = "Rock", paperName = "Paper", scissorsName = "Scissors";
String nameOfGame = "Rock, Paper, Scissors";
String userThrowEng, cpuThrowEng;
int outcome;
int langSel = 1;
String userInput;
boolean keepPlaying = true;
Scanner input = new Scanner(System.in);
Random generator = new Random();
LanguageHandler language = new LanguageHandler();
GameHandler game = new GameHandler();
System.out.println("Select Language");
System.out.println();
System.out.println("(1) US\n(2) UK");
System.out.println();
System.out.print(">");
langSel = input.nextInt();
if (langSel == 2)
{
language.setBritish();
}
System.out.println("Welcome to " + nameOfGame + "!");
LanguageHandler.java
public class LanguageHandler
{
public void setBritish ()
{
nameOfGame = "Paper, Scissors, Stone";
rockName = "Stone";
}
}
然而,这不起作用。我该如何让它发挥作用?
谢谢!
Java代码
public class TestProgram {
public static void main(String[] args) throws FileNotFoundException {
{
int userThrow, cpuThrow;
final int ROCK = 1, PAPER = 2, SCISSORS = 3;
String rockName = "Rock", paperName = "Paper", scissorsName = "Scissors";
String nameOfGame = "Rock, Paper, Scissors";
String userThrowEng, cpuThrowEng;
int outcome;
int langSel = 1;
String userInput;
boolean keepPlaying = true;
Scanner input = new Scanner(System.in);
Random generator = new Random();
LanguageHandler language = new LanguageHandler();
System.out.println("Select Language");
System.out.println();
System.out.println("(1) US\n(2) UK");
System.out.println();
System.out.print(">");
langSel = input.nextInt();
if (langSel == 2) {
nameOfGame=language.getNameOfGame();
}
System.out.println("Welcome to " + nameOfGame + "!");
}
}
}
public class LanguageHandler {
String nameOfGame = "Paper, Scissors, Stone";
String rockName = "Stone";
public String getNameOfGame() {
return nameOfGame;
}
public void setNameOfGame(String nameOfGame) {
this.nameOfGame = nameOfGame;
}
public String getRockName() {
return rockName;
}
public void setRockName(String rockName) {
this.rockName = rockName;
}
}
我正在制作剪刀石头布游戏。我想使用其他 类 更改 Main 方法中的变量。我该怎么做?这是我的代码。
主要方法
import java.util.Scanner;
public class RPSApp
{
public static void main(String[] args)
{
int userThrow, cpuThrow;
final int ROCK = 1, PAPER = 2, SCISSORS = 3;
String rockName = "Rock", paperName = "Paper", scissorsName = "Scissors";
String nameOfGame = "Rock, Paper, Scissors";
String userThrowEng, cpuThrowEng;
int outcome;
int langSel = 1;
String userInput;
boolean keepPlaying = true;
Scanner input = new Scanner(System.in);
Random generator = new Random();
LanguageHandler language = new LanguageHandler();
GameHandler game = new GameHandler();
System.out.println("Select Language");
System.out.println();
System.out.println("(1) US\n(2) UK");
System.out.println();
System.out.print(">");
langSel = input.nextInt();
if (langSel == 2)
{
language.setBritish();
}
System.out.println("Welcome to " + nameOfGame + "!");
LanguageHandler.java
public class LanguageHandler
{
public void setBritish ()
{
nameOfGame = "Paper, Scissors, Stone";
rockName = "Stone";
}
}
然而,这不起作用。我该如何让它发挥作用?
谢谢!
Java代码
public class TestProgram {
public static void main(String[] args) throws FileNotFoundException {
{
int userThrow, cpuThrow;
final int ROCK = 1, PAPER = 2, SCISSORS = 3;
String rockName = "Rock", paperName = "Paper", scissorsName = "Scissors";
String nameOfGame = "Rock, Paper, Scissors";
String userThrowEng, cpuThrowEng;
int outcome;
int langSel = 1;
String userInput;
boolean keepPlaying = true;
Scanner input = new Scanner(System.in);
Random generator = new Random();
LanguageHandler language = new LanguageHandler();
System.out.println("Select Language");
System.out.println();
System.out.println("(1) US\n(2) UK");
System.out.println();
System.out.print(">");
langSel = input.nextInt();
if (langSel == 2) {
nameOfGame=language.getNameOfGame();
}
System.out.println("Welcome to " + nameOfGame + "!");
}
}
}
public class LanguageHandler {
String nameOfGame = "Paper, Scissors, Stone";
String rockName = "Stone";
public String getNameOfGame() {
return nameOfGame;
}
public void setNameOfGame(String nameOfGame) {
this.nameOfGame = nameOfGame;
}
public String getRockName() {
return rockName;
}
public void setRockName(String rockName) {
this.rockName = rockName;
}
}