Você está na página 1de 3

[] J A V A

LAB

JavaLab_3

SELECTION

To demonstrate the use of if and switch operators.

%Lab3_1:
Write a Java program that accept an integer from input and reports whether it is divisible by 5, or it is an even number. [Hint: n is divisible by d if the remainder from dividing n by d is 0.] import java.util.Scanner; public class AdditionQuiz{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter an integer: "); int number = input.nextInt();

if (number % 5 == 0)
System.out.println("Hi Five " + number);

if (number % 2 == 0)
System.out.println(number + is Even ");

else
{ System.out.print( Your number : "); System.out.println( number + is Odd "); } } }

%Lab3_2:
To demonstrates the use of a switch statement. Reads a grade from the user and prints comments accordingly. import java.util.Scanner; public class GradeReport{ public static void main (String[] args) { int grade, category; Scanner scan = new Scanner (System.in); System.out.print (" Enter a numeric grade (0 to 100): ");
------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl 1

[] J A V A

LAB

grade = scan.nextInt(); category = grade / 10; System.out.print ("That grade is ");

switch (category)
{

case 10:
case 9: case 8: case 7: case 6:

System.out.println ("Perfect score. Well done.");

break;
System.out.println ("Above average. Excellent."); break; System.out.println ("above average. Nice job."); break; System.out.println ("average."); break; System.out.print ("below average. Please see the "); System.out.println ("instructor for assistance."); break;

default:
} } }

System.out.println ("not passing.");

%Lab3_3:
Here is an example that creates 10 letters randomly and determines whether they are vowels or consonants: public class VowelsAndConsonants { public static void main(String[] args) { for(int i = 0; i < 10; i++) { char c = (char)(Math.random() * 26 + 'a') ; System.out.print(c + ": "); switch(c) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println(c + "is vowel");
------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl 2

[] J A V A

LAB

break; case 'y': case 'w': System.out.println(c + "is sometimes a vowel"); break; default: System.out.println(c + " is consonant"); } // switch } // for } }

Try this

Write a program that prompts the user to enter the number of students and each students score. Finally display the highest score. Write a program to read the number and find if it is divisible by 2, 3, and 5.

------------------------------------------------------------------------------------------------------------------------------------------------Dr Mohammed Fadhl 3