【发布时间】:2019-03-24 10:31:37
【问题描述】:
我在 Java 方面非常有经验,但最近我编写了这个程序来对字符串进行排序(请不要介意效率和类似的东西)。但是我得到了这个奇怪的错误。
这是代码:
import java.io.*;
class Prog3
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter a String: ");
String s = br.readLine().toUpperCase();
String st = "", finalStr = "";
int count = 1, arI = 0;
char t = s.charAt(s.length()-1);
if(!(t=='.'||t=='?'||t=='!'))
{
System.out.print("Invalid Input!");
System.exit(0);
}
for(int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if(ch==' ')
{
count++;
}
}
String[] ar = new String[count];
for (int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if(ch!=' '&&ch!='.'&&ch!='?'&&ch!='!')
{
st += ch;
}
else
{
ar[arI++] = st;
st = " ";
}
}
for ( int i = 0; i < ar.length-1; i++);
{
for ( int j = 0; j < ar.length-1-i; j++);
{
if (ar[j].compareTo(ar[j+1])>0)
{
String temp = ar[j];
ar[j] = ar[j+1];
ar[j+1] = temp;
}
}
finalStr = ar[ar.length-1-i] + " " + finalStr;
}
System.out.println("Length: " + count);
System.out.println("Rearranged Sentence:\n" + ar[0] + " " + finalStr);
}
}
但我仍然在第 41 行收到此“找不到符号”错误,依此类推:
Prog3.java:41: error: cannot find symbol
for ( int j = 0; j < ar.length-1-i; j++);
^
symbol: variable i
location: class Prog3
Prog3.java:43: error: cannot find symbol
if (ar[j].compareTo(ar[j+1])>0)
^
symbol: variable j
location: class Prog3
Prog3.java:43: error: cannot find symbol
if (ar[j].compareTo(ar[j+1])>0)
^
symbol: variable j
location: class Prog3
Prog3.java:45: error: cannot find symbol
String temp = ar[j];
^
symbol: variable j
location: class Prog3
Prog3.java:46: error: cannot find symbol
ar[j] = ar[j+1];
^
symbol: variable j
location: class Prog3
Prog3.java:46: error: cannot find symbol
ar[j] = ar[j+1];
^
symbol: variable j
location: class Prog3
Prog3.java:47: error: cannot find symbol
ar[j+1] = temp;
^
symbol: variable j
location: class Prog3
Prog3.java:50: error: cannot find symbol
finalStr = ar[ar.length-1-i] + " " + finalStr;
^
symbol: variable i
location: class Prog3
8 errors
如果我愚蠢地错过了一些愚蠢的事情,那么我非常抱歉。
【问题讨论】:
-
循环行末尾有分号 (
-1; i++);),这会导致循环结束。投票结束是一个错字。 -
非常感谢。我不敢相信我错过了。现在我感到很尴尬。
-
我们每天至少在这里看到一次,用某种语言。这很常见。请记住这一点。
-
一个建议:使用 IDE 的自动格式化。至少 Eclipse 在按下 Ctrl-Shift-F 后清楚地显示了问题:循环体只是分号,并在自己的行上缩进。