we were doing this program in 'C' language and it compiled, runs, perfectly without any logical errors
this is the 'C' Code:
#include <stdio.h> void main() { int age; printf("\nEnter your age: "); scanf("%i", &age); switch (age >= 18) { case 1: printf("You Can Vote"); break; case 0: prinft("You CANNOT Voe!"); break; } }
please try to run it in a "C" compiler, you will see the logic ..
but for good sake I will state it ,
the logic is :
if any number that will be enterd is less than to 18 it will print ("You CANNOTt vote") but if the entered value is GREATER than or equal to 18 then it will print ("You can Vote")
--every of part of this program (in 'C') from compiling through logic doesnt have any errors;
but when i convert this program into java ..here it is
this is the JAVA code:
public class Exercise { private static BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); public static void main(String[] args) throws IOException { int age; System.out.print("Enter your age: "); age = Integer.parseInt(br.readLine()); switch (age >= 18) { case 1: System.out.println("You Can Vote!"); break; case 0: System.out.println("You Cannot Vote!"); break; } } }
you can notice that in 'C' language,even if the entered value is integer it can return a value of a boolean ,
where Case 1: is eqaul to TRUE if the entered value is greater than or equal to 18,
where Case 0: is equal to FALSE if the entered value is less than 18,
but in java,I have noticed that it CANNOT return a boolean value from an integer of a switch..
why is it happening like that?... its perfectly running in 'C' but why it cant in java?..
can anyone help me figure this out.. i really want to convert this in java... same logic... .
please .. need help badly...