| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 509 人关注过本帖
标题:这样赋值错在哪.
只看楼主 加入收藏
bhbh113
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-5-22
收藏
 问题点数:0 回复次数:7 
这样赋值错在哪.

String[] str=new String[4];
str[0]="hello";
str[1]="love";
str[2]="ting";
str[3]="abc";
总提示错误except']',知道的请给我解释解释.谢谢.
搜索更多相关主题的帖子: 赋值 
2007-06-15 14:43
pity1115
Rank: 1
等 级:新手上路
威 望:2
帖 子:184
专家分:0
注 册:2006-9-15
收藏
得分:0 
没什么错啊,把完整的代码和错误信息帖出来看看。

2007-06-15 14:46
bhbh113
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-5-22
收藏
得分:0 

这个程序要实现的功能是用户输入一字符串,如果在数组中有匹配的就输出"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";
}


2007-06-15 14:52
pity1115
Rank: 1
等 级:新手上路
威 望:2
帖 子:184
专家分:0
注 册:2006-9-15
收藏
得分:0 

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\";
}
}


2007-06-15 15:09
bhbh113
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-5-22
收藏
得分:0 

修改了一下,没成功,以下是修改后的:
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";
}
}


2007-06-15 15:54
pity1115
Rank: 1
等 级:新手上路
威 望:2
帖 子:184
专家分:0
注 册:2006-9-15
收藏
得分:0 
for(int i=0;i<sl.str.length;i++)
if(s==str[i]) //str改为sl.str , == 改为equals()              
{                       
System.out.println("done.");
System.exit(0);
}

2007-06-15 15:59
atomhdp
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2005-10-2
收藏
得分:0 
if(s.equals(sl.str[i]))
是不是要改成这样? 不好意思. 你的程序我都没看懂.才学的 JAVA.

2007-06-15 16:09
bhbh113
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-5-22
收藏
得分:0 
嘿嘿,终于弄好了,多谢了.

2007-06-15 18:02
快速回复:这样赋值错在哪.
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011816 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved