Question

In: Computer Science

Write an if-else statement for the following: If numDifference is less than -20, execute totalDifference =...

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);
   }
}

Solutions

Expert Solution

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:


Related Solutions

Analysis and Discussion about if, if else, else if statement and Switch C programming write on...
Analysis and Discussion about if, if else, else if statement and Switch C programming write on your computer font
Write an essay (not less than 500 words) on the following subject: - The effects of...
Write an essay (not less than 500 words) on the following subject: - The effects of the world epidemic of Corona virus on the Egyptian international companies: the opportunities and threats.
Discuss the statement, "to be less than maximally competent is unethical"
Discuss the statement, "to be less than maximally competent is unethical"
Write a statement of your design ideas in less than one page long (e.g., why you...
Write a statement of your design ideas in less than one page long (e.g., why you need those new luminaires, for what lighting purposes, which lighting effect you want to achieve, and how, etc.). Use some photometric data (e.g., lux, cd/m , CRI, etc.) to support your ideas. Calculation is optional. The photometric data can be obtained from the cut- sheets of the luminaires could you please give at least examples of ideas and what photometric data can I use...
Which of the following statement is correct? Select one: a) Adding less water than required in...
Which of the following statement is correct? Select one: a) Adding less water than required in concrete results in higher workability b) Adding more water than required in concrete results in higher compressive strength c) Adding more water than required in concrete results in more voids and higher porosity
Write a project proposal on this topic, not less than 2,500 words and not more than...
Write a project proposal on this topic, not less than 2,500 words and not more than 3000 words including references - “ Investigating the challenges confronting the performance of small and medium enterprises (SMEs) in Abuja.”
c++ class Topics Branches if statement if/else statement Description Write a program that will determine whether...
c++ class Topics Branches if statement if/else statement Description Write a program that will determine whether the user passed or failed a test. The program will ask the user to input the following: Maximum Test Score - the maximum total scores in the test. Percent Pass Score - the minimum percent scores required to pass the test. User Test Score - the actual scores obtained by the user in the test. From the above input values, the program will compute...
For the following problem, draw a flowchart, write a function and then execute the function with...
For the following problem, draw a flowchart, write a function and then execute the function with your choice of input parameter values: For given two input parameters: If parameter 1 is smaller than parameter 2 return the remainder of parameter 1 by parameter 2 Otherwise, return the multiplication of two input parameters. Python programming language
which of the following occurs when a person focuses on a nearby object (less than 20 ft away)?
which of the following occurs when a person focuses on a nearby object (less than 20 ft away)?a.ciliary zonule tightensb. ciliary muscle contractc. lens flattensd. two of the abovee. all of the above
Consider the following hypothesis test: H0: n greater than or equal to 20 Ha: n less...
Consider the following hypothesis test: H0: n greater than or equal to 20 Ha: n less than 20 a sample of 45 provided a sample mean of 19.6. the population standard deviation is 1.8 a.  Compute the value of the test statistic (to 2 decimals). Enter negative value as negative number. _______ b. what is the p-value? (3 decimals) d. using a=0.05, what is the critical value for the test statistic (to 3 decimals)? Enter negative value as negative number. ________...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT