CodeGym/Curso Java/Sintaxe do Java/Comparando e definindo condições

Comparando e definindo condições

Disponível
2
Tarefa
Sintaxe do Java,  nível 4lição 6
Bloqueado
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
Tarefa
Sintaxe do Java,  nível 4lição 6
Bloqueado
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
Tarefa
Sintaxe do Java,  nível 4lição 6
Bloqueado
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
Tarefa
Sintaxe do Java,  nível 4lição 6
Bloqueado
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.

Um trecho de palestra com um mentor como parte do curso Codegym University. Inscreva-se no curso completo.


"Gostaria de falar um pouco sobre a comparação de variáveis ​​em Java. "

"Você já conhece os operadores de comparação mais simples – menor que (<) e maior que (>)."

"Sim."

"Existem também operadores como igual a (==) e não igual a (!=). Assim como, menor ou igual a (<=) e maior ou igual a (>=)."

"Agora isso está ficando interessante."

"Observe que não há operadores =< ou => em Java!"

" O sinal = é usado para operações de atribuição. É por isso que dois sinais de igual (==) são usados ​​para testar a igualdade. Para verificar se as variáveis ​​não são iguais , use o operador != ."

"Eu vejo."

"Ao comparar duas variáveis ​​em Java usando o operador ==, estamos comparando o conteúdo das variáveis."

"Assim, para variáveis ​​primitivas , seus valores são comparados ."

"Para variáveis ​​de referência , as referências são comparadas . Suponha que temos objetos idênticos, mas distintos. Como as referências a eles são diferentes , uma comparação mostrará que eles não são iguais, ou seja, o resultado da comparação será falso . Uma comparação de referências será verdadeira somente se ambas as referências apontarem para o mesmo objeto. "

"Para comparar o conteúdo interno dos objetos, usamos o método equals especial . Este método (e todos os métodos da classe Object) são adicionados à sua classe pelo compilador, mesmo que você não os declare. Deixe-me mostrar alguns exemplos: "

Código Explicação
1
int a = 5;
int b = 5;
System.out.println(a == b);
Comparar tipos primitivos .
true será exibido na tela.
2
Cat cat1 = new Cat("Oscar");
Cat cat2 = cat1;
System.out.println(cat1 == cat2);
Comparar referências .
true será exibido na tela.
Ambas as variáveis ​​armazenam referências ao mesmo objeto .
3
String s = new String("Mom");
String s2 = s;
System.out.println(s == s2);
Comparar referências .
true será exibido na tela.
Ambas as variáveis ​​armazenam referências ao mesmo objeto .
4
Cat cat1 = new Cat("Oscar");
Cat cat2 = new Cat("Oscar");
System.out.println(cat1 == cat2);
Comparar referências .
falso será exibido na tela.
As duas variáveis ​​referenciam objetos Cat idênticos, mas não o mesmo.
5
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s == s2);
Comparar referências .
falso será exibido na tela.
As duas variáveis ​​referenciam objetos String idênticos, mas não o mesmo.
6
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s.equals(s2));
Comparar objetos .
true será exibido na tela.
As duas variáveis ​​referenciam objetos String idênticos

"Ah, quase esqueci! Aqui estão alguns exercícios para você:"

4
Tarefa
Sintaxe do Java,  nível 4lição 6
Bloqueado
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
Tarefa
Sintaxe do Java,  nível 4lição 6
Bloqueado
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
Tarefa
Sintaxe do Java,  nível 4lição 6
Bloqueado
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
Tarefa
Sintaxe do Java,  nível 4lição 6
Bloqueado
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.
Comentários
  • Populares
  • Novas
  • Antigas
Você precisa acessar para deixar um comentário
Esta página ainda não tem nenhum comentário