[英]How do I fix my java program not stopping running?
I am writing a simple program as some practice for Java.我正在编写一个简单的程序作为 Java 的一些练习。 It takes in integers and puts them into a two-dimensional array of R rows and C columns and then simply prints out each element of the array (just for troubleshooting).
它接收整数并将它们放入由 R 行和 C 列组成的二维数组中,然后简单地打印出数组的每个元素(仅用于故障排除)。 When I run the code it prints every integer like it should but the program does not stop running.
当我运行代码时,它会打印每个 integer ,但程序不会停止运行。 I have to force stop it.
我必须强行阻止它。 Why is it not stopping on its own?
为什么它不自行停止? I tried some basic debugging and tried to see if I accidentally had an infinite loop in the code but I couldn't find it.
我尝试了一些基本的调试,并试图查看我是否不小心在代码中出现了无限循环,但我找不到它。
In case it is important I am using IntelliJ Idea Ultimate 2019.3.万一很重要,我正在使用 IntelliJ Idea Ultimate 2019.3。
Thanks谢谢
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt(); //Takes number of test cases
for(int i = 1; i <= T; i++){ //This is the counter for each test case
int R = in.nextInt(); //R is # of rows
int C = in.nextInt(); //C is # of columns
int K = in.nextInt(); //K is the thickness allowed
int numArray[][] = new int[R+1][C+1];
for(int j = 0; j < R; j++){
for(int k = 0; k < C; k++){
numArray[j][k] = in.nextInt();
System.out.println(numArray[j][k]);
}
}
}
in.close();
}//End of main
}//End of main class
Edit: Just for future reference I will include the input and the output I was getting.编辑:仅供将来参考,我将包括输入和我得到的 output。
Input: 3 1 4 0 3 1 3 3 2 3 0 4 4 5 7 6 6 4 5 0 2 2 4 4 20 8 3 3 3 12 6 6 3 3 3 1 6 8 6 4输入: 3 1 4 0 3 1 3 3 2 3 0 4 4 5 7 6 6 4 5 0 2 2 4 4 20 8 3 3 3 12 6 6 3 3 3 1 6 8 6 4
Output: 3 1 3 3 4 4 5 7 6 6 2 2 4 4 20 8 3 3 3 12 6 6 3 3 3 1 6 8 6 (and here the cursor is stuck blinking not doing anything and the program doesn't quit on its own) Output: 3 1 3 3 4 4 5 7 6 6 2 2 4 4 20 8 3 3 3 12 6 6 3 3 3 1 6 8 6 (这里的 Z1791A97A8403730EE0760489A2AEB 没有做任何事情并且卡在程序上并没有闪烁)它自己的)
I solved the issue by manually typing the input instead of copying and pasting it.我通过手动输入输入而不是复制和粘贴来解决了这个问题。 Not sure what caused that issue but it worked!
不知道是什么导致了这个问题,但它有效!
This little piece of code requires the user to input 3 integers ( R
, C
and K
) T
-times:这段小代码需要用户输入 3 个整数(
R
、 C
和K
) T
次:
int T = in.nextInt();
for(int i = 1; i <= T; i++){
int R = in.nextInt();
int C = in.nextInt();
int K = in.nextInt();
// ...
}
Additionally the user has to input an integer R*C
-times until i <= T
:此外,用户必须输入 integer
R*C
-times 直到i <= T
:
for(int j = 0; j < R; j++){
for(int k = 0; k < C; k++){
numArray[j][k] = in.nextInt(); // << requires user-interaction each loop here >>
System.out.println(numArray[j][k]);
}
}
If you use T
=1, R
=1, C
=1, K
=1 the user has to input only 1 additional integer, as there is only 1 test-repitition and R*C
= 1. With that your program terminates just as expected. If you use
T
=1, R
=1, C
=1, K
=1 the user has to input only 1 additional integer, as there is only 1 test-repitition and R*C
= 1. With that your program terminates just as预期的。
With bigger values of T
, R
, C
, K
the required user-interactions grow exponentially, which probably makes you think that your program should have ended but it still waits for additional user-input.随着
T
、 R
、 C
、 K
的值越大,所需的用户交互呈指数增长,这可能会让您认为您的程序应该已经结束,但它仍在等待额外的用户输入。
Ok I think I figured out the issue.好的,我想我解决了这个问题。 I was copying and pasting the test inputs from a web page.
我正在从 web 页面复制和粘贴测试输入。 I tried double checking to make sure the input was valid and then inputting them by hand and it fixed it.
我尝试仔细检查以确保输入有效,然后手动输入并修复它。 Not 100% why but that fixes the problem.
不是 100% 为什么,但这可以解决问题。 Thanks for the help!
谢谢您的帮助!
your for(int i = 1; i <= T; i++) probably newer ends because you are increasing T.您的 for(int i = 1; i <= T; i++) 可能较新的结尾,因为您正在增加 T。
You need to made some condition in loop to exit.您需要在循环中设置一些条件才能退出。 For example if (xx == yy) break;
例如 if (xx == yy) break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.