【问题标题】:array of even, odd, and negative integers [closed]偶数、奇数和负整数数组[关闭]
【发布时间】:2014-06-01 04:08:31
【问题描述】:

我需要:

编写一个程序,输入 10 个整数。该程序将偶数放入一个称为evenList 的数组中,将奇数放入一个称为oddList 的数组中,将负整数放入一个称为negativeList 的数组中。输入所有整数后,程序会显示三个数组的内容。

import java.util.Arrays;
import java.util.Scanner;

public class testsheeeyt {

    public testsheeeyt() {

}

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int evenList[] = new int[10];
    int oddList[] = new int[10];
    int negativeList[] = new int[10];
    int neg =0;
    int odd = 0;
    int even =0;
    int x=0;

    System.out.println("Enter number ");
    for(x= 0;x<10;++x){     



        if(x%2 == 0 && x>0){
            evenList[x] = scan.nextInt();
            even++;
        } 
        if(x%2 != 0){
            oddList[x] = scan.nextInt();
            odd++;
        }if(x<0){
            negativeList[x] = scan.nextInt();
            neg++;
        }
    }


    System.out.println("The list of negative numbers is :-"); 
    for (x = 0; x < neg; x++) 
    { 
    System.out.println( negativeList[x]); 
    } 
    System.out.println("\nThe list of even numbers is :-"); 
    for (x = 0; x < even; x++) 
    { 
    System.out.println( evenList[x]); 
    } 
    System.out.println("The list of odd numbers is :-"); 
    for (x = 0; x < odd; x++) 
    { 
    System.out.println( oddList[x]); 
    }

    System.exit(0);
}

}

这行不通。帮助。

【问题讨论】:

  • 您遇到什么错误?你试过什么?能否请您发布更多信息,以便我们知道在哪里查找。
  • "it won't work" 没有提供太多可以用来帮助您的信息。请提供有关您的问题和问题的更多详细信息。
  • @Winston:请不要使用代码跨度 (like this) 来突出显示文本——它只能用于句子中的代码。例如,x % 2 != 0 可以,但 integers 不应该在代码跨度中。谢谢!
  • 好的,感谢指导。
  • 我的数组不在正确的类别下,我在最终结果中得到了很多 0。负面的通常永远不会起作用

标签: java arrays sorting


【解决方案1】:

首先,我认为您希望输入循环看起来像这样 -

for (int x = 0; x < 10; ++x) {
  System.out.println("Enter number: " + (x + 1));
  int num = scan.nextInt(); // <-- get user input to
                            // test.
  // Test for negative first.
  if (num < 0) {
    negativeList[neg++] = num;
  } else {
    // It isn't negative.
    if (num % 2 == 0) {
      // It is even.
      evenList[even++] = num;
    } else {
      // It must be odd.
      oddList[odd++] = num;
    }
  }
}

其次,这里有一种输出方法

// Are there any negative numbers?
if (neg > 0) {
  System.out.println("The negative numbers are: ");
  for (int x = 0; x < neg; x++) {
    if (x != 0) {
      // Add a comma between entries.
      System.out.print(", ");
    }
    // Print the number at x (where x is 0 to neg).
    System.out.print(negativeList[x]);
  }
  // Add a new line.
  System.out.println();
}

另一种打印数组的方法(如果它们的大小正确)是使用Arrays#toString(int[]) -

 System.out.println("The negative numbers are: " + Arrays.toString(negativeList));

【讨论】:

  • 我开始明白了。我现在可以确定它是否为负数并显示这些数字(根据您的代码),但我还需要弄清楚如何显示偶数和奇数值。 { System.out.println("偶数为:"); for (int x = 0; x
  • 您需要if (x != 0) 而不是(x % 2 =! 0),这可能会给您带来编译器错误。否则,这看起来是正确的。
【解决方案2】:

您需要检查输入数字而不是 x。 喜欢

for(x=0;x<10;++x){
    int t = scan.nextInt();
    if(t>=0){
        if(t%2 == 0 ){
            evenList[x] = t;
            even++;
        } else{
            oddList[x] = t;
            odd++;
        }
    }else {
        negativeList[x] = t;
        neg++;
    }
}

【讨论】:

    【解决方案3】:

    你在x 从一跳到十。

    如果x 是奇数,即使它是偶数,你也将输入放入奇数数组 如果x 是 evev 你把输入放到偶数数组中,即使它是偶数

    你想得到输入,看看输入是奇数还是偶数。

    【讨论】:

      【解决方案4】:

      您正在使用 x 遍历循环索引,并且您正在检查这个数字是偶数、奇数还是负数。如果我理解正确的问题,您需要处理输入的数字。请尝试以下操作:

      for(x= 0;x<10;++x){     
      int num = scan.nextInt();
          if(num%2 == 0 && num>0){
              evenList[x] = num;
              even++;
          } 
          else if(num%2 != 0){
              oddList[x] = num;
              odd++;
          }else if(num<0){
              negativeList[x] = num;
              neg++;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-04-04
        • 1970-01-01
        • 2010-09-14
        • 2018-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多