首页 > 编程 > Java > 正文

CodeBat Java Warmup-1

2019-11-06 06:27:09
字体:
来源:转载
供稿:网友
1.sleepIn
The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return true if we sleep in.sleepIn(false, false) → truesleepIn(true, false) → falsesleepIn(false, true) → true   答案:
public boolean sleepIn(boolean weekday, boolean vacation) {  if(weekday == true && vacation == false ){    return false;  }else{    return true;  }}2.monkeyTrouble
We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble.monkeyTrouble(true, true) → truemonkeyTrouble(false, false) → truemonkeyTrouble(true, false) → false 答案:
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {  if(aSmile == bSmile){    return true;  }  return false;}3.sumDouble
Given two int values, return their sum. Unless the two values are the same, then return double their sum.sumDouble(1, 2) → 3sumDouble(3, 2) → 5sumDouble(2, 2) → 8答案:
public int sumDouble(int a, int b) {  return (a==b)?2*(a+b):a+b;}4.diff21
Given an int n, return the absolute difference between n and 21, except return double the absolute difference if n is over 21.diff21(19) → 2diff21(10) → 11diff21(21) → 0答案:
public int diff21(int n) {  return (n-21)>0?2*(n-21):Math.abs(n-21);}5.parrotTrouble
We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return true if we are in trouble.parrotTrouble(true, 6) → trueparrotTrouble(true, 7) → falseparrotTrouble(false, 6) → false答案:
public boolean parrotTrouble(boolean talking, int hour) {  if( talking && (hour < 7 ||  hour > 20) ){    return true;  }    return false;}

6.makes10

Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10.

makes10(9, 10) → true

makes10(9, 9) → false

makes10(1, 9) → true

答案:

public boolean makes10(int a, int b) {  return ( a + b ==10 || a == 10 || b == 10)?true:false;}

7.nearHundred

Given an int n, return true if it is within 10 of 100 or 200. Note: Math.abs(num) computes the absolute value of a number.

nearHundred(93) → true

nearHundred(90) → true

nearHundred(89) → false

答案:

public boolean nearHundred(int n) {  return (Math.abs(n-100) <= 10 || Math.abs(n-200) <= 10)?true:false;}

8.posNeg

Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative.

posNeg(1, -1, false) → true

posNeg(-1, 1, false) → true

posNeg(-4, -5, true) → true

答案:


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表