简体   繁体   中英

How to use Java to write a program that read a sequence of integer inputs?

What is the simplest way to write a program using Java that will read the inputs and print out the smallest and largest number. What about the number of even and odd inputs?

For loop is required, and it needs to allow user to quit the loop. Here is what I got so far (it may be wrong):

import java.util.Scanner;

public class E62a
 {
   public static void main(String[] args)
   {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter numbers");
    int number = in.nextInt();
    int number2 = in.nextInt();
    for(int counter = counter % 2; counter <= 6; counter++)
    {
        System.out.println(number);
        System.out.println(number2);
    }
}        

}

What is the simplest way (...)

In programming, there is hardly ever a "simplest way" to do anything that requires more than a couple of commands. What "simple" means depends on circumstances.


How to write a Java program that will read (console) inputs?

You have already found a way:

Scanner in = new Scanner(System.in);
//...
in.nextLine(); //or nextInt(), or whatever else...

There is also...

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//...
String input = br.readLine();

...which is what I usually use. And there are probably other methods...


How to find/print out the smallest and largest number (between two numbers)?

A simple IF-THEN-ELSE structure will do. In Java this would be:

if (numA <= numB) { min = numA; max = numB; } else {min = numB; min = numA; }

You can also use Java's (very clean and convenient) built-in methods:

min = Math.min(numA,numB); max = Math.max(numA,numB);

You already know how to print... System.out.print() or .println() .


What about the number of even and odd inputs?

I am not sure about what you mean, but if you mean multiple inputs (more than two), you will first have to decide on how the user will enter the inputs; like in a single line, separated by spaces or commas or other kind of separator character , or as multiple lines, in which case you will need a trigger string to tell the program that the inputs have ended and that processing should continue/follow.

In case of single-line + separators, your program will be clean and highly maintainable, because processing of input is almost completely decoupled from the interface (console), but you will need to parse the resulting String to separate inputs from each-other.

In case of multi-line + trigger, you will be able to populate an array with individual inputs as they are typed, but the console gets a lot of clutter, and is considerably more coupled to the processing.


Last but not least, the best way to learn a programming language is by:

  1. Reading through the official documentation. Java 8 docs
  2. Googling when you're in doubt (trouble in programming is like rule-34; someone has had the same issue, and has posted the answer somewhere online).
  3. Experimenting. Practice makes perfect, because it allows you to experience and understand both the issues and their solutions, first-hand.
  4. Asking. Even if you don't find answers anywhere, and/or can't find a solution on your own experimentation, the programming community, and specially we here on SO (I think), are usually great people with open-minds, who are willing to help you find answers to your doubts; just don't try to push your work onto us, or expect a solution that you can copy-&-paste to your project/homework-assignment.

Cheers, and good luck!

What are you trying to do with int counter = counter % 2 ? This essentially tells the program, "Create an integer that is equal to the remainder of its current value divided by 2." That is not possible. counter = counter % 2 can only work if counter has previously been initialized.

Your for loop does not provide the user with a way to manually exit. To allow the user to manually exit the loop, you'll need to write it to accept user input within the loop and provide an exit condition. Here's one example of how you could do that:

Scanner in = new Scanner(System.in);
Integer input;
ArrayList<Integer> numbers = new ArrayList<>();

for (int i=0; i<=6; i++) {
    System.out.print("Enter a number or enter -1 to exit: ");
    input = in.nextInt();
    if (input != -1) {
        numbers.add(input);
    } else {
        break;
    }
}

This will allow the user to input up to six numbers and have the option to exit. Since numbers is an ArrayList in this example, items can be retrieved from it using the get() function. For instance, System.out.println(numbers.get(0)) would display the first number added to numbers .

Determining the smallest and largest numbers could be done like this:

int largest = -1;
int smallest = -1;
for (int i=0; i<numbers.size(); i++) {
    if (numbers.get(i) > largest) {
        largest = numbers.get(i);
    }
    if ((numbers.get(i) < smallest) || (smallest == -1)) {
        smallest = numbers.get(i);
    }
}

This section of code will count the number of even and odd inputs:

int evenNumbers = 0;
int oddNumbers = 0;
for (int i=0; i<numbers.size(); i++) {
    if ((numbers.get(i) % 2) == 0) {
        evenNumbers++;
    } else {
        oddNumbers++;
    }
}

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