gpt4 book ai didi

java - 如何更改主类中不同类的变量值?

转载 作者:行者123 更新时间:2023-12-01 23:39:19 25 4
gpt4 key购买 nike

我正在编写一个小游戏来娱乐,但在将一个类中的变量从另一个类中的值更改时遇到问题。实际上,我在另一个不是主要类(class)的类(class)中已经完成了此操作。我在运行时遇到的错误是我需要 main()main(String[] args) ,我将其更改为 main(int wgold).

import java.io.*;


class player {
public static void main (String[] args) throws IOException { // <<<<<<<<<<<<<<<<<<<<< Need to have this get the value sent
String Name, cmd;
int gold, quit, spaces, weint, randGold;

gold = 0;
cmd = "none";
quit = 0;
spaces = 0;
weint = 0;

/* Ideas for new commands:
attack - Attacks monster you have run into. - 100%
eat - Heals you. - 0%
run - Gets you away from a battle. - 100%
walk - Moves you one space and you can: find something (gold, food, ect...) or run into a monster. - 50%
*/

while (quit == 0) {
gold += wgold; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< This is the setting of the gold after the battle >>>>>>>>>>>>>>>>>>>>>>>
wgold = 0;
if (cmd.equalsIgnoreCase("none")) {
System.out.println("-------Commands-------\n");
System.out.println("distance - Displays the amount of spaces you have walked.");
System.out.println("gold - Shows the amount of gold you have.");
System.out.println("quit - Exits the program.");
System.out.println("walk - Moves you one space.\n");
System.out.println("--End of command list--\n");
cmd = "wait";
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
System.out.print("Command: ");
System.out.flush();
cmd = stdin.readLine();
}

if (cmd.equalsIgnoreCase("gold")) {
System.out.println("\nGold: " + gold + "\n");
cmd = "none";
}else if (cmd.equalsIgnoreCase("walk")) {
spaces += 1;
weint = misc.random(1000); // Sets a random number out of 1000 for a percentage.
if ((weint >= 0) & (weint < 300)) {
enemies obj = new enemies();
obj.main();
cmd = "none";
} else if ((weint >= 300) & (weint < 500)) {
randGold = misc.random(10); // Sets the random amount of gold found while walking.
if (randGold <= 0) {
randGold = 1;
}
System.out.println("\nYou have walked one space and found " + randGold + " gold piece(s).\n");
gold += randGold;
cmd = "none";
} else if ((weint >= 500) & (weint <= 1000)) {
System.out.println("\nYou have walked one space.\n");
cmd = "none";
}
} else if (cmd.equalsIgnoreCase("distance")) {
System.out.println("\nSpaces you have moved: " + spaces + ".\n");
cmd = "none";
} else if (cmd.equalsIgnoreCase("quit")) {
System.out.println("Quitting the program...");
quit = 1;
} else {
System.out.println("\nCommand not recognized.\n");
cmd = "none";
}
}
}
}

class battle {
public static void main(String monster, int level, int mhp, int drop) throws IOException { // (String monster) gets the return value of the monster sent by the enemies class.
int end, hp, str, smhp, exp;
Long hit;
String cmd;

end = 0;
hp = 10;
str = 1;
exp = 0;
smhp = mhp;
cmd = "none";

System.out.println("\n\n\n-------Battle!-------\n\n\n");
System.out.println("You've encountered a wild " + monster + " level " + level + "!\n\n");

while(end == 0) {
if(cmd.equalsIgnoreCase("none")) {
System.out.println("----Commands----\n");
System.out.println("attack - Attacks the enemy.");
System.out.println("run - Runs from battle.\n");
System.out.println("--End of command list--\n\n\n");
System.out.println("-----Status----\n");
System.out.println("HP: " + hp + "/10");
System.out.println("Strength: " + str + "\n");
System.out.println("---Wild " + monster + " stats---");
System.out.println("Wild " + monster + " HP: " + mhp + "/" + smhp + "\n");
cmd = "wait";

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Command: ");
System.out.flush();
cmd = stdin.readLine();
}

if(cmd.equalsIgnoreCase("run")) {
System.out.println("\nYou have escaped from the battle!\n");
end = 1;
} else if(cmd.equalsIgnoreCase("attack")) {
hit = Math.round(misc.random(str) * .7);

System.out.println("You hit: " + hit);
mhp -= hit;

if(hit >= 1) {
exp += hit * 12;
}

cmd = "none";
//Add HP for monsters / attack command. Not all here but you know where...
} else {
System.out.println("\nCommand not recognized.\n");
cmd = "none";
}

if(mhp <= 0) {
System.out.println("You have won!");
System.out.println("You found: " + drop + " coins!");

player obj = new player(); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< This is where the value is sent to the player class
obj.main(drop);

if(exp >= str * 80) {
str += 1;
System.out.println("Congradulations! You now have " + str + " strength.");
}
end = 1;
}

}
}
}

class enemies {
public static void main() throws IOException {
String monster;
int drop, level, mint, mhp;

mint = misc.random(10);

if (mint >= 7) {
monster = "Goblin";
} else {
monster = "Wizard";
}

battle obj = new battle(); // calls the battle class

level = misc.random(3); // Makes a random level for the enemy.

mhp = level + 3;

drop = misc.random(level);

if(drop <= 0) {
drop = 1;
}

drop = drop * 3;

obj.main(monster, level, mhp, drop); // sends the String monster to the battle class.
}
}

我知道我的代码很困惑,可能有很多事情我可以做不同的事情,但我现在只是在学习。任何帮助将不胜感激。

此代码也可以在这里找到:http://pastebin.com/kjuD3GJd

最佳答案

您需要对 Main 类的实例的引用。以下代码 scetch 中的一些内容(将包含拼写错误)

public class Main{
public static void main (String[] args){
new Main().start();
}
public void start(){
new OtherClass(this).doSomething();
}

public void whatever(){...}
}

public class OtherClass{
private final Main main;
public OtherClass(Main aMain){
main = aMain;
}
public void doSomething{
main.whatever();
}
}

whatever 方法中,您可以访问 Main 实例的所有状态。当然你也可以传递参数。

关于您的其他隐式问题:要启动程序,您必须使用static void main(String[] args)方法。其他任何事情都行不通。当然,您可以使用任意其他方法,包括一些具有相同名称的方法,尽管我建议初学者使用唯一的名称。这可以避免编译器让您对它所提示的方法感到困惑。

作为附加的一些关于在这里提问的建议:将您的代码复制(*)到一个新项目。然后删除所有不必要的内容来理解你的问题。在十分之九的情况下,您会在此过程中找到问题的解决方案。在最后一种情况下,您有一个人们实际正在查看的代码示例,从而得到更好的答案。

(*) 而不是复制使用版本控制系统,例如推荐,但我猜你还没到那一步。

关于java - 如何更改主类中不同类的变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18201085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com