刚学JAVA有2个多月了,求教一个问题,研究了一晚上都没搞定。。>_<
/**
*定义一个长度为10的一维字符串数组,在每一个元素存放一个单词;然后运行时从命令
*行输入一个单词,程序判断数组是否包含有这个单词,包含这个单词就打印出“Yes” ,不包
*含就打印出“No”
*/
import java.io.*;
class MyPoint
{
    public void array(String str){
String a[]=new String[10];
a[0]="one";
a[1]="two";
a[2]="three";
a[3]="four";
a[4]="five";
a[5]="six";
a[6]="seven";
a[7]="eight";
a[8]="night";
a[9]="ten";
for(int i=0;i<a.length ;i++){
 if(a[i]==str){
 System.out.println("YES");
 break;
 }
 else if(a[i]!=str){
 System.out.println("NO");
 break;
 }
        }
    }
}
public class Zy0503

public static void Print()
{
System.out.print("please input a value :");
} public  static void userPut() throws java.io.IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                String input = reader.readLine();
MyPoint mp = new MyPoint();
mp.array(input);
}
            
public static void main(String[] args) throws java.io.IOException
{
Print();
userPut();
}
}
编译能正常通过,但是执行的时候无论给包含在该数组中的值,还是不包含在该数组的值,打印都为NO求高手解答。。

