Question

JAVA Codelab (20940): [Sequential and Nested if/else Statements] Given a int variable named yesCount and another...

JAVA Codelab (20940): [Sequential and Nested if/else Statements] Given a int variable named yesCount and another int variable named noCount and an int variable named response write the necessary code to read a value into into response and then carry out the following: if the value typed in is a 1 or a 2 then increment yesCount and print out "YES WAS RECORDED" if the value typed in is a 3 or an 4 then increment noCount and print out "NO WAS RECORDED" If the input is invalid just print the message "INVALID" and do nothing else. ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Here a new java program with name "Main.java" is created, which contains following code.

Main.java :

import java.util.*;//import package
public class Main //Java class
{
//entry point of program , main method
   public static void main(String[] args) {
   //creating object of Scanner class
   Scanner sc=new Scanner(System.in);
   //declaring variables
   int yesCount=0,noCount=0;
   //asking user to enter value
       System.out.print("Enter response : ");
       int response=sc.nextInt();//reading response
       //checking response
       if(response==1 || response==2)
       {
       //if response is 1 or 2 then
       yesCount++;//increment yesCount
       System.out.println("YES WAS RECORDED");//print
       }
       else if(response==3 || response==4)
       {
       //if response is 3 or 4 then
       noCount++;// increment noCount
       System.out.println("NO WAS RECORDED");//print
       }
       else
       {
       //for any other value print
       System.out.println("INVALID");
       }
   }
}

======================================================

Output : Compile and Run Main.java to get the screen as shown below

Screen 1 :Main.java ,when 1 or 2 is entered

Screen 2 :Main.java ,when 3 or 4 is entered

Screen 3 :Main.java ,when invalid response is entered

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
JAVA Codelab (20940): [Sequential and Nested if/else Statements] Given a int variable named yesCount and another...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Given a int variable named callsReceived and another int variable named operatorsOnCall, write the necessary...

    C++ Given a int variable named callsReceived and another int variable named operatorsOnCall, write the necessary code to read values into callsReceived and operatorsOnCall and print out the number of calls received per operator (integer division with truncation will do). HOWEVER: if any value read in is not valid input, just print the message "INVALID". Valid input for operatorsOnCall is any value greater than 0. Valid input for callsReceived is any value greater than or equal to 0. Revel for...

  • photos arent too go so the answer there is right but im trying to figure put...

    photos arent too go so the answer there is right but im trying to figure put why codelap took operatorsoncall as > and callsrecieved aas >= if that makes any sense. why not both > or >= Workbend M Mathway Math Pr. Rate My Professors Vonida Vol Instructions Display stdout NOTE: In mathematics, division by zero is undefined. So, in C++, division by zero is always an error Given a int variable named calls Received and another int variable named...

  • Write Java statements that swap the element at the position given by the int variable index...

    Write Java statements that swap the element at the position given by the int variable index with the element that is two positions to its left in an array of char values called myChars. Use an if – else statement to make sure that this operation is possible (i.e. that all indexes are valid) and if not outputs a warning message.

  • JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named...

    JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named StringParser with the following: ApublicstaticmethodnamedfindIntegerthattakesaStringandtwocharvariables as parameters (in that order) and does not return anything. The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the firstchar parameter. However, you cannot assume that the parameters will be different from...

  • Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it)...

    Java code Guessing Game Refinement. (The user thinks of a number and the program guesses it) Program should be able to guess correctly in 7 tries or lower. Initialize a variable that represents the lowest possible number to 0 (what type should this be?) Initialize a variable that represent the highest possible number to 100 (what type should this be?) Initialize a Boolean variable that represents if we’ve achieved the correct guess to false Initialize a variable in which to...

  • a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a...

    a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a statement that declares an array namedstreetAddress that contains exactly eighty elements of typechar. 2. In a single statement: declare, create and initialize an arraynamed a of ten elements of type int with the values of the elements (starting with the first) set to 10, 20,..., 100 respectively. 3. Declare an array reference variable, week, and initialize it to an array containing the strings "mon",...

  • This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named...

    This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named Budgeter.java. To use a Scanner for console input, you must import java.util.*; in your code. This program prompts a person for income and expense amounts, then calculates their net monthly income. Below are two example logs of execution from the program. This program’s behavior is dependent on the user input (user input is bold and underlined below to make it stand out and differentiate...

  • This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named...

    This interactive program focuses on if/else statements, Scanner, and returning values. Turn in a file named Budgeter.java. To use a Scanner for console input, you must import java.util.*; in your code. This program prompts a person for income and expense amounts, then calculates their net monthly income. Below are two example logs of execution from the program. This program’s behavior is dependent on the user input (user input is bold and underlined below to make it stand out and differentiate...

  • Debug the following java code so that it will have given out put at the bottom....

    Debug the following java code so that it will have given out put at the bottom. Make sure to fix all syntax and logical errors. import java.util.Scanner; public class Graphs { // draws 5 histograms public void drawHistograms() Scanner input = new Scanner( System.in ); int number1 = 0; // first number int number2 = 0; // second number int number3 = 0; // third number int number4 = 0; // fourth number int number5 = 0; // fifth number...

  • Solve it by JAVA please. Now, write a class named InputSplitter. This class will take input...

    Solve it by JAVA please. Now, write a class named InputSplitter. This class will take input from the keyboard (using a Scanner) and split the incoming tokens into integers, floating point numbers, and strings. The incoming tokens will be added to one of three accumulators which start out at zero, and which keep a running total. Thus, the class will have an accumulator for integers. It starts out with the value zero. Every time another integer is read in, it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT