Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Re type the code and fix any errors. The code should convert negative numbers to

ID: 3736564 • Letter: R

Question

Re type the code and fix any errors. The code should convert negative numbers to 0.

if (userNum >= 0)

__________________________________________

import java.util.Scanner;

public class ConvertNegative {
public static void main (String [] args) {
int userNum = 0;

if (userNum >= 0)
System.out.println("Non-negative");
else
System.out.println("Negative; converting to 0");
userNum = 0;
System.out.format("Final: %d", userNum);
System.out.println("");


return;
}
}

Re type the code and fix any errors. The code should convert negative numbers to 0.

if (userNum >= 0)

     System.out.println("Non-negative");  else     System.out.println("Negative; converting to 0");     userNum = 0;  System.out.format("Final: %d", userNum);  System.out.println(""); 

__________________________________________

import java.util.Scanner;

public class ConvertNegative {
public static void main (String [] args) {
int userNum = 0;

if (userNum >= 0)
System.out.println("Non-negative");
else
System.out.println("Negative; converting to 0");
userNum = 0;
System.out.format("Final: %d", userNum);
System.out.println("");


return;
}
}

Explanation / Answer

Find the below update code as per the requirement. Java Scanner class has been used to get the user input.

/*****************/

import java.util.Scanner;
public class ConvertNegative{

public static void main(String []args){
  
Scanner scanInput=new Scanner(System.in);  

System.out.println("Enter any Number");  
  
int userNum= scanInput.nextInt();

if (userNum >= 0)
System.out.println("Non-negative");
else{
System.out.println("Negative; converting to 0");
userNum = 0;
}
System.out.format("Final: %d", userNum);
System.out.println("");
  
scanInput.close();

return;
}
}

/************/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote