In: Computer Science
Write an if-else statement for the following:
If numDifference is less than -20, execute totalDifference = -25.
Else, execute totalDifference = numDifference.
import java.util.Scanner;
public class NumberDifference {
public static void main (String [] args) {
int totalDifference;
int numDifference;
Scanner scnr = new
Scanner(System.in);
numDifference = scnr.nextInt(); //
Program will be tested with values: -19, -20, -21, -22.
//solution goes here//
System.out.println(totalDifference);
}
}
SOURCE CODE:
import java.util.Scanner; //importing java.util.Scanner package for reading input from user
public class NumberDifference {
public static void main (String [] args) {
int totalDifference;
int numDifference;
Scanner scnr = new Scanner(System.in); //Creating Scanner
object
numDifference = scnr.nextInt(); // Program will be tested with
values: -19, -20, -21, -22.
//solution goes here//
if(numDifference<-20) //if numDifference is less
than 20 then
{
totalDifference=-25; //totalDifference=-25
}
else //else
{
totalDifference=numDifference;
//totalDifference=numDifference
}
System.out.println(totalDifference); //printing Final
totalDifference value
}
}
CODE SCREENSHOT:
OUTPUT: