KnowledgeBoat Logo
|

Computer Applications

Write a program by using scanner class to accept a set of positive and negative numbers randomly. Print all the negative numbers first and then all the positive numbers without changing the order of the numbers.

Java

Input in Java

13 Likes

Answer

import java.util.Scanner;

public class KboatNumbers
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Type DONE to Stop Entering Numbers");
        System.out.println("Enter Positive & Negative Numbers Randomly:");
        String posStr = "", negStr = "";
        while(in.hasNextInt()) {
            int i  = in.nextInt();
            if (i < 0)
                negStr += i + " ";
            else
                posStr += i + " ";
        }
        System.out.println(negStr + posStr);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program by using scanner class to accept a set of positive and negative numbers randomly. Print all the negative numbers first and then all the positive numbers without changing the order of the numbers.

Answered By

4 Likes


Related Questions