mercredi 9 septembre 2015

if/else statement in character type

I am suppose to write a program that will input a year code from 1 to 4 and output year level.

Note: Using if...else statement and year code is in character type. (if Array is better to use, I am not allowed to use array since this is in the conditional control structures. And after doing that, how can I write this program using switch....case statement?

How can I declare char and get input from the user?

import java.util.Scanner;
import java.io.*;

public class CharStatement {

    public static void main(String[] a) {

    char userInput = new char;
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter year code: ");  
    System.out.println("");

    if (char == 1) {
    System.out.println("First Year");
    System.out.println("Freshmen");     
    }
    else if ( char == 2) {
        System.out.println("Second Year");
        System.out.println("Sophomore");            
    }
    else if (char == 3) {
        System.out.println("Third Year");
        System.out.println("Junior");   
    }
    else if (char == 4) {
        System.out.println("Fourth Year");
        System.out.println("Senior");   
    }
    else {
        System.out.println("Invalid");
    }

}

Aucun commentaire:

Enregistrer un commentaire