英文:
Type Mismatch: Cant resolve string to int
问题
public static void battle() {
Scanner b = new Scanner(System.in); // 收集数据
System.out.println("请输入你的攻击力:");
int attack = b.nextInt();
System.out.println("请输入你的防御力:");
int defence = b.nextInt();
System.out.println("请输入你的基础数值:");
int base = b.nextInt();
System.out.println("请输入你的属性加成数值:");
int stab = b.nextInt();
System.out.println("请输入你的生命值:");
int hp = b.nextInt();
System.out.println("请输入你的等级:");
int level = b.nextInt();
// 打印收集到的数据
System.out.println("=============================================================");
System.out.println("你正在与超梦战斗!\n 你的属性:");
System.out.println("\n等级:" + level + "\n攻击力:" + attack + "\n防御力:" + defence + "\n基础数值:" + base + "\n属性加成数值:" + stab + "\n生命值:" + hp);
System.out.println("=============================================================");
}
我已经独自学习了大约两周的Java。我正尝试完成一个编程练习,在这个练习中,计算机会询问用户输入宝可梦的属性,并将属性列表打印回给用户。我在一些愚蠢的语法错误上纠结了,虽然问题应该很明显,但我现在却无法想起来。
英文:
public static void battle() {
Scanner b = new Scanner(System.in); //collect data
System.out.println("What is your Attack? ");
int attack = b.next();
System.out.println("What is your Defence? ");
int defence = b.next();
System.out.println("What is your Base? ");
int base = b.next();
System.out.println("What is your STAB? ");
int stab = b.next();
System.out.println("What is your HP? ");
int hp = b.next();
System.out.println("What is your Level?");
int level = b.next();
//print data colected
System.out.println("=============================================================");
System.out.println("You are fighting mew!\n Your stats:");
System.out.println("\nLevel: " + level + "\nAttack: " + attack + "\nDefence: " + defence + "\nBase: " + base + "\nStab:" + stab + "\nHP: " + hp);
System.out.println("=============================================================");
}
I've been learning java solo for about 2 weeks. I'm trying to do this one programming worksheet where the computer asks the user for Pokemon stats input, then prints the stat list back to them. I'm making some stupid syntax error and it should be obvious but I'm spacing on it.
答案1
得分: 1
使用 nextInt
如果你查阅Javadoc
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()
你会看到 next
返回一个 String
Scanner b = new Scanner(System.in); // 收集数据
System.out.println("你的攻击力是多少?");
int attack = b.nextInt();
System.out.println("你的防御力是多少?");
int defence = b.nextInt();
System.out.println("你的基础力量是多少?");
int base = b.nextInt();
System.out.println("你的属性加成是多少?");
int stab = b.nextInt();
System.out.println("你的生命值是多少?");
int hp = b.nextInt();
System.out.println("你的等级是多少?");
int level = b.nextInt();
// 打印收集到的数据
System.out.println("=============================================================");
System.out.println("你正在与超梦战斗!\n 你的属性:");
System.out.println("\n等级: " + level + "\n攻击力: " + attack + "\n防御力: " + defence + "\n基础力量: " + base + "\n属性加成: " + stab + "\n生命值: " + hp);
System.out.println("=============================================================");
英文:
Use nextInt
If you check the javadocs
https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()
you will seee next
returns an String
Scanner b = new Scanner(System.in); //collect data
System.out.println("What is your Attack? ");
int attack = b.nextInt();
System.out.println("What is your Defence? ");
int defence = b.nextInt();
System.out.println("What is your Base? ");
int base = b.nextInt();
System.out.println("What is your STAB? ");
int stab = b.nextInt();
System.out.println("What is your HP? ");
int hp = b.nextInt();
System.out.println("What is your Level?");
int level = b.nextInt();
//print data colected
System.out.println("=============================================================");
System.out.println("You are fighting mew!\n Your stats:");
System.out.println("\nLevel: " + level + "\nAttack: " + attack + "\nDefence: " + defence + "\nBase: " + base + "\nStab:" + stab + "\nHP: " + hp);
System.out.println("=============================================================");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论