布尔类型又称逻辑类型,通过关键字boolean来定义布尔类型变量!
代码如下(示例):
boolean b = true //定义布尔型变量b,并赋给初值true代码如下(示例):
public class Mk{ static boolean Shuixian(int x){ boolean b=false; int y; int r=0, s=0,t=0; r=x % 10; s=( x % 100 )/10; t= x / 100; y=r*r*r+s*s*s+t*t*t; if ( x == y ){ b=true; } if ( x != y ){ b=false;} return b; } public static void main(String args[])throws IOException{ int i; String str; BufferedReader buf; buf=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Input an number:"); str=buf.readLine(); i=Integer.parseInt(str); boolean a= Shuixian(i); if (a==true) { System.out.println("该数字是水仙花数字!"); }else{ System.out.println("该数字不是水仙花数字!"); } } }