刚学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求高手解答。。
/**
*定义一个长度为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求高手解答。。
应该就可以了
楼主试试
首先是你要求的功能理解的不对,“程序判断数组是否包含有这个单词”,就不能直接用"=="或者equals()来判断,可以用indexof();
其次是你的判断:
if(a[i]==str){ //用equals()
System.out.println("YES");
break;
}
else if(a[i]!=str){ //如果“one”不存在就直接跳出循环了,放在循环后面,改成判断是否大于数组长度就好了
System.out.println("NO");
break;
}
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"是对引用对象的值进行比较。。
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();
}
}
网上谈论这个的太多了,建议去网上多查查,一次搞定该问题!
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);
}
}
另外,本题如果使用集合类来解决的话,就变得简洁多了。
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");
}
}
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;
}}}
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;
}
}
“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
楼主可以研究一下。
/**
*定义一个长度为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();
}
}
帮你调下,满足你的要求
/**
*定义一个长度为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();
}
}
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");
}
}
}
个人的代码 请高手多多指教