Przejdź do głównej zawartości

Logic-1 > alarmClock - if(true) - instrukcja warunkowa

Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are on vacation, return a string of the form "7:00" indicating when the alarm clock should ring. Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00". Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off"


larmClock(1, false) → "7:00"
alarmClock(5, false) → "7:00"
alarmClock(0, false) → "10:00"

mamy warunek isVacation true or false ktory determinuje ktorym torem pojdziemy.

Instrukcja warunkowa powinna ten warunek sprawdzic jako pierwszy.

if (isVacation){
.
.
.
}
else
{
.
.
.
}




public String alarmClock(int day, boolean vacation)
{
 if(vacation)
 {
  if(day == 0 || day == 6)
   return "off";
  return "10:00";
 }
 else
 {
  if(day == 0 || day == 6)
   return "10:00";
  return "7:00";
 }
}


Kolejny przykład: if outsideMode true than;

public boolean in1To10(int n, boolean outsideMode) {
  if (outsideMode){
    if(n<=1 || n>=10){
      return true;
    }
  }
  else 
  {
    if(n>=1 && n<=10){
       return true;
    }
  }return false;
}

Komentarze

Popularne posty z tego bloga

Skrócony zapis if - instrukcja warunkowa java

Instrukcja warunkowa - warunek i rezultat. if (warunek) { jesli spełniony wykonań operacje i zwróć wynik; } warunek nie spełniony Możliwości skrócenia kodu instrukcji warunkowej if (i < 0) ? i-- : i++; Jeżeli i mniejsze od zera to i-- jezeli false to i++ if (i < 0) {     i--; } else {     i++; } Skrócony zapis instrukcji warunkowej else if (i < 0) ? i--;  inna_zmienna=4; : i++; if (i < 0) {     i--; } else {     i++;     inna_zmienna = 4; } Skrócony zapis if

String-1 > endsLy

Given a string, return true if it ends in "ly". endsLy("oddly") → true endsLy("y") → false endsLy("oddy") → false public boolean endsLy ( String str ) { if ( str . length ()< 2 ){ return false ; } if (( str . substring ( str . length ()- 2 , str . length ())). equals ( "ly" )){ return true ; } return false ; }

String-1 > withouEnd2

Given a string, return a version without both the first and last char of the string. The string may be any length, including 0. withouEnd2("Hello") → "ell" withouEnd2("abc") → "b" withouEnd2("ab") → "" public String withouEnd2 ( String str ) { if ( str . length ()== 1 ){ return str . substring ( 1 ); } else if ( str . length ()== 0 ){ return str ; } return ( str . substring ( 1 , str . length ()- 1 )); }