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

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.”
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. ________...
Write a paragraph for each question no less than 4 sentences, and no more than 10...
Write a paragraph for each question no less than 4 sentences, and no more than 10 sentences. Why is responsibility retained by nurses when tasks are delegated? What are the five individual power sources? Provide an example of each. Why is positive reinforcement such a powerful motivator? How does leadership differ from power? Can a leader be effective without power?
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output...
Re-write following if-else-if statements as Switch statement. Your final code should result in the same output as the original code below. if (selection == 10) System.out.println("You selected 10."); else if (selection == 20) System.out.println("You selected 20."); else if (selection == 30) System.out.println("You selected 30."); else if (selection == 40) System.out.println("You selected 40."); else System.out.println("Not good with numbers, eh?"); 2. Write a Constructor for the TrafficLight class that sets stopLight value to “red”, waitLight to “yellow” and goLight to “green”?
Write an if-else statement with multiple branches. If givenYear is 2101 or greater, print "Distant future"...
Write an if-else statement with multiple branches. If givenYear is 2101 or greater, print "Distant future" (without quotes). Else, if givenYear is 2001 or greater (2001-2100), print "21st century". Else, if givenYear is 1901 or greater (1901-2000), print "20th century". Else (1900 or earlier), print "Long ago". Do NOT end with newline C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT