简体   繁体   中英

Why does my evenOdd code produce an output?

I'm a first year university student starting my computer science major so sorry for any rookie mistakes. We've just gotten to if/else statements and practice mostly on this website called "Practice-It!", a coding practice website for Java, C++, and Python although I'm currently learning Java. Now, I recently got to this problem called "evenOdd" in which we need to read an integer from the user and print "even" if its an even number or print "odd" if it's an odd number. The exact problem goes as follows:

Write Java code to read an integer from the user, then print even if that number is an even number or odd otherwise. You may assume that the user types a valid integer. The input/output should match the following example:

Type a number: 14
even

I'm pretty sure I know how to do this, but when I enter in my bare code, it produces no output. I'm unsure of why. My code goes as follows:

int number;
Scanner console = new Scanner(System.in);
System.out.print("Type a number: ");
number = console.nextInt();
if (number % 2 == 0) {
    System.out.println("even");
} else  if (number % 2 != 0) {
    System.out.println("odd");
}

I should mention I'm supposed to put in bare code, meaning no class or methods.

I'm not sure if it's just me or maybe the website's slightly faulty. Any help is much appreciated.

Your code is sound, but if that is all the code you submit to the compiler then it won't work. You need to import the Scanner class from java.util.Scanner and you need to run your code inside a function and a class, unlike python which can run free in an IDE or console. Here is the code that worked for me.

import java.util.Scanner;

public class temp{

public static void main(String [] args){

    int number;
    Scanner console = new Scanner(System.in);
    System.out.print("Type a number: ");
    number = console.nextInt();
    if (number % 2 == 0) {
        System.out.println("even");
    } else  if (number % 2 != 0) {
        System.out.println("odd");
    }
}


}

Hope that helps.

I would suggest you use some IDE like Eclipse or NetBeans. These IDEs will help you in writing and debugging your code. They will also mark errors in your code and also provide description and quick fix which will help you code.

package abc.xyz.test;

import java.util.Scanner;

public class EvenOdd 
{
public static void main(String... args)
{
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a number: ");
    if (input.nextInt() % 2 == 0) 
    {
        System.out.println("even");
    } 
    else 
    {
        System.out.println("odd");
    }
    input.close();
}
}

You can download eclipse from https://www.eclipse.org/downloads/ and NetBeans from https://netbeans.org/downloads/ . Both of them are free, you don't have to pay anything for using them.

You need to import the scanner from java.util package. In every java program there must be a main method. try the following code. Look at my code i am creating an instance of Scanner named userInput and at the end of my code i am calling the close() method to prevent resource leak. you will not get any error for not closing but it is part of good practice.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int number = 0;
        Scanner userInput = new Scanner(System.in);
        System.out.println("Type a number");
        number = userInput.nextInt();

        if( number % 2 == 0 ) {
            System.out.println("Even");
        } else {
            System.out.println("Odd");
        }
        userInput.close();

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM