CodeGym/Cursussen/Java Syntax/Voorwaardelijke operatoren

Voorwaardelijke operatoren

Beschikbaar
8
Taak
Java Syntax,  niveau 4les 4
Vergrendeld
Number of days in the year
On Earth, a year lasts 365 or 366 days. The number of days is calculated according to the following formula: A leap year (366 days) is every year evenly divisible by 4, except for years that are multiples of 100 but not multiples of 400. We'll write a program that will determine whether the user has entered a leap-year or ordinary year from the keyboard.
4
Taak
Java Syntax,  niveau 4les 4
Vergrendeld
Rule of the triangle
Can any three line segments be the sides of a triangle? You probably already guessed (or remembered from high-school geometry) that you can only have a triangle if the sum of the lengths of any two sides is greater than the length of the third side. Well, now we're going to write code to check whether 3 line segments are suitable for forming a triangle.
8
Taak
Java Syntax,  niveau 4les 4
Vergrendeld
Crossing the road blindly
Let's say that we are certain that at the beginning of every hour our traffic light is green for 3 minutes, yellow for a minute, and then red for another minute. Then the sequence repeats. Our program must determine what light is on now (where "now" is a real number that indicates the number of minutes that have elapsed since the beginning of the hour).
4
Taak
Java Syntax,  niveau 4les 4
Vergrendeld
Do we have a pair?
Suppose we have three numbers. Now let's imagine that they are not numbers, but people ... Actually, never mind about that. We don't need to make this weird. Let's just check if there is at least one pair of identical numbers among the three. If there is, we'll display it on the screen. And if the three numbers are the same, we'll display all three.

Een fragment uit een lezing met een mentor als onderdeel van de Codegym University-cursus. Schrijf je in voor de volledige cursus.


"Hallo, Amigo. Vandaag gaan we het hebben over if/else statements ."

"Programma's zouden weinig nut hebben als ze niet zouden reageren op veranderende externe omstandigheden. Een programma moet weten hoe het zich moet aanpassen aan de omstandigheden en de ene actie moet uitvoeren in het ene geval en de andere actie in het andere geval. In Java wordt dit bereikt met behulp van de 'if/else statement' – een speciale constructie die het mogelijk maakt om verschillende codeblokken uit te voeren als aan een voorwaarde is voldaan."

"Het bestaat uit drie delen: ' voorwaarde ', ' opdracht 1 ' en ' opdracht 2 '. Als de voorwaarde waar is, dan wordt ' opdracht 1 ' uitgevoerd, anders wordt 'opdracht 2' uitgevoerd. Deze opdrachten worden nooit beide uitgevoerd. De verklaring ziet er min of meer zo uit:"

Code voor een if/else statement
if (condition)
    command_1;
else
    command_2;

"Wat spannend! Ik denk dat die uitspraak het programmeren veel interessanter zal maken!"

"Ja. Hier zijn een paar voorbeelden voor je:"

Code Uitleg
1
if (a < b)
    System.out.println("A is less than B");
else
    System.out.println("B is less than  A");
Als a kleiner is dan b, wordt de eerste opdracht uitgevoerd. Anders wordt het tweede commando uitgevoerd . De commando's worden nooit allebei uitgevoerd.
2
if (a < b)
{
    System.out.println("A is less than B");
    System.out.println("B is greater than A");
}
else
{
     System.out.println("B is less than A");
     System.out.println("A is greater than B");
}
U kunt één opdracht vervangen door een codeblok. De rest is hetzelfde.
3
if (a < b)
{
    a = 0;
}
else
{
}
U kunt het else- blok weglaten als het leeg is.
Deze drie voorbeelden zijn volledig equivalent.
U kunt de accolades weglaten als u slechts één opdracht hoeft uit te voeren. Als je meer dan één opdracht hebt, moet je de haakjes behouden.
4
if (a < b)
{
    a = 0;
}
5
if (a < b)
    a = 0;

'Diego heeft me net gevraagd om je een paar taken te geven.'

2
Taak
Java Syntax,  niveau 4les 4
Vergrendeld
Good or bad?
Student robot Peter is an overachiever. Previously, his server was configured to receive scores on a five-point scale, but now his teachers have switched to a 12-point scale. But Peter doesn't know this. He's still focused on getting fives. Let's write him a compare method that compares any number with five.
4
Taak
Java Syntax,  niveau 4les 4
Vergrendeld
Closest to 10
Ten is extremely popular and attractive number. Everyone wants to be a ten. Or at least as close to it as possible. Two numbers are standing around wondering which of them is cooler. Answer: whichever is closer to ten. Let's write these numbers a displayClosestToTen method that will determine which of them is cooler.
4
Taak
Java Syntax,  niveau 4les 4
Vergrendeld
Come on, lucky seven!
Dice games are popular on the planet Foggy Multidimensions. The rules are different than the Earth version: Multidimensionals perceive far more dimensions than primitive three-dimensional earthlings. Their die has 4294967295 faces. Players win only if they roll a number between 50 and 100. We'll write a method that checks whether the die roll is in this range.
4
Taak
Java Syntax,  niveau 4les 4
Vergrendeld
Seasons on Terra
An Earth year consists of four seasons, each of which lasts 3 months. While our ship was parked on this cradle of humanity, the Interplanetary Tax Service asked us to write a program to determine the season based on a number corresponding to the month of the year. We don't know why they want it. They say that it's none of our business. But they promised not to remain in our debt.
4
Taak
Java Syntax,  niveau 4les 4
Vergrendeld
Positive and negative numbers
Diego is tall, but Alfredo is short. Rishi is experienced, but you're a "noob" programmer. Comparisons are unavoidable in life. And the same thing is true in programs. So we'll continue practicing comparisons and displaying stuff on the screen. This time let's compare the entered number with zero and manipulate it based on the result of the comparison.
4
Taak
Java Syntax,  niveau 4les 4
Vergrendeld
Day of the week
Planet Terra still has "offices"—an outdated type of workspace. With current technology, there is no need for them, but earthlings tend to be nostalgic and haven't been in a hurry to eradicate them. Terrestrial office workers develop "TGIF syndrome": they constantly want to know what day of the week it is. Let's write a program for them!

Een fragment uit een lezing met een mentor als onderdeel van de Codegym University-cursus. Schrijf je in voor de volledige cursus.


Opmerkingen
  • Populair
  • Nieuw
  • Oud
Je moet ingelogd zijn om opmerkingen te kunnen maken
Deze pagina heeft nog geen opmerkingen