我一直在尝试提交代码,但每次都会收到运行时错误。我不能指出我的代码的问题。代码在我的电脑上运行良好,当我尝试提交它时,它只显示运行时错误。
我是用IntelliJ编写的。
import java.util.Scanner;
class practice2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = 0;
do {
t = input.nextInt(); // number of test cases
} while (t < 1 || t > 100);
int n = 0; // variable to store length of the string
int k = 0; // variable to store the goodness number
String s = "";
for (int i = 0; i < t; i++) {
do {
n = input.nextInt(); // string length
} while (n < 1 || n > 100);
do {
k = input.nextInt(); // goodness number
} while (k < 0 || k > (n / 2));
input.nextLine(); // clearing buffer
do {
s = input.nextLine();
} while (s.length() != n);
s = s.toUpperCase(); // in uppercase
int minOp = checkGoodness(s, k, n);
System.out.println("case #" + (i + 1) + ": " + minOp);
}
}
public static int checkGoodness(String s, int k, int n) {
char[] sArr = new char[s.length()];
sArr = s.toCharArray();
int score = 0; int minOp = 0;
for (int i = 0; i < sArr.length / 2; i++) {
if (sArr[i] != sArr[sArr.length - i - 1]) {
score++;
}
}
if ( score == k)
minOp = 0;
else
minOp = Math.abs(score - k);
return minOp;
}
}
发布于 2021-05-10 14:12:19
将您的类设置为公共类,并将其名称从"practice2“更改为"Solution”。
发布于 2021-05-10 18:45:37
提交给在线评委最常见的运行时错误是主类的名称不正确。您应该检查Java的要求,并查看应该为主类使用什么名称。将"practice2“改为它,它应该可以工作。
https://stackoverflow.com/questions/67465462
复制相似问题