Java中的字符串数组,为相同的输入提供不同的值

时间:2018-09-04 16:37:16

标签: arrays string

以下是两个代码,它们为同一输入提供不同的输出。 在第二个代码中,如果我将s的值输入为5,将q的值输入为10,则将字符串s1 []中的元素输入为Y,N,D,B,G,将字符串s2 []中的元素输入为Y,Y,G,B ,G,N,B,N,D,GI得到0作为输出,但是在第一个代码中我得到1作为输出。

import java.io.*;
public class Ch
{
 public static void main()throws IOException
 {
    InputStreamReader read = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(read);
    int k=0, max=0,i,j,count;
    int sw=-1;
    String s1[]={"Y","N","D","B","G"};
    String s2[]={"Y","Y","G","B","G","N","B","N","D","G"};

    while(k<10)
    {
        for(i=0;i<5;i++)
        {
            count=0;
            for(j=k;j<10;j++)
            {
                if(s1[i]!=s2[j])
                    count++;
                if(s1[i]==s2[j])
                    break;
            }
            if(count>max)
            {
                max=count;
            }
        }
        sw++; k=k+max; max=0;
    }
    System.out.println(sw);
}
}

 ______________________________


import java.io.*;
public class Ch1
{
public static void main()throws IOException
{
    InputStreamReader read = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(read);
    int k=0, max=0,i,j,count=0;
    int sw=-1;
    String s1[];
    String s2[];
    System.out.println("Please enter the number of search engines");
    int s=Integer.parseInt(in.readLine());
    s1=new String[s];
     for(i=0;i<s;i++)
    {
    s1[i]=in.readLine();
    }
    System.out.println("Please enter the number of queries");
    int q=Integer.parseInt(in.readLine());
    s2=new String[q];

    for(i=0;i<q;i++)
    {
    s2[i]=in.readLine();
    }
    while(k<q)
    {
        for(i=0;i<s;i++)
        {
            count=0;
            for(j=k;j<q;j++)
            {
                if(s1[i]!=s2[j])
                    count++;
                if(s1[i]==s2[j])
                    break;
            }
            if(count>max)
            {
                max=count;
            }
        }
        sw++; k=k+max; max=0;
    }
    System.out.println(sw);
}
}

0 个答案:

没有答案