是/否布尔或的if / else? [英] Yes/No with boolean or if/else?

查看:130
本文介绍了是/否布尔或的if / else?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建一个是/我的程序没有输入的问题。我希望用户能够输入Y / N后的最后一个print语句,但我不能肯定我将如何实现它。我知道这很可能会包括一个布尔值或if / else语句,但我不能肯定。任何帮助吗?

 进口的java.util。*;
进口java.io. *;
进口java.util.Scanner中;公共类Person
{
公共静态无效的主要(字串[] args)
{    INT年龄;
    字符串名称;    扫描程序扫描=新的扫描仪(System.in);    的System.out.println(输入你的年龄。);
    年龄= scan.nextInt();        如果(年龄> = 10安培;&安培;年龄< 18)
        {
            的System.out.println(这么说你是一个孩子,是吧?);
        }        否则如果(年龄小于10)
        {
            的System.out.println(不错的尝试。);
            System.exit(0);
        }
        否则,如果(年龄> = 18安培;&安培;年龄< = 100)
        {
            的System.out.println(这么说你是一个成年人,是吧?);
        }
        否则如果(年龄大于100)
        {
            的System.out.println(不错的尝试。);
            System.exit(0);
        }        扫描仪在=新的扫描仪(System.in);
        的System.out.println(输入你的名字);
        名称= in.nextLine();        的System.out.println(这么说你是+年龄+岁,你的名字叫+姓名+?);        的System.out.println(Y / N);
        }
}


解决方案

 字符串的答案;
布尔YN;的System.out.println(Y / N);
而(真){
  。答案= in.nextLine()修剪()与toLowerCase()。
  如果(answer.equals(Y)){
    YN = TRUE;
    打破;
  }否则如果(answer.equals(N)){
    YN = FALSE;
    打破;
  }其他{
     的System.out.println(对不起,我没有听清楚,请回答Y / N。);
  }
}

I'm having issues creating a yes/no input in my program. I want the user to be able to input a y/n after the last print statement, but i'm not quite sure how I would implement it. I know it would most likely include a boolean or an if/else statement, but i'm not quite sure. Any help?

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

public class Person 
{
public static void main(String[]args) 
{

    int age;
    String name;

    Scanner scan = new Scanner(System.in);

    System.out.println("Enter in your age.");
    age = scan.nextInt();

        if (age >= 10 && age < 18) 
        {
            System.out.println("So you're a kid, huh?");
        } 

        else if (age < 10)
        {
            System.out.println("Nice try.");
            System.exit(0);
        }
        else if (age >= 18 && age <= 100)
        {
            System.out.println("So you're an adult, huh?");
        }
        else if (age > 100)
        {
            System.out.println("Nice try.");
            System.exit(0);
        }

        Scanner in = new Scanner(System.in);
        System.out.println("Enter in your name");
        name = in.nextLine();

        System.out.println("So you're " + age + " years old and your name is " + name + "?");

        System.out.println("y/n");
        }
}

解决方案

String answer;
boolean yn;

System.out.println("y/n");
while (true) {
  answer = in.nextLine().trim().toLowerCase();
  if (answer.equals("y")) {
    yn = true;
    break;
  } else if (answer.equals("n")) {
    yn = false;
    break;
  } else {
     System.out.println("Sorry, I didn't catch that. Please answer y/n");
  }
}

这篇关于是/否布尔或的if / else?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