奇/偶输出程序仅给出1个答案 [英] Program for Odd/Even output only giving 1 answer

查看:52
本文介绍了奇/偶输出程序仅给出1个答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在一个作业上,需要程序接受多个数字,然后如果输出为奇数,则输出是",如果输出为偶数,则输出否",并且不知道如何使程序接受多于1个int,然后输出正确的println. 到目前为止,这是我的代码.

I'm stuck on an assignment needs the program to accept multiple numbers and then output "Yes" if odd and "No" if even, and can't figure out how to make the program accept more than 1 int and then output the correct println. This is my code so far.

import java.util.Scanner;
class odd{
  public static void main(String args[]){

    Scanner in = new Scanner(System.in);

    int[] numbers = new int[10];
    for(int i = 0; i < 10; ++i) {
      numbers[i] = in.nextInt();

      if(i % 2 == 0 )
         System.out.println("Yes");
      if( i % 2 == 1 )
         System.out.println("No");
    }
  }
}

推荐答案

首先将所有数字存储在数组中.稍后循环.

First store all your numbers in an array. Later loop it.

检查输入的数字if(numbers[i] % 2 == 0),而不是检查像if(i % 2 == 0 )这样的索引值.

Instead of checking index value like if(i % 2 == 0 ), check the entered number if(numbers[i] % 2 == 0).

  int[] numbers = new int[10];

  //storing all the numbers in an array
  for(int i = 0; i < 10; ++i)
      numbers[i] = in.nextInt();

  //checking each one
  for(int i = 0; i < numbers.length; ++i) {
     if(numbers[i] % 2 == 0 )
         System.out.println("No");//even
     if(numbers[i] % 2 == 1 )
         System.out.println("Yes");//odd
  }

输出:

1 4 3 6 7 8 9 0 2个 3 不 是的 不 是的 不 是的 不 是的 是的 不

1 4 3 6 7 8 9 0 2 3 No Yes No Yes No Yes No Yes Yes No

这篇关于奇/偶输出程序仅给出1个答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