而程序显示偶数和奇数

时间:2015-02-24 19:27:28

标签: loops while-loop

好的,所以我的程序应该在一行上显示50到100之间的所有偶数,在整数之间用1个逗号分隔,并对单独行上的奇数执行相同的操作。如何在一条线上获得所有输出?

循环保持打印

所有偶数在50到100:50之间

50到100之间的所有奇数:51

50到100之间的所有偶数:52

50到100之间的所有奇数:53

50到100之间的所有偶数:54

50到100之间的所有奇数:55

应该是

50到100之间的所有偶数:50,52,54,56等......

50到100之间的所有奇数:51,53,55,57等......

这是我的代码

 int count = 50;

  while (count <= 100) {

  if (count % 2 == 0){
     System.out.println("Even numbers between 50 and 100: " + count + " ");
     count ++;
     }
  else if (count % 2 == 1){
     System.out.println("\n Odd numbers between 50 and 100: " + count + " ");
     count ++;
     }
  }
  }
  }

整个程序必须在一个while循环中。

4 个答案:

答案 0 :(得分:0)

#!/bin/bash

#Program for finding odd&evn number between 2 numbers

echo -n "Enter first number : "
read n1

echo -n "Enter second number : "
read n2

#condition For odd numbers 
if [ $n2 -gt $n1 ];
then
  echo Odd Numbers between $n1 and $n2 are : 
  for(( i=$n1; i<=$n2; i++ ))
  do
    test=$(( $i % 2 ))
    if [ $test -ne 0 ]; 
    then
      echo $i
    fi       
  done

  #condition For even numbers 
  echo Even Numbers between $n1 and $n2 are : 
  for(( i=$n1; i<=$n2; i++ ))
  do
    test=$(( $i % 2 ))
    if [ $test -eq 0 ]; 
    then
      echo $i
    fi       
  done
fi 

答案 1 :(得分:-1)

$intro2=("These are the even numbers only between 1 and 100: " . "<br><br>");
echo ($intro2); 
$i = 2;
while($i<101) 
{
    echo $i . ", ";
    $i+=2;
}   

答案 2 :(得分:-1)

$intro2=("These are the even numbers only between 1 and 100: " . "<br><br>");
echo ($intro2); 
    $i = 2;
while($i<101) 

{echo $i . ", ";   $i+=2; } 

$break=("<br><br>");
echo ($break);

$intro=("These are the odd numbers only between 1 and 100: " . "<br><br>");
echo ($intro);  
    $i = 1;
while($i<101) 
{echo $i . ", ";   $i+=2; } 

答案 3 :(得分:-1)

这是简单程序打印均匀&amp;范围50 - 100逗号分隔的奇数

namespace PurushLogics
{
    class Purush_EvenNo
    {
        static void Main()
        {
            int start = 50;
            int end = 100;

            for (int j=start; j <= end; j++)
            {
                if (j % 2 == 0)
                {
                    Console.Write("{0} ,", j);
                }
            } Console.WriteLine();
            for (int j=start; j <= end; j++)
            {
                if (j % 2 != 0)
                {
                    Console.Write("{0} ," , j);
                }
            }  Console.WriteLine();
            Console.ReadLine();


        }

    }
}