Java '';无法解析'';如何解决此错误?
我是一个初学者,对Java的理解很差。我使用Dr.Java,我得到以下错误:Java '';无法解析'';如何解决此错误?,java,string,Java,String,我是一个初学者,对Java的理解很差。我使用Dr.Java,我得到以下错误: "S cannot be resolved" 使用此Java程序: import java.util.Scanner; public class HDtest5 { public static int countWords(String str) { String words[] = str.split(" "); int count = words.length
"S cannot be resolved"
使用此Java程序:
import java.util.Scanner;
public class HDtest5
{
public static int countWords(String str) {
String words[] = str.split(" ");
int count = words.length;
return count;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = in.nextLine();
System.out.print("Your sentence has " + countWords(sentence) + " words.");
}
{
while (true) // have created infinite loop
{
if (s.equals("quit"))
// if enterd value is "quit" than it comes out of loop
{
String[] words = s.split(" "); // get the individual words
System.out.println("#words = " + words.length);
for (int i = 0; i < words.length; i++)
System.out.println(s + "word[" + i + words[i] + " nchars = " + words[i].length());
}
System.out.println(s); // to Print string
System.out.println(s.length()); // to print Entered string's length
}
}
}
import java.util.Scanner;
公共类HDtest5
{
公共静态整型countWords(字符串str){
字符串字[]=str.split(“”);
int count=words.length;
返回计数;
}
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入句子:”);
字符串语句=in.nextLine();
System.out.print(“你的句子有”+countWords(句子)+“words”);
}
{
while(true)//已创建无限循环
{
如果(s.equals(“退出”))
//若输入值为“退出”,则它将退出循环
{
String[]words=s.split(“”;//获取单个单词
System.out.println(“#words=“+words.length”);
for(int i=0;i
是什么原因导致此错误消息?
我认为所有名为“s”的字符串应该可以一起工作,没有任何问题。您能帮我找出它有什么问题吗?非常感谢您的帮助,非常感谢
另外,更清楚的是,我的程序的目的是输出字符总数、每个单词的字符数以及在文本窗口(扫描仪)中输入的文本的单词数。代码应该可以很好地工作,除了这个“s”字符串问题,它使代码块不能一起工作。在Java中,您必须先声明一个变量,然后才能使用它 缺少如下代码行:
String s;
顺便说一句,正如其他人所注意到的那样,您可能希望使用句子
而不是s
。可能的值“quit”
将位于句子
变量中
通过声明变量: 你说编译器: 从现在起,将
personName
理解为一段能够存储字符串的内存
编译器将为您完成一项伟大的工作-它不会让您分配给personName
任何其他内容(如int
),如果您输入了一个拼写错误并在某个地方编写了PersonName
,您会立即发现它,而且不会很晚,因为在运行时,您不知道代码为什么不能工作,就像在JavaScript中一样。在Java中,您必须先声明一个变量,然后才能使用它
缺少如下代码行:
String s;
顺便说一句,正如其他人所注意到的那样,您可能希望使用句子
而不是s
。可能的值“quit”
将位于句子
变量中
通过声明变量:
你说编译器:
从现在起,将personName
理解为一段能够存储字符串的内存
编译器将为您完成一项伟大的工作-它不会让您分配给personName
任何其他内容(如int
),如果您输入了一个拼写错误并在某个地方编写了PersonName
,您会立即发现它,而且不会很晚,因为在运行时,您不知道代码为什么不能工作,就像在JavaScript中一样。您没有在代码中声明s
。请在main()的开头编写这样的内容,String s=null;
方法,然后在以后的代码中给它一些值您还没有在代码中声明s
。在main()的开头写这样的内容,String s=null;
方法,然后在以后的代码中给它一些值您会遇到此错误,因为您没有声明任何变量,但在if条件中使用了此变量。此外,您还错误地放置了{}大括号。
将主要方法更改为以下内容:
注意:您的程序正在infinte循环中运行
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = in.nextLine();
System.out.print("Your sentence has " + countWords(s) + " words.");
while (true) // have created infinite loop
{
if (s.equals("quit"))
// if enterd value is "quit" than it comes out of loop
{
String[] words = s.split(" "); // get the individual words
System.out.println("#words = " + words.length);
for (int i = 0; i < words.length; i++)
System.out.println(s + "word[" + i + words[i]
+ " nchars = " + words[i].length());
}
System.out.println(s); // to Print string
System.out.println(s.length()); // to print Entered string's length
}
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入句子:”);
字符串s=in.nextLine();
System.out.print(“你的句子有”+countWords+words.”);
while(true)//已创建无限循环
{
如果(s.equals(“退出”))
//若输入值为“退出”,则它将退出循环
{
String[]words=s.split(“”;//获取单个单词
System.out.println(“#words=“+words.length”);
for(int i=0;i
之所以出现此错误,是因为您没有声明任何变量,但在if条件中使用了此变量。此外,您还错误地放置了{}大括号。
将主要方法更改为以下内容:
注意:您的程序正在infinte循环中运行
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = in.nextLine();
System.out.print("Your sentence has " + countWords(s) + " words.");
while (true) // have created infinite loop
{
if (s.equals("quit"))
// if enterd value is "quit" than it comes out of loop
{
String[] words = s.split(" "); // get the individual words
System.out.println("#words = " + words.length);
for (int i = 0; i < words.length; i++)
System.out.println(s + "word[" + i + words[i]
+ " nchars = " + words[i].length());
}
System.out.println(s); // to Print string
System.out.println(s.length()); // to print Entered string's length
}
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入句子:”);
字符串s=in.nextLine();
System.out.print(“你的句子有”+countWords+words.”);
while(true)//已创建无限循环
{
如果(s.equals(“退出”))
//若输入值为“退出”,则它将退出循环
{
String[]words=s.split(“”;//获取单个单词
System.out.println(“#words=“+words.length”);
for(int i=0;iimport java.util.Scanner;
public class HDtest5
{
public static int countWords(String str) {
String words[] = str.split(" ");
int count = words.length;
return count;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true) // have created infinite loop
{
System.out.print("Enter a sentence: ");
String sentence = in.nextLine();
if (sentence.equals("quit")) {
// if enterd value is "quit" than it comes out of loop
break;
}
System.out.print("Your sentence has " + countWords(sentence) + " words.");
String[] words = sentence.split(" "); // get the individual words
System.out.println("#words = " + words.length);
for (int i = 0; i < words.length; i++)
System.out.println("word[" + i + "] = " + words[i] + " nchars = " + words[i].length());
}
System.out.println(sentence); // to Print string
System.out.println(sentence.length()); // to print Entered string's length
}
in.close();
}
}