我正在尝试在控制台中输入“ END”时终止代码,但是我的代码无法正常运行,而且我似乎看不到哪里出了问题,顺便说一句,到目前为止我还没有学会如何调试这很大程度上与为什么我不知道这一点有关。如果可以的话,请提供帮助。
import java.util.*;
class Main
{
public static void main(String args[])
{
int j = 0;
while (j++ <= 3) {
// Create Scanner object to take input from command prompt
Scanner s=new Scanner(System.in);
// Take input from the user and store it in st
String st = "";
while (!st.equals("END")) {
{
st=s.nextLine();
// Initialize the variable count to 0
int count=0;
// Convert String st to char array
char[] c=st.toCharArray();
// Loop till end of string
for(int i=0;i<st.length();i++)
// If character at index 'i' is not a space, then increment count
if(c[i]!=' ') count++;
// Print no.of spaces
System.out.printf("[%4d] spaces in ", +(st.length()-count));
// Print no.of spaces
System.out.println('"' + st + '"');
}
j++;
}
}
}
最佳答案
由于您具有while (j++ <= 3) { ... }
,如果您两次输入“ END”,则程序将结束。