I should Define four boolean variables as follows:
the user enters the course code, then I decide which level is the student (user) and then define the 4 boolean variables depending on the level.
But I don't know how to do the equal()
for two thing or more.
this is my code:
import java.util.*;
public class point8 {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// declaring
String CourseCode, Level;
boolean Freshman, Sophomore, Junior, Senior;
// input
System.out.println("Course code:");
CourseCode = input.next().toUpperCase();
System.out.println("\nCourse Code: " + CourseCode);
// output
Level = CourseCode.substring(CourseCode.length() - 1);
System.out.println("Student Level: " + Level);
Freshman = Level.equals("1");
System.out.println("Freshman: " + Freshman);
Sophomore = Level.equals("3");
System.out.println("Sophomore: " + Sophomore);
Junior = Level.equals("6");
System.out.println("Junior: " + Junior);
Senior = Level.equals("9");
System.out.println("Senior: " + Senior);
}
}
What shall I do to compare from level 1 or 2 for freshman and compare from level 3 to 5 for Sophomore ?
It seems to me that you're better of using integers, just parse the String
to int
.
For example:
int myLevel = Integer.parseInt(Level);
if(myLevel >= 3 && myLevel <= 5)
{
System.out.println("Sophomore: " + Sophomore);
}
You might get an error if the user inserts a letter instead of a number, to avoid this you need to catch the exception and handle it. This however is an entire different story, but you should readup about it: https://docs.oracle.com/javase/tutorial/essential/exceptions/
if(Level.equals("9") || Level.equals("10"))
{
//Senior
}
Update: The OR operator is something you should learn in the first couple weeks. The only thing more basic is to just write out the second if statement.
if(Level.equals("9"))
{
//Senior
}
else if(Level.equals("10"))
{
//Senior
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.