给出一串数字,判断奇数偶数的个数
一、实例
1.C语言
#include<stdio.h>
int main(){
int array[6];
int count1=0;
int count2=0;
printf("输入6个数: ");
for(int i=0 ;i<6 ; i++){
scanf("%d",&array[i]);
if(array[i] % 2 == 0)
count1++;
else
count2++;
}
printf("The number of even: %d\n", count1 );
printf("The number of odd: %d", count2 );
return 0;
}
2.Java
package hello;
import java.util.Scanner;
public class EvenOddNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] array = new int[6];
int count1=0;
int count2=0;
for(int i=0;i<array.length;i++) {
array[i]=input.nextInt();
if(array[i]%2==0)
count1++;//the number of even
else
count2++;//the number of odd
}
System.out.println("The number of even: "+count1);
System.out.println("The number of odd: "+count2);
}
}
3.Python
4.C++
#include<iostream>
using namespace std;
int main(){
int array[6];
int count1=0;
int count2=0;
cout << "输入6个数: ";
for(int i=0 ;i<6 ; i++){
cin >> array[i];
if(array[i] % 2 == 0)
count1++;
else
count2++;
}
cout<<"The number of even: " << count1 << endl;
cout<<"The number of odd: " << count2 << endl;
system("pause");
return 0;
}
5.JavaScript
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript 数组</h1>
<p id="demo"></p>
<script>
var text=[6,13,8,24,9,28];
var i;
var count1=0;
var count2=0;
for(i=0;i<6;i++){
if(text[i]%2==0)
count1++;
else
count2++;
}
document.getElementById("demo").innerHTML = "The number of even: " +
count1 + "<br>" + "The number of odd: " + count2;
</script>
</body>
</html>
二、五种语言关于数组使用的异同点:
相同点:
1、五种语言的数组都是以"[ ]"的形式来表示的,并且通过具体的下标都可以得到 数组中的值。
2、数组的下标都是从0开始的。
3、数组名中存放着第一个数组元素的地址。
不同点:
1、Python和js的数组长度都是不固定的,可以随时的加入一些元素,但是C,C++,Java中的数组一旦定义了元素个数之后就无法改变,若是超出原有的长度进行访问会出现,下标越界的错误。
2、C/C++语言的数组没有求数组长度的方法,而Java/JS/python都有专门的直接求出数组长度的方法,所以非常方便。但是C/C++有strlen方法直 接可以得到字符串的长度。
3、五中语言的数组定义方式一点区别,C/C++数组定义的方式是一样的,直接定义数组的变量类型和数组长度,如 int a[10];定义的过程可以不用 初始化,Java定义方式:int [] a = new int[10] 或者 int [] a = {1,2,3,4,5};但是java 中数组的定义一定要初始化.python数组的定义 a = [] 直接可以将一个空数组/列表存放在一个列表变量当中,js定义 var a = {1,2,3,4,5}。
4、输出方式,python,Java,js可以直接用一个数组的名称直接将整个数组输出,但是C/C++不行,数组的输出通常要经过一个循环。
5、js,python中同一个数组中的值类型可以不一致,也就是说一个数组内可以有整型值、字符串型等等,但是C/C++/JAVA中一个数组内的类型必须一致,因为在数组定义的时候就确定了这个数组整体的类型,一旦确定便不能改变。
参考:https://blog.youkuaiyun.com/dingjigang3504/article/details/102379400