解决方案 »

  1.   

    if(a[i]==str)这一句改成if(a[i].equals(str))
    应该就可以了
    楼主试试
      

  2.   

    有两处不妥当!
    首先是你要求的功能理解的不对,“程序判断数组是否包含有这个单词”,就不能直接用"=="或者equals()来判断,可以用indexof();
    其次是你的判断:
    if(a[i]==str){ //用equals()
    System.out.println("YES"); 
    break; 

    else if(a[i]!=str){ //如果“one”不存在就直接跳出循环了,放在循环后面,改成判断是否大于数组长度就好了
    System.out.println("NO"); 
    break; 

      

  3.   

    同意楼上的说法哈,我也是个才学Java的,我把改了的代码发上来了。import java.io.*; 
    class MyPoint 

        public void array(String str){ 
    String a[]=new String[10]; 
    a[0]="one"; 
    a[1]="two"; 
    a[2]="three"; 
    a[3]="four"; 
    a[4]="five"; 
    a[5]="six"; 
    a[6]="seven"; 
    a[7]="eight"; 
    a[8]="night"; 
    a[9]="ten"; 
    for(int i=0;i <a.length ;i++){ 
    if(a[i].equals(str)){ //用equals()进行比较
    System.out.println("YES"); 
    break;
    }
    else if(i==(a.length-1)){//遍历数组后,没有满足条件的返回NO
    System.out.println("NO");
    break;
    }
            } 
        } 

    public class Zy0503 

    public static void Print() 

    System.out.print("please input a value :"); 
    }  public  static void userPut() throws java.io.IOException 

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String input = reader.readLine(); 
    MyPoint mp = new MyPoint(); 
    mp.array(input); 

                
    public static void main(String[] args) throws java.io.IOException 

    Print(); 
    userPut(); 

    } 没有把==和equals区分开
    "=="是对引用对象的地址进行比较。。
    "equals"是对引用对象的值进行比较。。
      

  4.   

    我也想了个。。  HOHOpackage lgc;
    import java.io.*; 
    class MyPoint 

        public void array(String str){ 
    String a[]=new String[10]; 
    a[0]="one"; 
    a[1]="two"; 
    a[2]="three"; 
    a[3]="four"; 
    a[4]="five"; 
    a[5]="six"; 
    a[6]="seven"; 
    a[7]="eight"; 
    a[8]="night"; 
    a[9]="ten";
    int zy=0;
    for(int i=0;i <a.length ;i++){ 
    if(a[i].equals(str)){ 
    System.out.println("YES"); 
    zy=1;
    //break; 
    } }
    if(zy==0)System.out.println("NO");
    /*else if(!(a[i].equals(str))){ 
    System.out.println("NO"); 
    //break; 

            }*/ 
        } 

    public class Zy0503 

    public static void Print() 

    System.out.print("please input a value :"); 
    } public  static void userPut() throws java.io.IOException 

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
                    String input = reader.readLine(); 
    MyPoint mp = new MyPoint(); 
    mp.array(input); 

                
    public static void main(String[] args) throws java.io.IOException 

    Print(); 
    userPut(); 


      

  5.   

    请问 for语句中的 break 会跳到  i++这里 还是  跳出 整个for语句?
      

  6.   

    慎用“==”,只有两个对象地址相同,那就意味着为同一对象时才返回true!实际应用中,我们多数情况关注的是,“值”是否相等(和地址没有一点关系),“值”相等即返回true! 这就是用"equals"了!也就是说用“==”判断返回true时,那用"equals"也一定返回true,反过来就不一定了!
    网上谈论这个的太多了,建议去网上多查查,一次搞定该问题!
      

  7.   

    没结贴,我也来路过。package lgc;
    import java.io.*; 
    class MyPoint { 
        public static final YES = "YES";
        public static final NO = "NO";    public String  [] getArray(){ 
            String a[]=new String[10]; 
            a[0]="one"; 
            a[1]="two"; 
            a[2]="three"; 
            a[3]="four"; 
            a[4]="five"; 
            a[5]="six"; 
            a[6]="seven"; 
            a[7]="eight"; 
            a[8]="night"; 
            a[9]="ten";
            return a;
        }    public boolean contains(String str){
            String arr = getArray();
            for(int i=0;i <arr.length ;i++) 
                if(a[i].equals(str)) return true;
            return false;
        }    public void printContains(String str){
            if(contains(str))System.out.println(YES);
            System.out.println(NO);
        }

    public class Zy0503 {     
        public static void main(String[] args) throws java.io.IOException { 
            System.out.print("please input a value :");
            String line = new Scanner(System.in).nextLine();
            new MyPoint().printContains(line);
        } 

      

  8.   

    字符串内容判断用equals方法。
    另外,本题如果使用集合类来解决的话,就变得简洁多了。
      

  9.   


    import java.util.*;
    public class ContainsTest
    {
    public static void main(String[] args)
    {
    String[] a = {"one","two","three","four","five","six","seven","eight","nine","ten"};
    ArrayList<String> list = new ArrayList<String>(Arrays.asList(a));
    System.out.println("请输入");
    Scanner in = new Scanner(System.in);
    String input = in.next();
    if(list.contains(input))
    System.out.println("yes");
    else
    System.out.println("no");
    }
    }
      

  10.   

    public class Test{
       public static void main0(String[] args){
      String a[]=new String[10]; 
    a[0]="one"; 
    a[1]="two"; 
    a[2]="three"; 
    a[3]="four"; 
    a[4]="five"; 
    a[5]="six"; 
    a[6]="seven"; 
    a[7]="eight"; 
    a[8]="night"; 
    a[9]="ten";
    for(int i=0;i <a.length ;i++){ 
                if(a[i].equals(str)){               
                   System.out.println("YES"); 
                    break;
                }
                else if(i==(a.length-1
                    System.out.println("NO");
                    break;
                }}}
      

  11.   

    直接return就行了
    for ( int i = 0; i < a.length; i++ )
    {
    if ( a[i].equals( str ) )
    {
    System.out.println( "YES" );
    return;
    } else if ( !a[i].equals( str ) )
    {
    System.out.println( "NO" );
    return;
    }
    }
      

  12.   

    “==”比较的是引用对象的值;
    “equals”比较的是值对象的值;String str1 = new String("aa");
    String str2 = new String("aa");
    String str3 = "aa";
    String str4 = "aa";
    System.out.println(str1==str2);
    System.out.println(str1.equals(str2));
    System.out.println(str1==str3);
    System.out.println(str1.equals(str3));
    System.out.println(str3==str4);运行结果是:
    false
    true
    false
    true
    true
    楼主可以研究一下。
      

  13.   

    已经有人帮你解决了,可是你还是认真弄清楚==写equals的区别才好。
      

  14.   


    /** 
    *定义一个长度为10的一维字符串数组,在每一个元素存放一个单词;然后运行时从命令 
    *行输入一个单词,程序判断数组是否包含有这个单词,包含这个单词就打印出“Yes” ,不包 
    *含就打印出“No” 
    */ 
    import java.io.*; 
    class MyPoint 

        public void array(String str){ 
    String a[]=new String[10]; 
    a[0]="one"; 
    a[1]="two"; 
    a[2]="three"; 
    a[3]="four"; 
    a[4]="five"; 
    a[5]="six"; 
    a[6]="seven"; 
    a[7]="eight"; 
    a[8]="night"; 
    a[9]="ten"; 
    for(int i=0;i <a.length ;i++){ 
    if(a[i].equals(str)){ 
    System.out.println("YES"); 
    return;

     }
    System.out.println("NO");  
            } 
        

    public class Zy0503 

    public static void Print() 

    System.out.print("please input a value :"); 
    } public  static void userPut() throws java.io.IOException 

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
                    String input = reader.readLine(); 
    MyPoint mp = new MyPoint(); 
    mp.array(input); 

                
    public static void main(String[] args) throws java.io.IOException 

    Print(); 
    userPut(); 


    帮你调下,满足你的要求
      

  15.   

    要比较对象的值得用equals,== 比较的是对象的引用,当然这里对象的引用是不相等的啦,而且你的程序在逻辑上也有问题,帮你调了一下
    /**
     *定义一个长度为10的一维字符串数组,在每一个元素存放一个单词;然后运行时从命令
     *行输入一个单词,程序判断数组是否包含有这个单词,包含这个单词就打印出“Yes” ,不包
     *含就打印出“No”
     */
    import java.io.*;class MyPoint {
    public void array(String str) {
    String a[] = new String[10];
    a[0] = "one";
    a[1] = "two";
    a[2] = "three";
    a[3] = "four";
    a[4] = "five";
    a[5] = "six";
    a[6] = "seven";
    a[7] = "eight";
    a[8] = "night";
    a[9] = "ten";
    int i = 0;
    for (i = 0; i < a.length; i++) {
    if (a[i].equals(str)) {
    System.out.println("YES");
    break;

    }
    if(i >= a.length){
    System.out.println("NO");
    }
    }
    }
    public class Zy0503 {
    public static void Print() {
    System.out.print("please input a value :");
    } public static void userPut() throws java.io.IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
    System.in));
    String input = reader.readLine();
    MyPoint mp = new MyPoint();
    mp.array(input);
    } public static void main(String[] args) throws java.io.IOException {
    Print();
    userPut();
    }
    }
      

  16.   

    有事==和equal的问题,好好研究下,必须掌握的问题,==是比较两个对象的地址,equal才是比较值得
      

  17.   

    来迟了,下次遇到这种问题,可以先查查api看是不是方法的问题,能自己解决的话就最好了,也不用花掉这100分
      

  18.   

    楼主主要是没弄明白"==" 与 "equals" 之间的区别...."=="是对引用对象的地址进行比较,"equals"是对引用对象的值进行比较....
      

  19.   

    import java.io.*;
    class Test{
    public static String[]  getStr(){
    String str[]=new String[10];
    str[0]="one";
        str[1]="two";
        str[2]="three";
        str[3]="four";
        str[4]="five";
        str[5]="six";
        str[6]="seven";
        str[7]="eight";
        str[8]="night";
        str[9]="ten";
        return str;
    }
    }
    public class  Zy0503{
    public static void main(String args[])throws Exception{
    fenXi();
    }
    public static String getShuru()throws Exception{
    System.out.println("请输入:");
    InputStreamReader fr=new InputStreamReader(System.in);
    BufferedReader br=new BufferedReader(fr);
    String string=br.readLine();
    return string;
    };
    public static void fenXi() throws Exception  {
    String[] str=Test.getStr();
    String str1=getShuru();
    for(int i=0;i<str.length;i++){
    if(str[i].indexOf(str1)!=-1){
    System.out.println("yes");
    }
    else System.out.println("no");
    }
    }
    }
    个人的代码  请高手多多指教