我正在开发一个程序,该程序可以根据用户输入的值创建第4版DnD字符,我想知道在我继续之前是否有更简单的方法来进行我的工作。我正在研究的程序的当前部分是让用户为其技能分配预选的值。我测试这部分的代码是:
import java.util.Scanner;
public class testCodeThree{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
standardArray(scan);
}
public static void standardArray(Scanner scan) {
System.out.println("----------------------------------------------------------");
System.out.println("Assign the numbers \"16\", \"14\", \"13\", \"12\", \"11\", and \"10\" to your skills.");
System.out.print("Enter your desired Str stat: ");
int userStr = scan.nextInt();
int strMod = 0;
int conMod = 0;
int dexMod = 0;
int intMod = 0;
int wisMod = 0;
int chaMod = 0;
if (userStr == 16 || userStr == 14 || userStr == 13 || userStr == 12 || userStr == 11 || userStr == 10) {
switch (userStr) {
case 16: // 16 Str, -- Con, -- Dex, -- Int, -- Wis, -- Cha
strMod = 3;
System.out.print("Enter your desired Con stat: ");
int userCon = scan.nextInt();
if (userCon == 14 || userCon == 13 || userCon == 12 || userCon == 11 || userCon == 10) {
switch (userCon) {
case 14: // 16 Str, 14 Con, -- Dex, -- Int, -- Wis, -- Cha
conMod = 2;
System.out.print("Enter your desired Dex stat: ");
int userDex = scan.nextInt();
if (userDex == 13 || userDex == 12 || userDex == 11 || userDex == 10) {
switch (userDex) {
case 13: // 16 Str, 14 Con, 13 Dex, -- Int, -- Wis, -- Cha
dexMod = 1;
System.out.print("Enter your desired Int stat: ");
int userInt = scan.nextInt();
if (userInt == 12 || userInt == 11 || userInt == 10) {
switch (userInt) {
case 12: // 16 Str, 14 Con, 13 Dex, 12 Int, -- Wis, -- Cha
intMod = 1;
System.out.print("Enter your desired Wis stat: ");
int userWis = scan.nextInt();
if (userWis == 11 || userWis == 10) {
switch (userWis) {
case 11: // 16 Str, 14 Con, 13 Dex, 12 Int, 11 Wis, 10 Cha
int userCha = 10;
System.out.println("Your Charisma stat is "+ userCha);
break;
default: // 16 Str, 14 Con, 13 Dex, 12 Int, 10 Wis, 11 Cha
userCha = 11;
System.out.println("Your Charisma stat is "+ userCha);
break;
}
} else {
System.out.println("----------------------------------------------------------");
System.out.println("Invalid input. Please try again.");
standardArray(scan);
}
break;
case 11:
break;
default:
break;
}
} else {
System.out.println("----------------------------------------------------------");
System.out.println("Invalid input. Please try again.");
standardArray(scan);
}
break;
case 12:
dexMod = 1;
break;
case 11:
// dexMod does not get updated here because the
// modifier is zero.
break;
default:
// dexMod does not get updated here because the
// modifier is zero.
break;
}
} else {
System.out.println("----------------------------------------------------------");
System.out.println("Invalid input. Please try again.");
standardArray(scan);
}
break;
case 13:
conMod = 1;
break;
case 12:
conMod = 1;
break;
case 11:
// conMod does not get updated here because the modifier
// is zero.
break;
default:
// conMod does not get updated here because the modifier
// is zero.
break;
}
} else {
System.out.println("----------------------------------------------------------");
System.out.println("Invalid input. Please try again.");
standardArray(scan);
}
break;
case 14:
strMod = 2;
break;
case 13:
strMod = 1;
break;
case 12:
strMod = 1;
break;
case 11:
// strMod does not get updated here because the modifier is
// zero.
break;
default:
// strMod does not get updated here because the modifier is
// zero.
break;
}
} else {
System.out.println("----------------------------------------------------------");
System.out.println("Invalid input. Please try again.");
standardArray(scan);
}
}
}
我最初的想法是要求用户输入他们想要的Str状态值,然后使用嵌套的if语句/ switch语句来确定他们为以下状态输入的内容。我不想这样做的原因是,我必须考虑输入的数字的每个可能顺序(16、14、13、12、11、10或16,13、14、11、10、12等等),总共有很多if和case语句。有没有一种方法可以减少行数?
编辑:用户不能输入相同的数字以使用一种以上的技能。
如有问题,请随时提问。
最佳答案
是的,有很多方法可以做到这一点。
这是一种方法,即使用Set来查看值是否唯一,并使用函数来避免重复很多代码。
private static boolean isValidStat(int userStat) {
return userStat >= 10 && userStat <= 16 && userStat != 15;
}
private static int readStat(Scanner scan, String stat, Set<Integer> seenStats) {
System.out.println("Enter your desired "+stat+" stat: ");
while(true) {
int userStat = scan.nextInt();
if(!isValidStat(userStat))
System.out.println("Not a valid choice - please try again:");
else if(seenStats.contains(userStat))
System.out.println("Already used that number - please try again:");
else {
seenStats.add(userStat);
return userStat;
}
}
}
private static int getMod(int userStat) {
switch(userStat) {
case 16: return 3;
case 14: return 2;
case 13: return 1;
case 12: return 1;
case 11: return 0;
case 10: return 0;
default: throw new RuntimeException("invalid userStat "+userStat); // shouldn't happen
}
}
public static void standardArray(Scanner scan) {
System.out.println("----------------------------------------------------------");
System.out.println("Assign the numbers \"16\", \"14\", \"13\", \"12\", \"11\", and \"10\" to your skills.");
Set<Integer> seenStats = new HashSet<Integer>();
int userStr = readStat(scan, "Str", seenStats);
int userCon = readStat(scan, "Con", seenStats);
int userDex = readStat(scan, "Dex", seenStats);
int userInt = readStat(scan, "Int", seenStats);
int userWis = readStat(scan, "Wis", seenStats);
int userCha = readStat(scan, "Cha", seenStats);
int strMod = getMod(userStr);
int conMod = getMod(userCon);
int dexMod = getMod(userDex);
int intMod = getMod(userInt);
int wisMod = getMod(userWis);
int chaMod = getMod(userCha);
/* do something with all these numbers, of course */
}