Question

In: Computer Science

Make a program that lets the user enter a positive or negative integer as many times...

Make a program that lets the user enter a positive or negative integer as many times as they want. java

//import statement

//class header

//Begin class scope

//Method header for main.

//Begin method scope.

//Declare input object for Scanner.

//Declare variable called entry initialized to zero.

//Declare variable called response initialized to blank space.

//Prompt "Do you want to enter an integer? Y or N: "

//Store value in correct variable.  

//while header that tests uppercase in response

//Begin scope for while.

//Prompt "Please enter an integer: "

//Store value in correct variable.

//Clear buffer.

//Test if entry is greater than 0

//Begin if scope.

//Print the number with a message that is positive, e.g., "3 is positive."

//End if scope.

//The option in a double-selection structure to handle if condition when false.

//Begin option scope.

//Test if entry is less than 0.

//Begin nested if scope.

//Print the number with a message that is negative, e.g., "-3 is negative."

//End nested if scope.

//The option in a double-selection structure to handle nested if condition when false.

//Begin nested option scope.

//Print “0 is neither positive or negative.”

//End nested option scope.

//End option scope.

//Prompt "Do you want to enter another integer? Y or N: "

//Store value in correct variable.   

//End while scope.

//Exit program.

//End main method scope.

//End class scope.

Solutions

Expert Solution

Look at my code and in case of indentation issues check the screenshots.

---------solution.java----------------

import java.util.*;

public class solution
{
   public static void main(String[] args)
   {
       Scanner in = new Scanner(System.in);   //create scanner object
       int entry = 0;                           //intialize entry to 0
       String response = " ";                   //intialize response to emoty space
      
       System.out.print("Do you want to enter an integer ? Y or N: ");
       response = in.nextLine();               //read user response

       while(response.equals("Y"))               //if response is Y, otherwise exit loop
       {
           System.out.print("Please enter an integer: ");  
           entry = in.nextInt();               //read integer from user
           in.nextLine();                       //clear the buffer
           if(entry > 0)                       //check if entry is positive
           {
               System.out.println(entry + " is positive.");
           }
           else if(entry < 0)                   //check if entry is negative
           {
               System.out.println(entry + " is negative.");
           }
           else                                //entry is 0
           {
               System.out.println(entry + " is neither positive or negative.");
           }
           System.out.print("Do you want to enter another integer ? Y or N: ");
           response = in.nextLine();           //read next response
       }
   }
}

----------Screenshots--------------

----------Output--------------

----------------------------------------

Do comment if you need any clarification.
Please let me know if you are facing any issues.
Please Rate the answer if it helped.

Thankyou


Related Solutions

Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer to quit):” and store this into an int named n. If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the sum of the inverse factorials from 0 to n, that is sum = 1/0! + 1/1! + 1/2! + ....
A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
Write a new program named Bar that prompts the user to enter a positive integer. The...
Write a new program named Bar that prompts the user to enter a positive integer. The program should then display a line consisting of the entered number of asterisks using a while loop. If the user enters a number that is not positive, the program should display an error message (see example below). Example 1: Enter a positive number: 6 ****** Example 2: Enter a positive number: 11 *********** Example 3: Enter a positive number: -4 -4 is not a...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a C++ program that keeps asking user to enter a positive integer number until a...
Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered. Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.) An example of executing such a program is shown below. Note that the user input is...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
Write a java program that asks user to enter a set of positive integer values. When...
Write a java program that asks user to enter a set of positive integer values. When the user stops (think of sentinel value to stop), the program display the maximum value entered by the user. Your program should recognize if no value is entered without using counter.
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT