CodeGym/Cursuri/Sintaxa Java/Compararea și stabilirea condițiilor

Compararea și stabilirea condițiilor

Disponibil
2
Sarcină
Sintaxa Java,  nivellecţie
Blocat
18+
Would any child close a website after being informed that it is not intended for anybody under 18? Despite being entirely ineffective, this age "verification" process is still used. Let's write a program that asks the user's age. If the answer is <18, the program displays "Grow up a little more".
2
Sarcină
Sintaxa Java,  nivellecţie
Blocat
Bouncer policy
The Chastity nightclub has commissioned Company X to make an automatic bouncer. A junior developer (you) was instructed to write a visitor identification method that includes an age check. Would-be guests need to use the keyboard to enter their name and age. If the age is 20 or more, the auto-bouncer should assure the guest that everything is OK—18 is old enough.
4
Sarcină
Sintaxa Java,  nivellecţie
Blocat
Three numbers
Keyboard input, numeric comparisons, and screen output—4th-level CodeGym students hone these skills until they are automatic. Let's write a program where the user enters three numbers from the keyboard. Then we'll perform a comparison. If we find a number that differs from the other two, we'll display its ordinal number.
2
Sarcină
Sintaxa Java,  nivellecţie
Blocat
Target locked!
A military robot has been equipped with a sight that helps it shoot down enemy tin cans. An informant passes the robot the enemy's surface coordinates (latitude and longitude). Let's write a program that helps the robot calculate the quadrant where the enemy tin can is located.

Un fragment de prelegere cu un mentor ca parte a cursului Universității Codegym. Înscrie-te la cursul complet.


„Aș dori să vă spun puțin despre compararea variabilelor în Java.

„Cunoașteți deja cei mai simpli operatori de comparație – mai puțin decât (<) și mai mare decât (>).”

"Da."

„Există și operatori ca egal cu (==) și nu egal cu (!=). La fel și mai mic sau egal cu (<=) și mai mare sau egal cu (>=).”

„Acum asta devine interesant”.

„Rețineți că nu există operatori =< sau => în Java!”

" Semnul = este folosit pentru operațiunile de atribuire. De aceea sunt folosite două semne egale (==) pentru a testa egalitatea. Pentru a verifica dacă variabilele nu sunt egale , utilizați operatorul != ."

"Înțeleg."

„Când comparăm două variabile în Java folosind operatorul ==, comparăm conținutul variabilelor.”

„Astfel, pentru variabilele primitive , valorile lor sunt comparate .”

„Pentru variabilele de referință , referințele sunt comparate . Să presupunem că avem obiecte identice, dar distincte. Deoarece referințele la ele sunt diferite , o comparație va arăta că nu sunt egale, adică rezultatul comparației va fi fals . O comparație a referințelor va fi adevărată. numai dacă ambele referințe indică același obiect.

„Pentru a compara conținutul intern al obiectelor, folosim metoda special equals . Această metodă (și toate metodele clasei Object) sunt adăugate la clasa dumneavoastră de către compilator chiar dacă nu le declarați. Permiteți-mi să vă arăt câteva exemple: "

Cod Explicaţie
1
int a = 5;
int b = 5;
System.out.println(a == b);
Comparați tipurile primitive .
true va fi afișat pe ecran.
2
Cat cat1 = new Cat("Oscar");
Cat cat2 = cat1;
System.out.println(cat1 == cat2);
Comparați referințe .
true va fi afișat pe ecran.
Ambele variabile stochează referințe la același obiect .
3
String s = new String("Mom");
String s2 = s;
System.out.println(s == s2);
Comparați referințe .
true va fi afișat pe ecran.
Ambele variabile stochează referințe la același obiect .
4
Cat cat1 = new Cat("Oscar");
Cat cat2 = new Cat("Oscar");
System.out.println(cat1 == cat2);
Comparați referințe .
false va fi afișat pe ecran.
Cele două variabile fac referire la obiecte Cat identice, dar nu la același.
5
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s == s2);
Comparați referințe .
false va fi afișat pe ecran.
Cele două variabile fac referire la obiecte String identice, dar nu la același.
6
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s.equals(s2));
Compara obiecte .
true va fi afișat pe ecran.
Cele două variabile fac referire la obiecte String identice

"Oh, aproape că am uitat! Iată câteva exerciții pentru tine:"

4
Sarcină
Sintaxa Java,  nivellecţie
Blocat
Minimum of two numbers
All search and sort algorithms are based on comparisons. You'll be able to handle these very soon, if you so desire. In the meantime, we suggest starting with something small: write a program to find the minimum of two numbers. Find it and then display it. And if the numbers are the same, display either of them.
4
Sarcină
Sintaxa Java,  nivellecţie
Blocat
Maximum of four numbers
Finding the maximum is an n-ary operation (an operation on n numbers) that returns the largest of several numbers. Never mind. We have no need for such definitions at the secret CodeGym center. We're here to learn how to write code. In this task, you need to use the keyboard to enter four numbers. Then determine the largest of them and display it on the screen.
8
Sarcină
Sintaxa Java,  nivellecţie
Blocat
Sorting three numbers
Planet Linear Chaos is populated by isomorphs. They are believed to have invented sorting algorithms. Everything in their heads is extremely well-ordered. They only issue planetary visas to people who know at least 7 sorting algorithms. Let's take our first step toward Linear Chaos: Read three numbers from the keyboard, put them in descending order, and then display them on the screen.
4
Sarcină
Sintaxa Java,  nivellecţie
Blocat
Jen or Jen?
Jen, Company X's admin, learned how to pilot a space ship and flew away to another planet. People in Company X are good and sincere. It's just that they're scatterbrained and they mix up names. So they decided that the new administrator would also be called Jen. Let's help Company X find their Jen: write a program that checks the identity of two entered names.
Comentarii
  • Popular
  • Nou
  • Vechi
Trebuie să fii conectat pentru a lăsa un comentariu
Această pagină nu are încă niciun comentariu