第三章(迴圈) 編程題,第三章迴圈編程題

來源:互聯網
上載者:User

第三章(迴圈) 編程題,第三章迴圈編程題

1. 奇偶個數(5分)

題目內容:

你的程式要讀入一系列正整數資料,輸入-1表示輸入結束,-1本身不是輸入的資料。程式輸出讀到的資料中的奇數和偶數的個數。

輸入格式:

一系列正整數,整數的範圍是(0,100000)。如果輸入-1則表示輸入結束。

輸出格式:

兩個整數,第一個整數表示讀入資料中的奇數的個數,第二個整數表示讀入資料中的偶數的個數。兩個整數之間以空格分隔。

輸入範例:

9 3 4 2 5 7 -1 

輸出範例:

4 2

我的回答:

import java.util.Scanner;public class Main {    public static void main(String[] args) {        // TODO Auto-generated method stub                Scanner in = new Scanner(System.in);                int i;        int j=0,k=0;                i = in.nextInt();                while (i!=-1)        {            if ( i % 2 == 0 )            {                k = k + 1;//                System.out.println("k="+ k);            }            else            {                j = j + 1;//                System.out.println("j="+ j);            }            i = in.nextInt();         }        System.out.println(j + " " + k);            }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.