Matemática Java

Abordaremos a matemática Java com exemplos na lição de hoje para atender melhor às necessidades dos alunos.

Você pode executar operações matemáticas em números usando a classe Java Math

A classe Math consiste em vários métodos para executar operações matemáticas, como

  • min()
  • max()
  • média()
  • pecado()
  • cos()
  • bronzeado()
  • redondo()
  • teto()
  • chão()
  • abdômen()

 

Math.max( mrx,amplo )

Math.max( mrx,ample ) pode ser usado para encontrar o valor mais alto entre dois números:

Example: 

1
2
3
4
5
6
7
8
9
10
11
public class Main {
public static void main(String[] args) {
int mrx=10;
int ample=20; System.out.println("The highest value is: "+Math.max(mrx, ample));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Main {
public static void main(String[] args) {
int mrx_age=15;
int ample_age=21;
System.out.println("The highest value is: "+Math.max(mrx_age, ample_age));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


Math.min(mrx,amplo)

Math.max(mrx,ample) pode ser usado para encontrar o valor mais baixo de mrx e ample :

Example: 

1
2
3
4
5
6
7
8
9
10
11
public class Main {
public static void main(String[] args) {
int mrx=10;
int ample=20; System.out.println("The minimum value among both is: "+Math.min(mrx, ample));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Main {
public static void main(String[] args) {
int mrx_num=50;
int ample_num=70;
System.out.println("The minimum value among both is: "+Math.min(mrx_num,ample_num ));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Math.sqrt(mrx)

Math.sqrt ( mrx ) retorna a raiz quadrada de mrx :

Example: 

1
2
3
4
5
6
7
8
9
10
11
public class Main {
public static void main(String[] args) {
int mrx=81;
System.out.println("The Square root of "+mrx+" is : "+Math.sqrt(mrx));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Também podemos encontrar a raiz de um número em decimais:

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
public class Main {
public static void main(String[] args) {
int ample=141;
System.out.println("The Square root of "+ample+" is : "+(double)Math.sqrt(ample));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Math.abs( mrx )

Math.abs ( mrx ) retorna o valor absoluto ( positivo ) de mrx :

Example: 

1
2
3
4
5
6
7
8
9
10
11
public class Main {
public static void main(String[] args) {
int mrx=-612;
System.out.println("The positive value of "+mrx+" using Math.abs is: "+Math.abs(mrx));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Main {
public static void main(String[] args) {
float ample=-15.24f;
System.out.println("The positive value of "+ample+" using Math.abs is: "+Math.abs(ample));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Matemática Java – Números Aleatórios

A função Math.random() retorna um número aleatório entre 0,0 (inclusivo) e 1,0 (exclusivo):

Example: 

1
2
3
4
5
6
7
8
9
10
public class Main {
public static void main(String[] args) {
System.out.println("Generating any number by the command: "+Math.random());
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Example: 

1
2
3
4
5
6
7
8
9
10
11
public class Main {
public static void main(String[] args) {
System.out.println("Generating any number Randomly: "+Math.random());
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Aqui está uma fórmula que você pode usar se quiser mais controle sobre o número aleatório, como um número entre 0 e 200:

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
public class Main {
public static void main(String[] args) {
int random_Num_Range = (int)(Math.random() * 201);
// Any random number between 0 to 200 will be printed
System.out.println(random_Num_Range);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Da mesma forma para obter um número aleatório entre 0 a 30 siga o exemplo abaixo:

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Main {
public static void main(String[] args) {
int random_Num_Range = (int)(Math.random() * 31);
// Any random number between 0 to 30 will be printed
System.out.println(random_Num_Range);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Referência matemática completa

Existem vários métodos na classe Java-Math para executar cálculos numéricos básicos, incluindo logaritmos, raízes cúbicas e funções trigonométricas. Você pode encontrar uma referência completa aos métodos matemáticos em nossa Referência de métodos matemáticos Java .

Nós valorizamos o seu feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Assine a nossa newsletter
Digite seu e-mail para receber um resumo semanal de nossos melhores posts. Saber mais!
ícone