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;
}
}
/************/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.