逻辑判断_课后练习

1.1.png
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 智能闹钟
*/
public class Itheima{
public String alarmClock(int a ,boolean b){ //输入的a为星期,b代表是否处于假期
if((a!=0&&a!=6)&&!b){ //当不处于假期和周末时的情况
return "7:00";
}else if((a==0||a==6)&&b){ //当处于假期且为周末的情况
return "off";
}else
return "10:00";
}
}
2.2.png
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 蓝色的彩票
*/
public class Itheima{
public int blueTicket(int a,int b, int c){ //三个数字的输入
if(a+b==10||a+c==10||b+c==10){ //abc其中任意一对的和(如a+b)为10,输出10
return 10;
}else if((a+b)-(b+c)==10||(a+b)-(a+c)==10){ //如果ab的和减去bc的和等于10,或者ab的和减去ac的和等于10,则返回5.
return 5;
}return 0;
}
}
3.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 交警查车
*/
public class Itheima{
public int caughtSpeeding(int a,boolean b){
if(b){ //生日时,可减少处罚车速5度
a-=5;
}
if(a<=60){ //速度小于等于60则无罚单
return 0;
}else if(60<a&&a<=80){ //在61到80(包括)之间则开小罚单
return 1;
}
return 2; //大于等于81就开大罚单
}
}
4.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 松鼠聚会
*/
public class Itheima{
public boolean cigarParty(int a,boolean b){
if(!b&&(a>60||a<40)){ //不处于周末,雪茄不在数量在40和60之间(含)时,聚会失败
return false;
}else if(b&&a<40){ //处于周末,雪茄数量小于40,聚会失败
return false;
}return true; //其他情况聚会成功
}
}
5.

/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 情侣时尚值
*/
public class Itheima{
public int dateFashion(int a,int b){ //a,b两人开房
if(a<=2||b<=2){ //两人中有人太丑,房东不收
return 0;
}else if(a>8||b>=8){ //没有人丑到恶心房东的情况下,有一个颜值过硬,房东直接通过
return 2;
}else
return 1;
}
}
6.
public class Itheima{
public String fizzString(String a){
String a1 = a.substring(0,1); //取到a的第一个符号
String a2 = a.substring(a.length()-1,a.length()); //取到a的最后一个符号
if(a1.equals("f")&&a2.equals("b")){ //既以“f”开头、又以“b”结尾则返回“FizzBuzz”
return "FizzBuzz";
}
else if(a1.equals("f")){ //以“f”开头则返回“Fizz”
return "Fizz";
}
else if(a2.equals("b")){ //以“b”结尾则返回“Buzz”
return "Buzz";
}
return a; //都不是,返回字符串本身
}
}
7.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 指定字符返回2
*/
public class Itheima{
public String fizzString2(int a){
int a1 = a%3; //取a除3和5的模
int a2 = a%5;
if(a1==0&&a2==0){ //既能被3整除又能被5整除
return "FizzBuzz!";
}else if(a1==0){ //如果数字可以被3整除,则返回"Fizz!"
return "Fizz!";
}else if(a2==0){ //如果数字能被5整除就返回"Buzz!"
return "Buzz!";
}
return a+"!";
}
}
8.

/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 绿色的彩票
*/
public class Itheima{
public int greenTicket(int a,int b,int c){
if(a==b&&b==c&&a==c){ //所有的数字都相同则返回20
return 20;
}else if(a==b||b==c||a==c){ //如果只有两个数字相同则返回10。
return 10;
}return 0; //三个数都不相同则返回0
}
}
9.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 外部模式
*/
public class Itheima{
public boolean in1To10(int a, boolean b){
if(b!=true&&a>=1&&a<=10){ //b为假,整型参数在1到10之间方法就返回true
return true;
}else if(b==true&&(a<=1||a>=10)){ //b为真,整型参数小于等于1或者大于等于10时,方法就返回true。
return true;
}else //其他为假
return false;
}
}
10.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 判断三个参数
*/
public class Itheima{
public boolean inOrder(int a,int b,int c,boolean d){
if(b>a&&c>b){ //如果b大于a且c大于b,则返回true
return true;
}else if(c>b&&d==true){ //如果d为true时,b可以不大于a,也返回true
return true;
}else
return false;
}
}
11.

/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 参数递增
*/
public class Itheima{
public boolean inOrderEqual(int a,int b,int c,boolean d){
if(d!=true&&c>b&&b>a){ //d为假时,后面的数字大于前面的数字
return true;
}else if(d==true&&c>=b&&b>=a){ //d为真时,后面的数字大于或等于前面的数字
return true;
}else
return false;
}
}
12.

