0

in this program it is suppose to act as if anyone can type a number (to represent a class period), and the program will return what class I have in that period. I'm pretty sure my switch and cases are correct, but for some reason, I can't get a re-run to work, it is constantly glitching. The overall goal for this program is to have it ask the user what number they want to enter, have them enter the number, return the class period, and then ask them if they would like to enter another class period (yes/no) and either end the program or continue on. Any ideas of help on that? I'm really stuck. Thanks

//import scanner
import java.util.Scanner;
import java.text.*;
import java.util.Random;

//declare variables and methods (make class "schedule" since there are multiple files)
class Main {
  Scanner scan = new Scanner(System.in);
  int period;
  String response;
  boolean runSchedule = true;

  public void runProgram(){
    while (runSchedule){
      //allow user to input number to represent class
      System.out.println("Please enter any number 1-8 and the class period at that time will print out: ");
      period = scan.nextInt();
      askUser();


      //using switch, case, and breaks for each class on the schedule
        switch (period){
          case 1:
            System.out.println("Precalculus");
            break;
          case 2:
            System.out.println("Java");
            break;
          case 3:
            System.out.println("Honors Chinese");
            break;
          case 4:
            System.out.println("Gym");
            break;
          case 5:
            System.out.println("English");
            break;
          case 6:
            System.out.println("AP World History");
            break;
          case 7:
            System.out.println("AP Physics 1");
            break;
          case 8:
            System.out.println("Music");
            break;
          //this is a default incase user enters number not 1-8 
          default:
            System.out.println("Make sure to select a number 1 through 8 to be able to see class schedule!");
            
        }
    }
  }

  public void askUser(){
    System.out.println("\nDo you want to enter another class period? yes or no: ");
    response = scan.nextLine();
    if (response.equalsIgnoreCase("yes")){
      runSchedule = true;
    }
    else{
      runSchedule = false;
    }
  }

public static void main(String[] args) {
  Main prog = new Main();
  prog.runProgram();
  }
}
  • Since you're working with single words only, just use `next()` instead of `nextLine()`. – Tom Jun 16 '21 at 10:30
  • I've tried that, that ends up messing up the program and not printing the actual class till after. – maxximous Jun 16 '21 at 10:44
  • It's not messing up the program, that's the program flow you've written there. First ask for the class number, then ask if the user wants to continue and after that print the class matching the number. – Tom Jun 16 '21 at 11:01

0 Answers0