我需要幫助以在代碼中查找錯誤並修復它。 下面是代碼:查看此代碼
import java.util.*;
public class Main
{
public static void main(String[] args) {
Random r = new Random();
Scanner s = new Scanner(System.in);
Fighter f1 = new Fighter();
Fighter f2 = new Fighter();
System.out.println("Enter the name of the first fighter:");
String str=s.nextLine();
f1.name=str.substring(0,1).toUpperCase()+str.substring(1).toLowerCase();
System.out.println("Choose the style:\n1/ Boxing\n2/ Kick Boxing\n3/ MMA");
int style1 = s.nextInt();
switch (style1){
case 1 :f1 =new Boxer();
break;
case 2 :f1 = new Kickeboxer();
break;
case 3 :f1 = new MMA_Fighter();
break;}
System.out.println("Enter the name of the seconde fighter:");
String str1=s.nextLine();
f2.name=str1;
System.out.println("Choose the style:\n1/ Boxing\n2/ Kick Boxing\n3/ MMA");
int style2 = s.nextInt();
switch (style2){
case 1 :f2 = new Boxer();
break;
case 2 :f2 = new Kickeboxer();
break;
case 3 :f2 = new MMA_Fighter();
break;}
for (int rnd=1;rnd <=5;rnd++){
f1.punch=r.nextInt(f1.power);
f2.punch=r.nextInt(f2.power);
f1.hp-=f2.punch;
f2.hp-=f1.punch;
if (f1.hp <= 0){
System.out.println("Round "+rnd+" result:\n K.O !");
break;
}else if (f2.hp <= 0){
System.out.println("Round "+rnd+" result:\n K.O !");
break;
}else{
System.out.println("Round "+rnd+" result:\n"+f1.name+"'s health : "+f1.getHp()+"\n"+f2.name+"'s health : "+f2.getHp()+"\n **********");}
if (f1.hp>f2.hp){
System.out.println(f1.name+" wins !");
}else if(f1.hp<f2.hp){
System.out.println(f2.name+" wins !");
}else {System.out.println("It's a DRAW !");}
}//end for
}}
class Fighter {
String name;
int hp;
int power;
int punch;
public int getHp() {
return hp;
}
}
class Boxer extends Fighter{
int hp = 100;
int power = 20;
}
class Kickeboxer extends Fighter{
int hp =80;
int power = 40;
}
class MMA_Fighter extends Fighter{
int hp = 70;
int power = 50;
}
當執行它,它給了我一個錯誤
Enter the name of the first fighter: Choose the style: 1/ Boxing 2/ Kick Boxing 3/ MMA Enter the name of the seconde fighter: Choose the style: 1/ Boxing 2/ Kick Boxing 3/ MMA Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at Main.main(Main.java:26)
感謝您的幫助:)
歡迎來到Stack Overflow!它看起來像你需要學習使用調試器。請幫助您自己完成一些互補的調試技術。 –
@IvanPronin我想你忘了互補調試技術... – shmosel
你試過[調試你的程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) ? –