简体   繁体   English

数组错误在给定条件下有多少奇数和偶数

[英]array error how many odd and even numbers in given terms

  1. I want to output it like so我想像这样 output 在此处输入图像描述
  2. my compiler doesn't prompt errors in code I use netbeans apache我的编译器没有提示代码错误 我使用 netbeans apache
  3. when i put how many numbers当我输入多少个数字时在此处输入图像描述 ====================================================== ==================================================== ====

        public class NewClass 
        {
          public static void main(String[] args)
          {
            int n = 0;
            int EvenNo;
            int OddNo;
            Scanner sc = new Scanner(System. in);
     
            System.out.println("How many numbers will be entered?");
            n = sc.nextInt();
            int a[]=new int[n];
        
            //I think Im missing some code here

            System.out.println("Enter " + n + " Elements Separated by Space >> " );
            String input;
            input = sc. nextLine();
            String[] tokens = input.split(" ");
            int[] inputNumbers = new int[tokens.length];
 
            EvenNo = OddNo = 0;

            for(int i = 0; i < tokens.length; i++)
            {
               inputNumbers[i] = Integer.parseInt(tokens[i]);

               if(inputNumbers[i] % 2 == 0 && inputNumbers[i] != 0)
               EvenNo++;
 
               else if(inputNumbers[i] % 2 != 0)
               OddNo++;
            }
               System.out.print("\n");
               System.out.println("The number of Even Numbers >> " + EvenNo);
               System.out.print("The number of Odd Numbers >> " + OddNo);
          }
        }    

You should convert the string format of scanner input to an integer, using Integer.parseInt()您应该使用Integer.parseInt()将扫描仪输入的字符串格式转换为 integer

....
System.out.println("How many numbers will be entered?");
n = Integer.parseInt(sc.nextLine());

...

Here is the problem.这是问题所在。 You have this...你有这个...

System.out.println("How many numbers will be entered?");
n = sc.nextInt();

...which is later followed by this... ...后来是这个...

input = sc. nextLine();

Long story short, nextLine() does not play well with the other next***() methods of the scanner.长话短说, nextLine()不能很好地与扫描仪的其他next***()方法配合使用。 You can read here for more info - https://stackoverflow.com/a/13102066/10118965您可以在此处阅读更多信息 - https://stackoverflow.com/a/13102066/10118965

The easiest way to solve your problem would be to do this解决您的问题的最简单方法是执行此操作

System.out.println("How many numbers will be entered?");
n = sc.nextInt();
sc.nextLine();

That's probably the least painful way to solve this.这可能是解决这个问题的最不痛苦的方法。 But in the future, you can avoid this problem entirely if you only use nextLine() .但是在未来,如果你只使用nextLine()就可以完全避免这个问题。 Those convenience methods are nice, but as you see, they can cause you problems if you don't know their particular nuances.这些方便的方法很好,但是如您所见,如果您不了解它们的特殊细微差别,它们可能会给您带来麻烦。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM