gpt4 book ai didi

java - 我的代码有什么问题(初学者)(Java)

转载 作者:行者123 更新时间:2023-12-01 07:57:36 25 4
gpt4 key购买 nike

我正在做一些 Java 只是为了练习,并决定做一个龙与地下城类型的代码。 (这只是角色统计数据的滚动)

我输入了所有内容,它一直说(角色)最大的统计数据是活力。帮忙?

import java.util.Random;
import java.util.Scanner;
public class DAndD {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Random generator = new Random();
System.out.println("Choose a name for your Dungeons & Dragons character.");
String character = input.next();
System.out.print("Ah, " + character + " has a nice ring to it.\nLet's roll for stats shall we?");
int str = generator.nextInt(21);
int dex = generator.nextInt(21);
int intel = generator.nextInt(21);
int wis = generator.nextInt(21);
int vit = generator.nextInt(21);

if (str > 0);
String trait = "strength";
int large = str;
if (dex > large);
trait = "dexterity";
large = dex;
if (intel > large);
trait = "intelligence";
large = intel;
if (wis > large);
trait = "wisdom";
large = wis;
if (vit > large);
trait = "vitality";
large = vit;


if(large == str){
System.out.println("\n" + character + "'s best stat is its strength!");
}
else if (large == dex){
System.out.println("\n" + character + "'s best stat is its dexterity!");
}
else if (large == intel){
System.out.println("\n" + character + "'s best stat is its intelligence!");
}
else if (large == wis){
System.out.println("\n" + character + "'s best stat is its wisdom!");
}
else if (large == vit){
System.out.println("\n" + character + "'s best stat is its vitality");
}
System.out.println("\nStrength: " + str + "\nDexterity:" + dex + "\nIntelligence: " + intel + "\nWisdom: " + wis + "\nVitality: " + vit);

}
}

所以,这就是目前的最终结果:

输出

Choose a name for your Dungeons & Dragons character.

*Toetar*

Ah, Toetar has a nice ring to it.

Let's roll for stats shall we?

Toetar's best stat is its vitality

Strength: 5

Dexterity: 19

Intelligence: 1

Wisdom: 6

Vitality: 4

最佳答案

if 语句后有分号 (;) (if (vit > large);),这会否定它们的功能。这就是为什么使用 {...}

很重要

我“假设”你的意思更像是......

String trait = "";
int large = -1;
if (str > 0) {
trait = "strength";
large = str;
} else if (dex > large) {
trait = "dexterity";
large = dex;
} else if (intel > large) {
trait = "intelligence";
large = intel;
} else if (wis > large) {
trait = "wisdom";
large = wis;
} else if (vit > large) {
trait = "vitality";
large = vit;
}

关于java - 我的代码有什么问题(初学者)(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28080998/

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