第一个 for 循环少执行一个循环

时间:2021-02-17 18:20:47

标签: java for-loop

这是一个用于在两个字符串数组中查找重复项的 Java 程序。

import java.util.*;
public class session
{
    public static void main(String[] args) 
    {
        int flag=0;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the number of students in session 1 and session 2");
        int nk= s.nextInt();
        int ng= s.nextInt();
        String[] ses1 = new String[nk];
        String[] ses2 = new String[ng];
        System.out.println("Enter the registration nos. for session 1");
        for(int i = 0; i < nk; i++)
        {
           ses1[i] = s.nextLine();
        }
        System.out.println("Enter the registration nos. for session 2");
        for(int i = 0; i < ng; i++)
        {
          ses2[i] = s.nextLine();
        }
        for(int i = 0; i < nk; i++)
        {
            for(int j = 0; j < ng; j++)
            {
                if(ses1[i].equals(ses2[j]))
                {
                    flag=1;
                }
            }
        }
        if(flag==0)
            System.out.println("No Duplicates");
        else
            System.out.println("Error, There are duplicates");
    }
}

第一个 for 循环运行 nk-1 个周期而不是 nk 个周期。是否有涉及多个 for 循环的规则?

0 个答案:

没有答案