当前位置: 代码迷 >> Java相关 >> 这样赋值错在哪.
  详细解决方案

这样赋值错在哪.

热度:131   发布时间:2007-06-15 14:43:03.0
这样赋值错在哪.

String[] str=new String[4];
str[0]="hello";
str[1]="love";
str[2]="ting";
str[3]="abc";
总提示错误except']',知道的请给我解释解释.谢谢.
搜索更多相关的解决方案: 赋值  

----------------解决方案--------------------------------------------------------
没什么错啊,把完整的代码和错误信息帖出来看看。
----------------解决方案--------------------------------------------------------

这个程序要实现的功能是用户输入一字符串,如果在数组中有匹配的就输出"done",没有就输出"nothing".
import java.io.*;
import java.util.*;
public class inputMethed{
public static void main(String args[]){

inputMethed in=new inputMethed();

Scanner sc=new Scanner(System.in);
System.out.println("************************");
System.out.println("a simple input program.");
System.out.println("************************");
if(sc.hasNextLine())
{
String s=sc.nextLine();
in.match(s);
}
}
/* public void strLibrary(){
String str[]=new String[4];
str[0]="hello";
str[1]="love";
str[2]="ting";
str[3]="abc";
}*/

public void match(String s){
for(int i=0;i<strLibrary.str.length();i++)

if(s==str[i])
System.out.println("done.");
else
System.out.println("nothing.");
}
}

class strLibrary{
// String str[]={"hello","long","love","ting"};

String[] str=new String[4];         //就是这几行
str[0]="hello";
str[1]="love";
str[2]="ting";
str[3]="abc";
}


----------------解决方案--------------------------------------------------------

程序代码:
import java.io.*;
import java.util.*;
public class inputMethed{
public static void main(String args[]){

inputMethed in=new inputMethed();

Scanner sc=new Scanner(System.in);
System.out.println(\"************************\");
System.out.println(\"a simple input program.\");
System.out.println(\"************************\");
if(sc.hasNextLine())
{
String s=sc.nextLine();
in.match(s);
}
}
/* public void strLibrary(){
String str[]=new String[4];
str[0]=\"hello\";
str[1]=\"love\";
str[2]=\"ting\";
str[3]=\"abc\";
}*/

public void match(String s){
for(int i=0;i<strLibrary.str.length();i++)
//这里应该创建一个strlibrary类的对象,通过对象来访问str,因为str不是static的,不能像这样来访问,还有length是属性不是方法,不用加括号
if(s==str[i])
System.out.println(\"done.\");
else
System.out.println(\"nothing.\");
}
}

class strLibrary{
// String str[]={\"hello\",\"long\",\"love\",\"ting\"};

String[] str=new String[4];         //就是这几行
str[0]=\"hello\";
str[1]=\"love\";
str[2]=\"ting\";
str[3]=\"abc\";
//这几句应该放在一个方法里面。可以想像这样:
public strLibrary()
{

str[0]=\"hello\";
str[1]=\"love\";
str[2]=\"ting\";
str[3]=\"abc\";
}
}


----------------解决方案--------------------------------------------------------

修改了一下,没成功,以下是修改后的:
import java.io.*;
import java.util.*;
public class inputMethed{
public static void main(String args[]){

inputMethed in=new inputMethed();

Scanner sc=new Scanner(System.in);
System.out.println("************************");
System.out.println("a simple input program.");
System.out.println("************************");
if(sc.hasNextLine())
{
String s=sc.nextLine();
in.match(s);
}
}
public void match(String s){
strLibrary sl=new strLibrary();
for(int i=0;i<sl.str.length;i++)
if(s==str[i])               //系统提示错在这一行,提示内容为找不到符号,
{                     //换成表达式s.equals(str[i])后还是一样,不知道为什么   
System.out.println("done.");
System.exit(0);
}

System.out.println("nothing.");

}
}

class strLibrary{

String[] str=new String[4];
public strLibrary()
{
str[0]="hello";
str[1]="love";
str[2]="ting";
str[3]="abc";
}
}


----------------解决方案--------------------------------------------------------
for(int i=0;i<sl.str.length;i++)
if(s==str[i]) //str改为sl.str , == 改为equals()              
{                       
System.out.println("done.");
System.exit(0);
}

----------------解决方案--------------------------------------------------------
if(s.equals(sl.str[i]))
是不是要改成这样? 不好意思. 你的程序我都没看懂.才学的 JAVA.
----------------解决方案--------------------------------------------------------
嘿嘿,终于弄好了,多谢了.
----------------解决方案--------------------------------------------------------
  相关解决方案