【发布时间】:2018-05-17 12:53:13
【问题描述】:
当“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++;
}
}
}
【问题讨论】:
-
如果你还没有学会调试,这篇文章可能会对你有所帮助:How to debug small programs
-
是什么让你认为它不是?
-
还有一个问题:为什么是
while (j++ <= 3)(特别是考虑到你稍后会做j++)?你为什么首先循环这部分程序? -
“我还没学会调试”现在是学习它的好时机!