测试 1 上的运行时错误(代码强制),但在 netbeans 中工作



我是代码强制的新手,当我在 NetBeans 中运行此代码时,它可以正常工作,但是当我在代码强制下提交它时,它会在测试 1 上出现运行时错误。 有什么问题? 这就是问题所在

import java.util.Scanner;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String first = input.nextLine();
String second = input.nextLine();
input.close();
String first_line[] = first.split(" ");
String second_line[] = second.split(" ");
int first_numbers[] = new int[first_line.length];
int second_numbers[] = new int[second_line.length];
for (int i = 0; i < 2; i++) {
if (Integer.parseInt(first_line[i]) >= 1)
first_numbers[i] = Integer.parseInt(first_line[i]);
else
first_numbers[i] = 0;
}
for (int i = 0; i < second_line.length; i++)
if (Integer.parseInt(second_line[i]) >= 1)
second_numbers[i] = Integer.parseInt(second_line[i]);
else
second_numbers[i] = 0;
int x = 0;
try {
for (int i = 0; first_numbers[1] < second_numbers[i]; i++)
x++;
} catch (Exception ex) {
}
System.out.println(x);
}

如果你修改你的最后一个 for 循环,如下所示怎么办

int x = 0;
for (int i=0; i<second_numbers.length; i++) {
if (first_numbers[1] < second_numbers[i])
x++;
}

为什么你拆分字符串int那些

String first_line[] = first.split(" ");
String second_line[] = second.split(" ");
int first_numbers[] = new int[first_line.length];
int second_numbers[] = new int[second_line.length];

您可以从头开始初始化 int 并像下面这样使用它 2 int

Scanner input = new Scanner(System.in);
int k = input.nextInt();
int n = input.nextInt();
int cout = 0;
int[] arr = new int[k];

在这里,您是我从 2014 年开始解决此问题的完整解决方案

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();
int counter = 0, tk = 0;
int a[] = new int[n];
for(int i = 0; i < n; ++i){
a[i] = input.nextInt();
if((i + 1) == k){
tk = a[i];
}
}
for(int i = 0; i < n; ++i){
if(a[i] > 0 && a[i] >= tk){
counter++;
}
}
System.out.println(counter);
}

我希望我能帮上忙

如果我能给你建议 在开始时,尝试在这些问题中尽可能简化。

相关内容

最新更新