In: Computer Science
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.
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