/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 两个以上的个位相同的数字
*/
public class Itheima{
public boolean lastDigit(int a,int b,int c){
int t1=a%10; //取到abc的个位数
int t2=b%10;
int t3=c%10;
if(t1==t2||t2==t3||t1==t3){ //abc的个位数有两个相同就可以
return true;
}else
return false;
}
}
13.

/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 比20小1或2的余数
*/
public class Itheima{
public boolean less20(int a){
int i = a%20; //取a除20的模
if(i>=18&&i<=19){ //余数比20小1或者2则返回true
return true;
}else
return false;
}
}
14.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 比较大小
*/
public class Itheima{
public boolean lessBy10(int a,int b,int c){
if(Math.abs(a-b)>=10||Math.abs(a-c)>=10||Math.abs(c-b)>=10){
return true;
}else
return false;
}
}
15.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* love6
*/
public class Itheima{
public boolean love6(int a,int b){
if((a==6||b==6)||(a+b==6)||(a-b==6)||(b-a==6)){
return true;
}else
return false;
}
}
16.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 按要求返回值
*/
public class Itheima{
public int maxMod5(int a,int b){
if(a-b==5){
return b;
}else if(b-a==5){
return a;
}else if(a==b){
return 0;
}else if(a>b){
return a;
}else
return b;
}
}
17.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 余数是1或2
*/
public class Itheima{
public boolean more20(int a){
int t = a%20;
if(t==1||t==2){
return true;
}else
return false;
}
}
18.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 近10
*/
public class Itheima{
public boolean nearTen(int a){
int t = a%10;
if((10-t)<=2||t<=2){
return true;
}else
return false;
}
}
19.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 3或5的倍数
*/
public class Itheima{
public boolean old35(int a){
int x=a%3;
int y=a%5;
if((x==0&&y!=0)||(x!=0&&y==0)){
return true;
}else
return false;
}
}
20.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 红色的彩票
*/
public class Itheima{
public int redTicket(int a,int b,int c){
if(a==2&&b==2&&c==2){
return 10;
}else if((a==0&&b==0&&c==0)||(a==1&&b==1&&c==1)){
return 5;
}else if(b!=a&&c!=a){
return 1;
}else
return 0;
}
}
21.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 数字分享
*/
public class Itheima{
public boolean shareDigit(int a,int b){
int x1=a/10;
int y1=a%10;
int x2=b/10;
int y2=b%10;
if(x1==x2||x1==y2||y1==x2||y1==y2){
return true;
}else
return false;
}
}
22.

/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 按要求求和
*/
public class Itheima{
public int sortaSum(int a,int b){
int sum = a+b;
if(sum>=10&&sum<=19){
return 20;
}else
return sum;
}
}
23.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 松鼠玩耍所需的温度值
*/
public class Itheima{
public boolean squirrelPlay(int a,boolean b){
if(a>=60&&a<=90&&b!=true){
return true;
}else if(a>=60&&a<=100&&b==true){
return true;
}else
return false;
}
}
24.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 限制求和
*/
public class Itheima{
public int sumLimit(int a,int b){
int i1;
int i2;
int num = a+b;
int a1=a;
for(i1=0;a1>0;i1++){ //求出a的位数i1
a1=a1/10;
}
for(i2=0;num>0;i2++){ //求出ab和的位数i2
num=num/10;
}
if(i1==i2){
return a+b;
}else
return a;
}
}
25.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 茶果派对
*/
public class Itheima{
public int teaParty(int a,int b){
if(a<5||b<5){
return 0;
}else if(a/b>=2||(double)a/b<=0.5){
return 2;
}else
return 1;
}
}
26.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 青少年的取值范围
*/
public class Itheima{
public int teenSum(int a,int b){
int sum =a+b;
if((sum>=13&&sum<=19)||(a>=13&&a<=19)||(b>=13&&b<=19)){
return 19;
}else
return sum;
}
}
27.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 任意两参数和与第三个参数相等
*/
public class Itheima{
public boolean twoAsOne(int a,int b,int c){
int x=a+b;
int y=a+c;
int z=b+c;
if(x==c||y==b||z==a){
return true;
}else
return false;
}
}
28.
/*
* 传智播客-黑马程序员 http://www.itheima.com
* 为莘莘学子改变命运而讲课,为千万学生少走弯路而著书
* 改变中国IT教育,我们正在行动
* 玩骰子
*/
public class Itheima{
public int withoutDoubles(int a,int b,boolean c){
if(c!=true){
return a+b;
}else if(a==6&&b==6){
return 7;
}else if(a==b){
return a+b+1;
}else
return a+b;
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章