SlideShare ist ein Scribd-Unternehmen logo
1 von 3
Given a int variable named yesCount and another int variable named noCount and an int variable
named response that stores an input, write the necessary code to carry out the following:
if the value typed in is a 1 or a 2 then increment yesCount and assign "YES WAS
RECORDED" to state variable.
if the value typed in is a 3 or an 4 then increment noCount and assign "NO WAS RECORDED"
to state variable.
otherwise leave yesCount and noCount as they are and assign "INVALID" to state variable.
-------------
import java.util.Scanner;
class switchdemo{
public static void main(String arg[]){
int response=0;
String state="";
int yesCount=0,noCount=0;
Scanner scanner =new Scanner(System.in);
System.out.println("Enter the number:");
response=scanner.nextInt();
System.out.println("Enter initial yesCount:");
yesCount=scanner.nextInt();
System.out.println("Enter initial noCount:");
noCount=scanner.nextInt();
switch(response){
///{
//write your code here
//start
//end
///}
}//switch
System.out.println("State:"+state);
System.out.println("yesCount after switch:"+yesCount);
System.out.println("noCount after switch:"+noCount);
}
}
Solution
import java.util.Scanner;
class switchdemo {
public static void main(String arg[]) {
int response = 0;
String state = "";
int yesCount = 0, noCount = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number:");
response = scanner.nextInt();
System.out.println("Enter initial yesCount:");
yesCount = scanner.nextInt();
System.out.println("Enter initial noCount:");
noCount = scanner.nextInt();
switch (response) {
case 1:
yesCount++;
state = "YES WAS RECORDED";
break;
case 2:
yesCount++;
state = "YES WAS RECORDED";
break;
case 3:
noCount++;
state = "NO WAS RECORDED";
break;
case 4:
noCount++;
state = "NO WAS RECORDED";
break;
default:
state = "INVALID";
}//switch ends
System.out.println("State:" + state);
System.out.println("yesCount after switch:" + yesCount);
System.out.println("noCount after switch:" + noCount);
}
}

Weitere ähnliche Inhalte

Ähnlich wie Given a int variable named yesCount and another int variable named noC.docx

Write a program Grader that that will be used by a program driver to.pdf
Write a program Grader that that will be used by a program driver to.pdfWrite a program Grader that that will be used by a program driver to.pdf
Write a program Grader that that will be used by a program driver to.pdf
arjunenterprises1978
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
Vince Vo
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
amrit47
 
help me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdfhelp me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdf
arihantmum
 
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdfHello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
flashfashioncasualwe
 
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdfPart 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Lalkamal2
 

Ähnlich wie Given a int variable named yesCount and another int variable named noC.docx (17)

3. control statements
3. control statements3. control statements
3. control statements
 
Write a program Grader that that will be used by a program driver to.pdf
Write a program Grader that that will be used by a program driver to.pdfWrite a program Grader that that will be used by a program driver to.pdf
Write a program Grader that that will be used by a program driver to.pdf
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
informatics practices practical file
informatics practices practical fileinformatics practices practical file
informatics practices practical file
 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
 
An input file A1-txt is given which contains a list of integer values-.docx
An input file A1-txt is given which contains a list of integer values-.docxAn input file A1-txt is given which contains a list of integer values-.docx
An input file A1-txt is given which contains a list of integer values-.docx
 
Array Cont
Array ContArray Cont
Array Cont
 
Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C Lab
 
07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt
 
help me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdfhelp me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdf
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdfHello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
Hello. I need help fixing this Java Code on Eclipse. Please fix part.pdf
 
C code examples
C code examplesC code examples
C code examples
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdfPart 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
 

Mehr von delicecogupdyke

You are the CFO of a large academic medical center and your organizati (1).docx
You are the CFO of a large academic medical center and your organizati (1).docxYou are the CFO of a large academic medical center and your organizati (1).docx
You are the CFO of a large academic medical center and your organizati (1).docx
delicecogupdyke
 
