集册 Java实例教程 从控制台询问是或否

从控制台询问是或否

欢马劈雪     最近更新时间:2020-01-02 10:19:05

567
从控制台询问是或否


//package com.nowjava;
/**
来 自 NowJava.com
**/

import java.util.Scanner;


public class Main {

    public static void main(String[] argv) throws Exception {

        String query = "nowjava.com";

        System.out.println(askYesOrNo(query));

    }


    public static boolean askYesOrNo(String query) {

        String yesOrNo = null;

        boolean validInput = false;

        while (!validInput) {

            System.out.println(query);


            yesOrNo = getConsoleInput();

            validInput = yesOrNo.toUpperCase().equals("Y")

                    || yesOrNo.toUpperCase().equals("N");

            if (!validInput) {
            /*来自 
             NowJava.com - 时代Java*/

                System.out

                        .println("Invalid input! Please enter /'Y/' or /'N/'");

            }

        }

        return yesOrNo.toUpperCase().equals("Y");

    }


    
展开阅读全文