Switch Learn programming Java

Lesson:

Flow Control


Exercise:

Switch


Objetive:

Create a java program to display the text grade corresponding to a given numerical grade, using the following equivalence:

9,10 = Excellent
7,8 = Very good
6 = Good
5 = Pass
0-4 = Fail

Your program should ask the user for a numerical grade and display the corresponding text grade. You should do this twice: first using "if" and then using "switch".


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int number;

		System.out.print("Number? ");
		number = Integer.parseInt(new Scanner(System.in).nextLine());

		if ((number == 9) || (number == 10))
		{
			System.out.println("Sobresaliente");
		}
		else if ((number == 7) || (number == 8))
		{
			System.out.println("Notable");
		}
		else if (number == 6)
		{
			System.out.println("Bien");
		}
		else if (number == 5)
		{
			System.out.println("Aprobado");
		}
		else if ((number >= 0) && (number <= 4))
		{
			System.out.println("Suspenso");
		}
		else
		{
			System.out.println("No válido");
		}

		switch (number)
		{
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
				System.out.println("Suspenso");
				break;
			case 5:
				System.out.println("Aprobado");
				break;
			case 6:
				System.out.println("Bien");
				break;
			case 7:
			case 8:
				System.out.println("Notable");
				break;
			case 9:
				System.out.println("Bajo, pero... ");
			case 10:
				System.out.println("Sobresaliente");
				break;
			default:
				System.out.println("Nota no válida");
				break;
		}
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.