简体   繁体   English

我无法弄清楚比较数组索引有什么问题

[英]I can't figure out what's wrong with comparing array indexes

I need to compare array indexes, not their meanings.我需要比较数组索引,而不是它们的含义。 I'm a newbie in java at all, and wrote this code and can't understand what exactly I'm doing wrong.我根本是java的新手,写了这段代码,不明白我到底做错了什么。 Pls hlp.请帮忙。

public class Solution {
public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(System.in);
    int[] numOfPeople = new int[15];
    for (int i = 0; i < numOfPeople.length; i++) {
        numOfPeople[i] = scanner.nextInt();
        
    int sum2 = 0;
    int sum1 = 0;
    
    if (i % 2 == 0) {
        sum2 = sum2 + numOfPeople[i];
    } else if (i % 2 != 0) {
        sum1 = sum1 + numOfPeople[i];
    }
    
    if (sum2 > sum1) {
        System.out.println("В домах с четными номерами проживает больше жителей.");
    } else if (sum2 < sum1) {
        System.out.println("В домах с нечетными номерами проживает больше жителей.");
    } else {
        System.out.println();
            }
        }
    }

} }

Please see my comments.请看我的评论。 Make sure you close the scanner and assign the variables outside the loop.确保关闭扫描仪并在循环外分配变量。 I am not sure when you want the evaluation to take place, but it seems like it should not happen on every loop.我不确定您希望何时进行评估,但似乎它不应该在每个循环中都发生。 I think you only want the end result, no?我想你只想要最终结果,不是吗?

 import java.util.Scanner; public class Solution { public static void main(String[] args) { int[] numOfPeople = new int[15]; //I think you want these to be available outside the loop and not modified every iteration of the loop int sum2 = 0; int sum1 = 0; Scanner scanner = new Scanner(System.in); for (int i = 0; i < numOfPeople.length; i++) { //I added this just to help you see the iteration System.out.println("You must enter the value for index i: " + i); numOfPeople[i] = scanner.nextInt(); //I think you mean to know if the index is even or odd here if (i % 2 == 0) { sum2 = sum2 + numOfPeople[i]; } else if (i % 2 != 0) { sum1 = sum1 + numOfPeople[i]; } } // I think you want to close the loop here, because you do not want this evaluated every time you go to a new index, but rather at the end? scanner.close(); if (sum2 > sum1) { System.out.println("Even has more occupancy"); } else if (sum2 < sum1) { System.out.println("Odd has more occupancy"); } else { System.out.println(); } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要使用ArrayList类对数组进行排列。 我不知道怎么了 - I need to make permutations with an array using the ArrayList Class. I can't figure out what's wrong 无法弄清楚我的DocumentFilter有什么问题 - Can't figure out what's wrong with my DocumentFilter 我无法弄清楚出了什么问题,出现了“类型已定义的错误” - I can't figure out what's wrong, getting a 'type already defined error' 我不知道怎么了,我的逻辑似乎正确 - I can't figure out what's wrong, my logic seems correct 我无法弄清楚我的NullPointerException有什么问题或者为什么它甚至存在 - I can't figure out what's wrong with my NullPointerException or why it even exists 项目Euler#23(Java)。 我不知道怎么了。 答案是64 - Project Euler #23 (Java). I can't figure out what's wrong. Answer is off by 64 解析错误; 我已经尝试过,无法弄清楚出了什么问题? - Parsing error; I have tried and can't figure out what's wrong? 比较数组中的元素以查找对,但是在找到两个对时无法弄清楚该怎么做 - Comparing elements in my array to find pairs, but can't figure out what to do when it finds two pairs 索引超出范围异常,但无法找出问题所在 - Index out of bounds Exception but can't figure out what is wrong 数组索引是“越界”。 但我看不出有什么问题 - Array Index is “Out of Bounds”. But I can't see what's wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM