I am currently writing a program (java) that asks users for their class schedule, and how many credits each class is. The only valid answers for credits should be 1, 2, 3, or 4. How would I check to make sure these are the only valid ones? If they input an invalid amount, I want it to loop and prompt them for the exact same question. Here is what I have so far.
//Jake Petersen
import java.util.Scanner;
public class test{
//I promise not to make all methods static in CS1
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("How many courses are you going to list?");
int courses = Integer.parseInt(scan.nextLine());
String courseArray[] = new String[courses];
for (int i = 0; i < courseArray.length; i++){
System.out.println("Please enter a course:");
courseArray[i] = scan.nextLine();
}
int creditArray[] = new int[courses];
for (int i = 0; i < creditArray.length; i++){
System.out.println("Please enter how many credits " + courseArray[i] + " is:");
creditArray[i] = scan.nextInt();
}
int sum = 0;
for (int i : creditArray){
sum += i;
}
for (int i = 0; i < courseArray.length; i++) {
System.out.print(courseArray[i] + " is a " + creditArray[i] + " credit class. \n");
}
print(sum);
}
public static void print(int sum){
if(sum >= 12 && sum <= 18){
System.out.println("You are taking " + sum + " total credits, which makes you a full time student.");
}else if(sum < 12){
System.out.println("You are taking " + sum + " total credits, which makes you not a full time student.");
}else{
System.out.println("You are taking " + sum + " total credits, which means you are overloaded");
}
}
}