TOPIC

java. what is problem in my code. can anyone help?

Erik19 asked 5 years ago

import java.util.Scanner;

public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in);

    int a, b;
    while ((a = scan.nextInt()) != 0 && (b = scan.nextInt()) != 0) {
        int[] alice = new int[a];
        int[] betty = new int[b];
        for (int i = 0; i < a; i++) {
            alice[i] = scan.nextInt();
        }
        for (int i = 0; i < b; i++) {
            betty[i] = scan.nextInt();
        }
        int counter = 0, al = 0;
        int counter2 = 0,be = 0;
        for (int i = 0; i < a; ++i) {
            for (int j = 0; j < b; ++j) {
                if (alice[i] != betty[j]){
                    counter++;
                    if (j + 1 == b && counter == b){
                        al++;
                        counter = 0;
                    }
                } else {
                    counter = 0;
                    break;
                }
                if (betty[j] != alice[i]){
                    counter2++;
                    if (j + 1 == b && counter2 == b){
                        be++;
                        counter2 = 0;
                    }
                } else {
                    counter2 = 0;
                    break;
                }
            }
        }
        if (al > be) {
            System.out.println(be);
        } else {
            System.out.println(al);
        }
    }
}

}

This topic has not been answered yet. Be the first!

Remember not post solutions. Your post may be reviewed by our moderators.