jeudi 29 décembre 2016

New to java stuck on Switch statement

Hi I want to create a program by using switch statement where the user input for length should be within 1-100. If the user enters a value which is lees than 1 or greater than 100 then an error will be shown [System.out.println("Your number (" + Length + ") is not between 1-100. \nTry again.");]. The program I made is asking for an user input between 1-100 and its giving the output correct when I am entering the number 1 as number 1 is mentioned in case 1. But its not reading the condition that I have mentioned in case 1, when I am giving a different value to check whether the condition is working or not.

package com.company;
import java.util.Scanner;

/**
 * Created by MRIDULA on 27-12-2016.
 */
public class SwitchDemo {

    public static void main(String[] args) {

        Scanner length = new Scanner(System.in);
        System.out.println("Enter a number between 1-100 for length: ");

        int lamba;
        lamba = length.nextInt();

        switch (lamba) {

            case 1: while ((lamba> 100) || (lamba <= 1)) {

                System.out.println("Great, your number for length is between 1 and 100");
            break;}

                case 2: System.out.println("Your number for length is not between 1-100. \nTry again.");
                    break;

                default: System.out.println("Invalid number.\nTry again.");
            }
        }
    }

. Can someone tell me what needs to be done to get the desired result in the below mentioned program?

Aucun commentaire:

Enregistrer un commentaire