私のコードでエラーを見つけて修正するための助けが必要です。このコードをチェックしてください
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)
感謝:)
ようこそ!デバッガの使い方を学ぶ必要があるようです。いくつかの相補的なデバッグ手法をお手伝いしてください。 –
@IvanProninあなたは補完的なデバッグテクニックを忘れてしまったと思います... – shmosel
[あなたのプログラムをデバッグしようとしましたか](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) ? –