Question

This is an if statement how to convert it to swich statement if (academicYear <=5 &&...

This is an if statement how to convert it to swich statement

if (academicYear <=5 && academicYear >=1)

{

if (academicYear == 5)

{

System.out.println("I am a " + YEAR5 + " student. ");

}

else if (academicYear == 4)

{

System.out.println("I am a " + YEAR4 + " student. ");

}

else if (academicYear == 3)

{

System.out.println("I am a " + YEAR3 + " student. ");

}

else if (academicYear == 2)

{

System.out.println("I am a " + YEAR2 + " student. ");

}

else if (academicYear == 1)

{

System.out.println("I am a " + YEAR1 + " student. ");

}

else

{

System.out.println(academicYear + " is not a vaild year.");

System.out.println("Please enter a valid academic year.");

System.out.println("Quitting the program.!!!!");

System.exit(0);

}

}

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

Answer:

switch(academicYear) {

case 5:

System.out.println("I am a " + YEAR5 + " student. ");

break;

case 4:

System.out.println("I am a " + YEAR4 + " student. ");

break;

case 3:

System.out.println("I am a " + YEAR3 + " student. ");

break;

case 2:

System.out.println("I am a " + YEAR2 + " student. ");

break;

case 1:

System.out.println("I am a " + YEAR1 + " student. ");

break;

default:

System.out.println(academicYear + " is not a vaild year.");

System.out.println("Please enter a valid academic year.");

System.out.println("Quitting the program.!!!!");

System.exit(0);

}

Add a comment
Know the answer?
Add Answer to:
This is an if statement how to convert it to swich statement if (academicYear <=5 &&...
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
  • Equipment cost: 262,500 The equipment has a salvage value of $10,000, life expectancy of 5 years....

    Equipment cost: 262,500 The equipment has a salvage value of $10,000, life expectancy of 5 years. Calculate depreciation for all years necessary using the double declining balance. What is the book value at the beginning of year 6? depreciation book-value year1 year2 year3 year4 year5 year6

  • I need the solution step by step for this question please, Consider the following two projects...

    I need the solution step by step for this question please, Consider the following two projects and choose the correct answer: Year 0 Year1 Year2 Year3 Year4 Year5 Year6 Year7 Discount Rate project C/F C/F C/F C/F C/F C/F C/F C/F    Alpha - 79 20 25 30 35 40 N/A N/A 15% Beta - 80 25 25    25 25 25 25 25 16% The internal rate of return (IRR) for project Alpha is closest to: () 24.5% ()...

  • Timber Regrowth. A problem in timber management is to determine how much of an area to...

    Timber Regrowth. A problem in timber management is to determine how much of an area to leave uncut so that the harvested area is reforest in a certain period of time. It is assumed that reforestation takes place at a known rate per year, depending on climate and soil conditions. A reforestation equation expresses this growth as a function of the amount of timber standing and the reforestation rate. For example, if 100 acres are left standing after harvesting and...

  • Java Branches code not working please HELP!! I am trying to build this class that will...

    Java Branches code not working please HELP!! I am trying to build this class that will ask for an ingredient, and the number of cups of the ingredient. It should then branch into an if/else branch off to verify that the number entered is valid and that it is within range. Once that branch completes it should then continue on to ask for the calories per cup, and then calculate total calories. Once I get it to enter the branch...

  • HOW DO I ADD TRY CATCH STATEMENTS TO THIS PIECE OF CODE SO THE APPLICATION DOES...

    HOW DO I ADD TRY CATCH STATEMENTS TO THIS PIECE OF CODE SO THE APPLICATION DOES NOT CRASH case 4: //switch profiles                    if(nop <= 1){                        System.out.println("Theres no profiles to switch to");                        break;                    }                    else{                        System.out.println("Please select a profile");                        int switchpro =...

  • I am creating a program that will allow users to sign in with a username and...

    I am creating a program that will allow users to sign in with a username and password. Their information is saved in a text file. The information in the text file is saved as such: Username Password I have created a method that will take the text file and convert into an array list. Once the username and password is found, it will be removed from the arraylist and will give the user an opportunity to sign in with a...

  • Taking this program how can the requirements under it be added in java? import java.util.Scanner; public...

    Taking this program how can the requirements under it be added in java? import java.util.Scanner; public class RetirementSavings {    private static final int MAX_SALARY = 300000;    private static final double MAX_SAVING_RATE = 0.3;    private static final double MAX_INTEREST_RATE = 0.2;    private static final int MAX_YEAR_EMPLOYED = 40;    public static void main(String[] args) {        double savings_rate, interest_rate;        int salary, years_employed;        String name = ""; // name is initialized with an...

  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

  • Java programming 1. Write a program that reads an unspecified number of integers, determines how many...

    Java programming 1. Write a program that reads an unspecified number of integers, determines how many positive and negative value have been read, and computes the total and average of the input values (not counting zeros. Your program ends with the input 0. Display the average as a floating point number Here are sample runs: (red indicates a user input) Enter an integer, the input ends if it is 0: 1 2 -1 3 0 The number of positives is...

  • Java programming How do i change this program to scan data from file and find the...

    Java programming How do i change this program to scan data from file and find the sum of the valid entry and count vaild and invalid entries The provided data file contains entries in the form ABCDE BB That is - a value ABCDE, followed by the base BB. Process this file, convert each to decimal (base 10) and determine the sum of the decimal (base 10) values. Reject any entry that is invalid. Report the sum of the values...

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