有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何比较第一个数组的每个元素和第二个数组的每个元素

如何比较第一个数组的第n个元素与第二个数组的第n个元素

  • 假设第一个数组的第一个元素大于第二个数组的第一个元素
  • 比较两个数组的第二个元素
  • 如果两个元素都大于第二个数组printWIN
  • 如果第一个数组的一个元素小于第二个数组,则打印LOSE

如果第一个数组的所有元素大于第二个数组的所有元素,则printWIN否则printLOSE

例1:

6
10 20 50 100 500 400 
30 20 60 70 90 490 
LOSE

例2:

5
10 20 30 40 50 
40 50 60 70 80
WIN

说明:

在第一个输入中,数组大小为6 由于array2索引3小于array1索引3,因此它会丢失打印 但在第二次输入中,array2中的所有元素都大于array1中的元素,因此它会打印win

i have tried this

import java.util.*;
public class CandidateCode {
    public static void main(String args[] ) throws Exception {
        Scanner sc=new Scanner(System.in);
        int i,j;
        int n=sc.nextInt();
        int a[]=new int[n];
        int b[]=new int[n];
        for(i=0;i<n;i++)
            a[i]=sc.nextInt();
        for(j=0;j<n;j++)
            b[j]=sc.nextInt();
        for(i=0;i<a.length;i++){
            for(j=0;j<b.length;j++){
                if(b[j]>a[i]){
                    System.out.println("WIN");
                }else
                System.out.println("LOSE");
            }           
        }
    }
}

共 (2) 个答案

  1. # 1 楼答案

    您需要采取不同的方法,您的情况是:

    Print WIN if all numbers in second array are greater than the ones in each index of first array, otherwise print LOSE

    你现在要做的是:要求所有更大的数字,用另一种方式问,当你发现一个数字低于第一个数组中的数字时,你就输了。因此:

    1. 创建一个标志变量,我们称之为win

      boolean win = true;
      
    2. 改变你的状况

      发件人:

      if(b[j] > a[i]) {
      

      致:

      if(b[j] < a[i]) {
      
    3. 在该条件内,将标志更改为false,这意味着第二个数组中至少有一个值低于第一个数组中的值。打破循环

      win = false;
      break;
      
    4. 然后只需在win中输入值:

      if (win) {
          System.out.println("WIN");
      } else {
          System.out.println("LOSE");
      }
      

    你完了


    您的代码应该是这样的(我改进了间距以提高可读性,您也应该这样做,我还为循环添加了大括号以提高可读性)

    import java.util.*;
    public class CandidateCode {
        public static void main(String args[] ) throws Exception {
            Scanner sc = new Scanner(System.in);
            int i, j;
            int n = sc.nextInt();
            int a[] = new int[n];
            int b[] = new int[n];
            boolean win = true;
            for(i = 0; i < n; i++) {
                a[i] = sc.nextInt();
            }
            for(j = 0; j < n; j++) {
                b[j]=sc.nextInt();
            }
            for(i = 0; i < a.length; i++) {
                for(j = 0; j < b.length; j++) {
                    if(b[j] < a[i]) {
                        win = false;
                        break;
                    }
                }           
            }
            if (win) {
                System.out.println("WIN");
            } else {
                System.out.println("LOSE");
            }
        }
    }
    

    示例输出:

    3
    1 2 3
    4 5 6
    WIN
    
    3
    1 4 5
    2 1 6
    LOSE
    
  2. # 2 楼答案

    首先,实际上只需要一个循环。由于两个阵列的大小相同,因此无需第二个循环即可对其进行比较:

    for(int i=0; i<n; i++){ 
     if(b[i] > a[i])
      ...
    }
    

    由于您的代码可能会为每个具有更高值的索引打印“WIN”,因此可能有一个不错的解决方法。您可以做的是在代码的开头设置一个变量,并且只有在满足特定需求时才对其进行更改。然后,您可以插入一个打印,作为带有所述变量的最后一行:

    String result = "WIN";
    ...
    for(int i=0; i<n; i++){ 
     if(b[i] < a[i])
      result = "LOSE";
    }
    System.out.println(result);