Write the following report that can be a powerpoint presentation-The g.docx
Write the following report that can be a powerpoint presentation-The g.docxWrite the following report that can be a powerpoint presentation-The g.docx
Write the following report that can be a powerpoint presentation-The g.docx
delicecogupdyke
 
Write the definition of the linkedListKeepLast function- (Please write.docx
Write the definition of the linkedListKeepLast function- (Please write.docxWrite the definition of the linkedListKeepLast function- (Please write.docx
Write the definition of the linkedListKeepLast function- (Please write.docx
delicecogupdyke
 

Mehr von delicecogupdyke (20)

You have been learning about Urie Bronfenbrenner this week- Let's mak.docx
You have been learning about Urie Bronfenbrenner this week-  Let's mak.docxYou have been learning about Urie Bronfenbrenner this week-  Let's mak.docx
You have been learning about Urie Bronfenbrenner this week- Let's mak.docx
 
You encounter a flat green organism growing on a rock with no obvious.docx
You encounter a flat green organism growing on a rock with no obvious.docxYou encounter a flat green organism growing on a rock with no obvious.docx
You encounter a flat green organism growing on a rock with no obvious.docx
 
You are the PR specialist for St- John's Ambulance- The organization w.docx
You are the PR specialist for St- John's Ambulance- The organization w.docxYou are the PR specialist for St- John's Ambulance- The organization w.docx
You are the PR specialist for St- John's Ambulance- The organization w.docx
 
You are the CFO of a large academic medical center and your organizati (1).docx
You are the CFO of a large academic medical center and your organizati (1).docxYou are the CFO of a large academic medical center and your organizati (1).docx
You are the CFO of a large academic medical center and your organizati (1).docx
 
You are on a field trip with your bio class- Your water sample from a.docx
You are on a field trip with your bio class- Your water sample from a.docxYou are on a field trip with your bio class- Your water sample from a.docx
You are on a field trip with your bio class- Your water sample from a.docx
 
You are fortunate to travel to a tropical forest- You find an insect o.docx
You are fortunate to travel to a tropical forest- You find an insect o.docxYou are fortunate to travel to a tropical forest- You find an insect o.docx
You are fortunate to travel to a tropical forest- You find an insect o.docx
 
You are a Government employee working in the Human Resources departmen.docx
You are a Government employee working in the Human Resources departmen.docxYou are a Government employee working in the Human Resources departmen.docx
You are a Government employee working in the Human Resources departmen.docx
 
You are a genetic cownselar- A single mother cnmes to vou- where both.docx
You are a genetic cownselar- A single mother cnmes to vou- where both.docxYou are a genetic cownselar- A single mother cnmes to vou- where both.docx
You are a genetic cownselar- A single mother cnmes to vou- where both.docx
 
Write the following report that can be a powerpoint presentation-The g.docx
Write the following report that can be a powerpoint presentation-The g.docxWrite the following report that can be a powerpoint presentation-The g.docx
Write the following report that can be a powerpoint presentation-The g.docx
 
Write two functions using the Front and Back Linear Search algorithms-.docx
Write two functions using the Front and Back Linear Search algorithms-.docxWrite two functions using the Front and Back Linear Search algorithms-.docx
Write two functions using the Front and Back Linear Search algorithms-.docx
 
xpected to grow geometrically- (Assume that interactions ith other spe.docx
xpected to grow geometrically- (Assume that interactions ith other spe.docxxpected to grow geometrically- (Assume that interactions ith other spe.docx
xpected to grow geometrically- (Assume that interactions ith other spe.docx
 
Write the pseudocode for Python for the following- from Celsius to Fah.docx
Write the pseudocode for Python for the following- from Celsius to Fah.docxWrite the pseudocode for Python for the following- from Celsius to Fah.docx
Write the pseudocode for Python for the following- from Celsius to Fah.docx
 
Write the definition of the linkedListKeepLast function- (Please write.docx
Write the definition of the linkedListKeepLast function- (Please write.docxWrite the definition of the linkedListKeepLast function- (Please write.docx
Write the definition of the linkedListKeepLast function- (Please write.docx
 
Write SQL commands that convert the Database schema to Tables with the.docx
Write SQL commands that convert the Database schema to Tables with the.docxWrite SQL commands that convert the Database schema to Tables with the.docx
Write SQL commands that convert the Database schema to Tables with the.docx
 
write the adjusting entries for following Given Data-.docx
write the adjusting entries for following    Given Data-.docxwrite the adjusting entries for following    Given Data-.docx
write the adjusting entries for following Given Data-.docx
 
Write python code to collect 1000 posts from Twitter- or Facebook- or.docx
Write python code to collect 1000 posts from Twitter- or Facebook- or.docxWrite python code to collect 1000 posts from Twitter- or Facebook- or.docx
Write python code to collect 1000 posts from Twitter- or Facebook- or.docx
 
write one to two paragraphs on the significance about Jati Explain how.docx
write one to two paragraphs on the significance about Jati Explain how.docxwrite one to two paragraphs on the significance about Jati Explain how.docx
write one to two paragraphs on the significance about Jati Explain how.docx
 
Write in C++ Calculate the mean of a vector of floating point numbers.docx
Write in C++ Calculate the mean of a vector of floating point numbers.docxWrite in C++ Calculate the mean of a vector of floating point numbers.docx
Write in C++ Calculate the mean of a vector of floating point numbers.docx
 
Write down the code that will count the number of clicks for a LIKE bu.docx
Write down the code that will count the number of clicks for a LIKE bu.docxWrite down the code that will count the number of clicks for a LIKE bu.docx
Write down the code that will count the number of clicks for a LIKE bu.docx
 
Write declarations for variables p1 and p2 whose values will be addres.docx
Write declarations for variables p1 and p2 whose values will be addres.docxWrite declarations for variables p1 and p2 whose values will be addres.docx
Write declarations for variables p1 and p2 whose values will be addres.docx
 

Kürzlich hochgeladen

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

Given a int variable named yesCount and another int variable named noC.docx

  • 1. Given a int variable named yesCount and another int variable named noCount and an int variable named response that stores an input, write the necessary code to carry out the following: if the value typed in is a 1 or a 2 then increment yesCount and assign "YES WAS RECORDED" to state variable. if the value typed in is a 3 or an 4 then increment noCount and assign "NO WAS RECORDED" to state variable. otherwise leave yesCount and noCount as they are and assign "INVALID" to state variable. ------------- import java.util.Scanner; class switchdemo{ public static void main(String arg[]){ int response=0; String state=""; int yesCount=0,noCount=0; Scanner scanner =new Scanner(System.in); System.out.println("Enter the number:"); response=scanner.nextInt(); System.out.println("Enter initial yesCount:"); yesCount=scanner.nextInt(); System.out.println("Enter initial noCount:"); noCount=scanner.nextInt(); switch(response){ ///{ //write your code here //start //end ///} }//switch System.out.println("State:"+state); System.out.println("yesCount after switch:"+yesCount); System.out.println("noCount after switch:"+noCount); }
  • 2. } Solution import java.util.Scanner; class switchdemo { public static void main(String arg[]) { int response = 0; String state = ""; int yesCount = 0, noCount = 0; Scanner scanner = new Scanner(System.in); System.out.println("Enter the number:"); response = scanner.nextInt(); System.out.println("Enter initial yesCount:"); yesCount = scanner.nextInt(); System.out.println("Enter initial noCount:"); noCount = scanner.nextInt(); switch (response) { case 1: yesCount++; state = "YES WAS RECORDED"; break; case 2: yesCount++; state = "YES WAS RECORDED"; break; case 3: noCount++; state = "NO WAS RECORDED"; break; case 4: noCount++;
  • 3. state = "NO WAS RECORDED"; break; default: state = "INVALID"; }//switch ends System.out.println("State:" + state); System.out.println("yesCount after switch:" + yesCount); System.out.println("noCount after switch:" + noCount); } }