将 main 方法中的变量与另一个 Java 方法连接

如果参数为偶数,则该方法应返回 true,否则返回 false。程序的 main 方法应该使用循环来生成 100 个随机整数。它应该使用 isEven 方法来确定每个随机数是偶数还是奇数。这一切都完成了!!!


这是我想不通的部分!


循环完成后,程序应显示生成的偶数个数和奇数个数。


这是我的代码:


import java.util.Random;


public class EvenOdd

{


    public static void main(String[] args)

    {


        Random random = new Random();


        int randomInteger = 0;


        for(int i = 0; i < 100; i++){

            randomInteger = random.nextInt();

            System.out.println("Random Integer: " + randomInteger);

            EvenOdd(randomInteger);

        }

    }


    public static void EvenOdd(int x)

    {


        int oddNumbers = 0;

        int evenNumbers = 0;


        if ((x % 2) == 0)

        {

            System.out.println("Even");

            evenNumbers++;


        }

        else

        {

            System.out.println("Odd");

            oddNumbers++;


        }

    }

}


12345678_0001
浏览 118回答 3
3回答

喵喵时光机

尝试用这个:public static void main(String[] args){&nbsp; &nbsp; Random random = new Random();&nbsp; &nbsp; int randomInteger = 0;&nbsp; &nbsp; int oddNumbers = 0;&nbsp; &nbsp; int evenNumbers = 0;&nbsp; &nbsp; for(int i = 0; i < 100; i++){&nbsp; &nbsp; &nbsp; &nbsp; randomInteger = random.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Random Integer: " + randomInteger);&nbsp; &nbsp; &nbsp; &nbsp; if(evenOdd(randomInteger)) evenNumbers++;&nbsp; &nbsp; &nbsp; &nbsp; else oddNumbers++;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.printf("Even numbers: %d - Odd numbers: %d", evenNumbers, oddNumbers);}public static boolean evenOdd(int x){&nbsp; &nbsp; if ((x % 2) == 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Even");&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Odd");&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}您原来的方法不起作用,因为每次调用该方法时都会将oddNumbers和变量初始化为 0。evenNumbers

交互式爱情

Java 不是 JavaScript。而且,它不具备C++“函数中的静态变量”的能力。方法内声明的变量是局部的。每次代码到达方法内的变量定义时都会发生变量初始化,并在退出方法后被销毁。https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html所以你有这样的变体:1) 在主方法中计算数字并从实用程序方法返回指示器。1.1) 布尔值public static boolean isEven(int x){&nbsp; &nbsp; return (x % 2) == 0;};1.2) 枚举private enum NumberType {&nbsp; &nbsp; EVEN,&nbsp; &nbsp; ODD}public static NumberType getNumberType (int x) {&nbsp; &nbsp; if ((x % 2) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; return NumberType.EVEN;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return NumberType.ODD;&nbsp; &nbsp; }};2)使变量静态:public class EvenOdd {&nbsp; &nbsp; private static int evenNumbersCount = 0;&nbsp; &nbsp; private static int oddNumbersCount = 0;&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // your code&nbsp; &nbsp; }&nbsp; &nbsp; public static void countNumberType (int x) {&nbsp; &nbsp; &nbsp; &nbsp; if ((x % 2) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++evenNumbersCount;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++oddNumbersCount;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}3)在一些复杂的情况下,您需要将容器传递给您的方法:public class EvenOdd {&nbsp; &nbsp; private static final String EVEN = "even";&nbsp; &nbsp; private static final String ODD = "odd";&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // initialize container&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Integer> evenOddCounts = new HashMap<>(2, 1);&nbsp; &nbsp; &nbsp; &nbsp; evenOddCounts.put(EVEN, 0);&nbsp; &nbsp; &nbsp; &nbsp; evenOddCounts.put(ODD, 0);&nbsp; &nbsp; &nbsp; &nbsp; Random random = new Random();&nbsp; &nbsp; &nbsp; &nbsp; int randomInteger = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 100; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; randomInteger = random.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countNumberType(evenOddCounts, randomInteger);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(evenOddCounts.toString());&nbsp; &nbsp; }&nbsp; &nbsp; public static void countNumberType(Map<String, Integer> counts, int x) {&nbsp; &nbsp; &nbsp; &nbsp; if ((x % 2) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counts.compute(EVEN, (numberType, count) -> ++count);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counts.compute(ODD, (numberType, count) -> ++count);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕村225694

将 oddNumbers、evenNumbers 变量定义为静态类变量,在循环之后您可以打印这 2 个值。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java