java-将boolean更改为yes [英] java -change boolean to yes no

查看:76
本文介绍了java-将boolean更改为yes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布尔数组,需要稍后将它们显示给用户,而不是显示true或false,我想显示yes或no.有人可以帮我吗?

I have a boolean array and need them displayed to the user later, as opposed to showing true or false I'd like to display yes or no. Could someone please help me on this?

    public static boolean[] seen (boolean[] birds)
    {   String quit = "100";
        String ans = "";
        while(!ans.equals(quit))
        {   ans=JOptionPane.showInputDialog(null,"Which bird are you reporting? \n   1) Blue Tit\n   2) Blackbird\n   3) Robin\n   4) Wren\n   5) Greenfinch");
            if (ans.equals("1"))
            {   birds[0]=true;
            }
            else if (ans.equals("2"))
            {   birds[1]=true;
            }
            else if (ans.equals("3"))
            {   birds[2]=true;
            }
            else if (ans.equals("4"))
            {   birds[3]=true;
            }
            else if (ans.equals("5"))
            {   birds[4]=true;
            }
        }
        return birds;   
    }
public static void display(boolean[] newbirds)
    {   JOptionPane.showMessageDialog(null,"newbirds[0]+" "+newbirds[1]+" "+newbirds[2]+" "+newbirds[3]+" "+ newbirds[4]+"");
    }
}

我希望将newbirds [i]数组显示为yes或no格式,有人可以帮忙吗?

I would like the arrays newbirds[i] displayed as yes or no format, could someone help?

推荐答案

这可用于遍历数组并显示"yes"或"no":

This can be used to loop over the array and print "yes " or "no " :

            boolean[] newbirds = {false, true, true};
            StringBuilder sb = new StringBuilder();
            for(boolean bird : newbirds){
                sb.append(bird? "yes " : "no "); 
            }
            System.out.println(sb.toString());

这篇关于java-将boolean更改为yes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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