所以我暂时没有使用Java,并且正在尝试codingame.com看看我还记得什么。 我正在做教程级别,它有以下代码:
import java.util.*;
import java.io.*;
import java.math.*;
/**
* The code below will read all the game information for you.
* On each game turn, information will be available on the standard input, you will be sent:
* -> the total number of visible enemies
* -> for each enemy, its name and distance from you
* The system will wait for you to write an enemy name on the standard output.
* Once you have designated a target:
* -> the cannon will shoot
* -> the enemies will move
* -> new info will be available for you to read on the standard input.
**/
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
// game loop
while (true) {
int count = in.nextInt(); // The number of current enemy ships within range
String[] enemy = new String[count];
int[] dist = new int[count];
String closeEnemy = "";
for (int i = 0; i < count; i++) {
enemy[i] = in.next(); // The name of this enemy
dist[i] = in.nextInt(); // The distance to your cannon of this enemy
}
for(int j = 0; j <= count - 1; j++){
if(dist[j] > dist[j+1] ){
closeEnemy = enemy[j];
}
else{
closeEnemy = enemy[j+2];
}
}
// Write an action using System.out.println()
// To debug: System.err.println("Debug messages...");
System.out.println(closeEnemy); // The name of the most threatening enemy (HotDroid is just one example)
}
}
}
我需要做的是假设计数是2,我需要通过比较来自不同敌人的dist
变量来检查哪个敌人更接近。
我做的是:
import java.util.*;
import java.io.*;
import java.math.*;
/**
* The code below will read all the game information for you.
* On each game turn, information will be available on the standard input, you will be sent:
* -> the total number of visible enemies
* -> for each enemy, its name and distance from you
* The system will wait for you to write an enemy name on the standard output.
* Once you have designated a target:
* -> the cannon will shoot
* -> the enemies will move
* -> new info will be available for you to read on the standard input.
**/
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
// game loop
while (true) {
int count = in.nextInt(); // The number of current enemy ships within range
String[] enemy = new String[count];
int[] dist = new int[count];
String closeEnemy = "";
for (int i = 0; i < count; i++) {
enemy[i] = in.next(); // The name of this enemy
dist[i] = in.nextInt(); // The distance to your cannon of this enemy
}
for(int j = 0; j <= count - 1; j++){
if(dist[j] > dist[j+1] ){
closeEnemy = enemy[j];
}
else{
closeEnemy = enemy[j+2];
}
}
// Write an action using System.out.println()
// To debug: System.err.println("Debug messages...");
System.out.println(closeEnemy); // The name of the most threatening enemy (HotDroid is just one example)
}
}
}
我试图让它适用于任何数量的敌人(对于用户会插入的任何计数),但是当我测试它时,它会给我以下错误:
Exception in thread "main" java.util.InputMismatchException
at Player.main on line 24
我检查了所有内容,但我无法找到错误,并希望得到一些帮助。
答案 0 :(得分:0)
当程序期待String
时,您可能正在输入double
或int
。以下输入无需抛出InputMismatchException
:
2
enemy1
1
enemy2
2
之后你会得到一些超出界限的数组,可以用以下方法修复:
for(int j = 0; j < count - 1; j++){
if(dist[j] > dist[j+1] ){
closeEnemy = enemy[j];
} else {
closeEnemy = enemy[j+1];
}
}