Question

How would this switch statement be modified to not use "continue"? This piece of code is...

How would this switch statement be modified to not use "continue"?

This piece of code is part of a larger assignment and that assignment prohibits the use of "continue" for switch statements. How do I modify this code so that it still does what I want it to do, without using "continue"?

while (true) {
choice = displayMenu(keyboard);
switch (choice) {
case 'D':
case 'd': {
courseStudents.displayStudentList();
  
continue;
}
case 'A':
case 'a': {
int id;
double stGpa;
System.out.println("Enter id number of student to add:");
id=keyboard.nextInt();
System.out.println("Enter GPA of student to add:");
gpa=keyboard.nextDouble();
courseStudents.addStudent(id, gpa);
  
continue;
}
case 'C':
case 'c': {
int id;
double StGpa;
System.out.println("Enter id number of student whose GPA will change:");
id=keyboard.nextInt();
System.out.println("Enter new GPA for student:");
gpa=keyboard.nextDouble();
courseStudents.changeStudentGpa(id, gpa);
  
continue;
}
case 'S':
case 's': {
courseStudents.showHighestGpaStudents();
  
continue;
}
case 'U':
case 'u': {
String file = courseNum + "update.txt";
courseStudents.writeUpdatedFile(file);
  
continue;
}
case 'E':
case 'e': {


continue;
}

}
break;
}

Each case represents a menu option for entering data into a program:

MENU

D - Display student list

A - Add a student to list

C - Change a student GPA

S - Show highest GPA students

U - Update student data file

E - Exit program

The user needs be able to make a choice (for example, D) and after that choice is made, be prompted again for another choice, like S, until they exit by entering E.

Thank you and please let me know if there is any missing information!

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

Screenshot

Program

import java.util.Scanner;

public class SwitchCheck {

   public static void main(String[] args) {
       Scanner keyboard=new Scanner(System.in);
       char choice=' ';
       double gpa;
       int id;
       double StGpa;
       choice = displayMenu(keyboard);
       while (choice!='E'&& choice!='e') {
           switch (choice) {
               case 'D':
               case 'd':
                    //courseStudents.displayStudentList();
                    break;
          
           case 'A':
           case 'a':
               System.out.println("Enter id number of student to add:");
                id=keyboard.nextInt();
                System.out.println("Enter GPA of student to add:");
                gpa=keyboard.nextDouble();
                keyboard.nextLine();
                //courseStudents.addStudent(id, gpa);
                break;
          
           case 'C':
           case 'c':
               System.out.println("Enter id number of student whose GPA will change:");
                id=keyboard.nextInt();
                System.out.println("Enter new GPA for student:");
                gpa=keyboard.nextDouble();
                keyboard.nextLine();
                //courseStudents.changeStudentGpa(id, gpa);          
                break;
          
           case 'S':
           case 's':
                //courseStudents.showHighestGpaStudents();          
                break;
          
           case 'U':
           case 'u':
                //String file = courseNum + "update.txt";
                //courseStudents.writeUpdatedFile(file);          
                break;
           default:
                break;
           }
           choice = displayMenu(keyboard);
       }
   }
public static char displayMenu(Scanner keyboard) {
   System.out.println("MENU:");
   System.out.println("D - Display student list\r\n" +
           "\r\n" +
           "A - Add a student to list\r\n" +
           "\r\n" +
           "C - Change a student GPA\r\n" +
           "\r\n" +
           "S - Show highest GPA students\r\n" +
           "\r\n" +
           "U - Update student data file\r\n" +
           "\r\n" +
           "E - Exit program");
   System.out.print("Enter your choice: ");
  
   char ch=keyboard.nextLine().charAt(0);
   return ch;
}
}

---------------------------------------------------

Output

MENU:
D - Display student list

A - Add a student to list

C - Change a student GPA

S - Show highest GPA students

U - Update student data file

E - Exit program
Enter your choice: d
MENU:
D - Display student list

A - Add a student to list

C - Change a student GPA

S - Show highest GPA students

U - Update student data file

E - Exit program
Enter your choice: e

----------------------------------------------------

Note:-

You have to use choice as loop exit,then it will work.

Replace your program with the above program

Add a comment
Know the answer?
Add Answer to:
How would this switch statement be modified to not use "continue"? This piece of code is...
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
  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h...

    Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h (Given. Just use it, don't change it!) Student.cpp (Partially filled, need to complete) 1. Assignment description In this assignment, you will write a simple class roster management system for ASU CSE100. Step #1: First, you will need to finish the design of class Student. See the following UML diagram for Student class, the relevant header file (class declaration) is given to you as Student.h,...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • Project Objectives: To develop ability to write void and value returning methods and to call them...

    Project Objectives: To develop ability to write void and value returning methods and to call them -- Introduction: Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can...

  • If I enter a negative value the program throws an error and the withdrawal amount is...

    If I enter a negative value the program throws an error and the withdrawal amount is added to the balance please help me find why? public abstract class BankAccount { private double balance; private int numOfDeposits; private int numOfwithdrawals; private double apr; private double serviceCharge; //Create getter and setter methods public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getNumOfDeposits() { return numOfDeposits; } public void setNumOfDeposits(int numOfDeposits) { this.numOfDeposits =...

  • In the processLineOfData, write the code to handle case "H" of the switch statement such that:...

    In the processLineOfData, write the code to handle case "H" of the switch statement such that: An HourlyEmployee object is created using the firstName, lastName, rate, and hours local variables. Notice that rate and hours need to be converted from String to double. You may use parseDouble method of the Double class as follows:               Double.parseDouble(rate) Call the parsePaychecks method in this class passing the HourlyEmployee object created in the previous step and the checks variable. Call the findDepartment method...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

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