如何定义一个变量,以便在java中的while循环之外使用它?
所以我遇到的问题是,我在while循环中声明了一个数组能力1,然后试图在循环外的if语句中访问同一个数组。我不知道如何将其作为一个全局函数进行访问。(顺便说一句,我是Java新手,所以我不知道你能做的所有花哨的事情)。 谢谢如何定义一个变量,以便在java中的while循环之外使用它?,java,Java,所以我遇到的问题是,我在while循环中声明了一个数组能力1,然后试图在循环外的if语句中访问同一个数组。我不知道如何将其作为一个全局函数进行访问。(顺便说一句,我是Java新手,所以我不知道你能做的所有花哨的事情)。 谢谢 在循环之前初始化能力1数组。即,删除所有三个ability1=新字符串[3]分配和替换字符串[]能力1具有字符串[]能力1=新字符串[3]。 您的代码至少将开始编译。但是它确实有很多其他问题(例如,永无止境的(x= 1)Loop),因此考虑重构。在循环之前初始化 Absiu
在循环之前初始化
能力1
数组。即,删除所有三个ability1=新字符串[3]代码>分配和替换字符串[]能力1代码>具有字符串[]能力1=新字符串[3]代码>。
您的代码至少将开始编译。但是它确实有很多其他问题(例如,永无止境的<代码>(x= 1)Loop),因此考虑重构。在循环之前初始化 Absiult1数组。即,删除所有三个ability1=新字符串[3]代码>分配和替换字符串[]能力1代码>具有字符串[]能力1=新字符串[3]代码>。
您的代码至少将开始编译。但是它确实有很多其他问题(例如,永无止境的<代码>(x= 1)< /代码>循环),因此考虑重构。
实际上,在<<代码> > 循环时,您并没有声明<代码> ab<1/代码>。<代码> String []RESP=新字符串[3 ];resp[0]=“左”;resp[1]=“正确”;resp[2]=“笔直”代码>可以重新编写为:字符串[]resp={“左”、“右”、“直”}代码>不要编写大量代码,将其拆分为更小的逻辑单元(方法)使用方法…抱歉,但是构造while(condition){….;break;}
与if(condition){…}
完全等效。你永远不会重复任何,而在这里是不合适的。我强烈建议您在使用Java进行任何编程之前,都要先学习一下。实际上,您根本没有在而循环中声明能力1
。String[]resp=new String[3];resp[0]=“左”;resp[1]=“正确”;resp[2]=“笔直”代码>可以重新编写为:字符串[]resp={“左”、“右”、“直”}代码>不要编写大量代码,将其拆分为更小的逻辑单元(方法)使用方法…抱歉,但是构造while(condition){….;break;}
与if(condition){…}
完全等效。你永远不会重复任何,而在这里是不合适的。我强烈建议您在使用Java进行任何编程之前,先阅读本教程。
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
public class TextGame {
public static void main(String args[]) throws InterruptedException {
System.out.println('\u000C');
int x = 1;
int y = 1;
int money = 0;
int exp = 0;
int mobHealth = 50;
int playerHealth = 50;
int[] loot = new int[0];
String[] ability1;
//String ability2;
//String ability3;
String[] resp = new String[3];
resp[0] = "Left";
resp[1] = "Right";
resp[2] = "Straight";
String[] mob = new String[5];
mob[0] = "Kobold";
mob[1] = "Wolf";
mob[2] = "Orc";
mob[3] = "Pirate";
mob[4] = "Thief";
String[] clxss = new String[3];
clxss[0] = "Warrior";
clxss[1] = "Hunter";
clxss[2] = "Mage";
String[] mobloot = new String[5];
mobloot[0] = "13 Gold";
mobloot[1] = "28 Gold";
mobloot[2] = "Iron Shortsword";
mobloot[3] = "Linen Cloth";
mobloot[4] = "15 Gold";
System.out.println("Enter Character Name: ");
Scanner name = new Scanner(System.in);
String username = name.nextLine();
System.out.println("Character " + username + " created");
System.out.println("\n");
System.out.println("Enter A Class: ");
System.out.println("\n");
System.out.println(Arrays.toString(clxss));
Scanner se = new Scanner(System.in);
String clyss = se.nextLine();
System.out.println("Class " + clyss + " selected");
System.out.println("\n");
System.out.println("Loading Game...");
TimeUnit.SECONDS.sleep(3);
System.out.println('\u000C');
while (x == 1) {
System.out.println("You are in a forest");
System.out.println("\n");
System.out.println("Go Left, Right, or Straight.");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
Random rand = new Random();
int mobs = rand.nextInt(5);
System.out.println("You encounter a " + mob[rand.nextInt(4) + 1] + "!");
while (input.equals("Left") || input.equals("Right") || input.equals("Straight")) {
System.out.println("Fight or Run!");
Scanner sd = new Scanner(System.in);
String encounter = sd.nextLine();
if (encounter.equals("Fight")) {
System.out.println("Encounter Started!");
break;
} else if (encounter.equals("Run")) {
System.out.println("Encounter Started Anyway!");
break;
} else {
System.out.println("Enter A Valid Command!");
continue;
}
}
System.out.println("Enter Your Attack Method!");
while (clyss.equals("Warrior")) {
ability1 = new String[3];
ability1[0] = "Slam";
ability1[1] = "Charge";
ability1[2] = "Cleave";
System.out.println(Arrays.toString(ability1));
break;
}
while (clyss.equals("Hunter")) {
ability1 = new String[3];
ability1[0] = "Arcane Shot";
ability1[1] = "Steady Shot";
ability1[2] = "Aimed Shot";
System.out.println(Arrays.toString(ability1));
break;
}
while (clyss.equals("Mage")) {
ability1 = new String[3];
ability1[0] = "Firebolt";
ability1[1] = "Frostbolt";
ability1[2] = "Arcanebolt";
System.out.println(Arrays.toString(ability1));
break;
}
break;
}
Scanner tak = new Scanner(System.in);
String attack = tak.nextLine();
if (attack == ability1[0]) {
System.out.println("You cast " + ability1[0] + "!");
} else if (attack == ability1[1]) {
System.out.println("You cast " + ability1[1] + "!");
} else if (attack == ability1[2]) {
System.out.println("You cast " + ability1[2] + "!");
}
}
}