获取要检查和计算的偶数值 [英] Get even values to be checked and counted

查看:50
本文介绍了获取要检查和计算的偶数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是程序识别数字何时均匀以及有多少数字。我有我的int,但它目前什么都不做,但确实有效(显然是0)。了解我,我的源代码可能存在很多问题。



我尝试过:



公共类ArraysAsArguments

{

随机r = new Random();

int [] []数组;

int row;

int col;

static int count;

static int [] [] myArray;

static int value;

static ArraysAsArguments c = new ArraysAsArguments();

static int evenNum;



public int [] [] createArray(int rSize,int cSize)

{

array = new int [rSize] [cSize] ;

for(row = 0; row< array.length; row ++)

{

for(col = 0; col< array [0] .length; col ++)

{

array [row] [col] = r.nextInt(100)+ 1;

}

}

返回数组;

}



public void printArray(int [] [] array)

{

for(row = 0;行< array.length;行++)

{

for(col = 0; col< array [0] .length; col ++)

{

System.out.print(array [row] [col] +\t);

}

System.out.println(\\ \\ n);

}

}



public int countInstance(int [] [] array, int search)

{

count = 0;

for(row = 0; row< array.length; row ++)
{

for(col = 0; col< array [0] .length; col ++)

{

if (array [row] [col] == search)

{

count ++;

}

}

}

返回计数;

}

/ **

* @param args命令行参数

* /

public static void main(String [] args )

{

myArray = c.createArray(25,1);





c.printArray(myArray);

System.out.println(有+ evenNum +偶数);

}

}

I'm having an issue with the program recognizing when a number is even and how many there are. I have my int but it currently does nothing but does work (obviously comes up with 0). Knowing me, there is probably a bunch of issues with my source code.

What I have tried:

public class ArraysAsArguments
{
Random r = new Random();
int[][] array;
int row;
int col;
static int count;
static int[][] myArray;
static int value;
static ArraysAsArguments c = new ArraysAsArguments();
static int evenNum;

public int[][] createArray(int rSize, int cSize)
{
array = new int[rSize][cSize];
for(row = 0; row < array.length; row++)
{
for(col = 0; col < array[0].length; col++)
{
array[row][col] = r.nextInt(100) + 1;
}
}
return array;
}

public void printArray(int[][] array)
{
for(row = 0; row < array.length; row++)
{
for(col = 0; col < array[0].length; col++)
{
System.out.print(array[row][col] + "\t");
}
System.out.println("\n");
}
}

public int countInstance(int[][] array, int search)
{
count = 0;
for(row = 0; row < array.length; row++)
{
for(col = 0; col < array[0].length; col++)
{
if(array[row][col] == search)
{
count++;
}
}
}
return count;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
myArray = c.createArray(25, 1);


c.printArray(myArray);
System.out.println("There are " + evenNum + " even numbers");
}
}

推荐答案

您声明一个名为 evenNum 的变量。您打印出一个名为 evenNum 的变量。但无处为变量 evenNum 赋值。



你会想要类似于你的 countInstance 功能。除了测试数组元素是否等于指定值,你需要测试它是否是偶数。



最简单的方法是使用模运算符:

使用Modulo(%)运算符 - Java教程| Dream.In.Code [ ^ ]
You declare a variable called evenNum. You print out a variable called evenNum. But nowhere do you assign a value to the variable evenNum.

You'll want something similar to your countInstance function. Except instead of testing whether the array element is equal to a specified value, you'll need to test whether it's even.

The simplest way to do that would be using the modulo operator:
The Use Of The Modulo (%) Operator - Java Tutorials | Dream.In.Code[^]


import java.io. *;

class evenNumber

{

public static void main(String args [])

{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int num;

num = Integer.parseIntbr.readLine();

if(num%2 == 0)

{

System.out.println(数字是偶数);

}

其他

{

System.out.println(数字不均匀);

}

}

}









如果有很多数字,那么输入那么多数字并用模数运算符检查......
import java.io.*;
class evenNumber
{
public static void main(String args[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num;
num=Integer.parseIntbr.readLine();
if(num%2==0)
{
System.out.println("The number is even");
}
else
{
System.out.println("The number is not even");
}
}
}




If there are many numbers,then enter that many numbers and check with the modulous operator...


这篇关于获取要检查和计算的偶数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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