Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Atividade Prática 2 – Random & Vetores

1-) Array de Números Pares:


import java.util.Scanner;
import java.util.Random;
class Principal{
public static void main(String[] args){
Scanner ler = new Scanner(System.in);
Random rand = new Random();

int[] aleatorios = new int[10];

for(int c=0; c<=9; c++){


aleatorios[c] = rand.nextInt(100);
}

for(int c=0; c<=9; c++){


if(aleatorios[c]%2 == 0) {
System.out.println(aleatorios[c]);
}
}
}
}

2-) Adivinhe o número:


import java.util.Scanner;
import java.util.Random;

class Principal {
public static void main(String[] args) {
Scanner ler = new Scanner(System.in);
Random rand = new Random();

int aleatorio, num;

aleatorio = rand.nextInt(100);
System.out.println("Advinhe o numero");

for(int i = 0; i < 100; i++){


System.out.println("Digite um numero: ");
num = ler.nextInt();

if(num < aleatorio){


System.out.println("Baixo\n");
}
else if (num > aleatorio){
System.out.println("Alto\n");
}
else if (num == aleatorio){
System.out.println("\nResposta correta! O numero gerado era: " + aleatorio);
break;
}
}
}
}
3-) Média de Valores:
import java.util.Scanner;
import java.util.Random;
class Principal{

public static void main(String[] args){

Scanner ler = new Scanner(System.in);


Random rand = new Random();

int macaco_louco_e_nerfado = 0;
int[] aleatorios = new int[20];

for(int c=0; c<aleatorios.length; c++){


aleatorios[c] = rand.nextInt(100);
}

for(int c=0; c<aleatorios.length; c++){


System.out.println(aleatorios[c] + "\t");
macaco_louco_e_nerfado += aleatorios[c];
}
System.out.println("Media final: " + (macaco_louco_e_nerfado / 20));
}
}

4-) Número Máximo e Mínimo:


Import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

class Principal {
public static void main(String[] args) {
Scanner ler = new Scanner(System.in);
Random rand = new Random();

int[] aleatorios = new int [15];


for(int c=0; c < aleatorios.length; c++){
aleatorios[c] = rand.nextInt(100);
}

for(int c=0; c < aleatorios.length; c++){


System.out.println(aleatorios[c] + "\t");
}
Arrays.sort(aleatorios);
System.out.println("Maior numero:" + aleatorios[0] + "\nMenor numero: " +
aleatorios[14]);
}
}
5-) Contando Ímpares:
import java.util.Random;

public class Main {


public static void main(String[] args) {

Random rand = new Random();

int[] molecula_malcriada = new int[30];


int[] capivara_mutante_do_rock = new int[30];
int SITU_FREDDY_FAZZBEAR = 0;

for (int c = 0; c < molecula_malcriada .length; c++) {


molecula_malcriada [c] = rand.nextInt(100);

if (aleatorios[c] % 2 != 0) {
capivara_mutante_do_rock [SITU_FREDDY_FAZZBEAR] =
molecula_malcriada[c];
SITU_FREDDY_FAZZBEAR++;
}
}

System.out.println("Numeros aleatorios:");
for (int c = 0; c < molecula_malcriada .length; c++) {
System.out.println(molecula_malcriada [c]);
}

System.out.println("Dos 30 numeros, " + SITU_FREDDY_FAZZBEAR + " sao


impares.");
}

6-) Reversão de Array:


import java.util.Scanner;
import java.util.Random;

class Principal {
public static void main(String[] args) {
Scanner ler = new Scanner(System.in);
Random rand = new Random();

int[] aleatorios = new int [10];

for(int c = 0; c < aleatorios.length; c++){


aleatorios[c] = rand.nextInt(100);
}
System.out.println("\n" + "\nVetor original:");
for(int c = 0; c < aleatorios.length; c++){
System.out.print(aleatorios[c] + "\t");
}

System.out.println("\n" + "\nVetor em ordem Reversa: ");


for(int c = 9; c >= 0; c--){
System.out.print(aleatorios[c] + "\t");
}
}
}

7-) Frequência de Números:


import java.util.Random;
import java.util.Arrays;

class Principal
{
public static void main(String[] args) {
Random rand = new Random();

int[] aleatorios = new int[50];


int[] contagem = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] soma = new int[10];

for(int c = 0; c < aleatorios.length; c++){


aleatorios[c] = rand.nextInt(10) + 1;
}
Arrays.sort(aleatorios);
for(int c = 0; c < aleatorios.length; c++){
System.out.print(aleatorios[c] + "\n");
}

for(int i = 0; i < aleatorios.length; i++){

for(int c = 0; c < contagem.length; c++){


if(aleatorios[i] == contagem[c]){
soma[c] += 1;
}
}
}

for(int c = 0; c < contagem.length; c++){


System.out.println("\nQuantidade de numeros " + (c+1) + ": " + soma[c]);
}
}
}

8-) Preenchendo o Array (Incompleto):


import java.util.Scanner;
import java.util.Arrays;

class Main {
public static void main(String[] args) {
Scanner ler = new Scanner(System.in);

int[] nums = new int[10];


int[] soma = new int[10];
int[] contagem = new int[20];
int soma_total = 0;

for(int c= 0; c < nums.length; c++){


System.out.print("Insira o " + (c+1) + " numero: ");
nums[c] = ler.nextInt();
contagem[c + 1] = nums[c];
}

for(int c = 0; c < nums.length; c++){

if(contagem[c + 1] == nums[c]){
soma[c] += 1;
}
else{
c++;
}

for(int c = 0; c < soma.length; c++){


soma_total += soma[c];
}

System.out.print("\n" + soma_total);

}
}
9-) Busca Linear:
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;

public class Main


{
public static void main(String[] args) {

Scanner ler = new Scanner(System.in);


Random rand = new Random();

int[] aleatorios = new int[25];


int numero;

boolean flag = false;

for(int c = 0; c < aleatorios.length; c++){


aleatorios[c] = rand.nextInt(100);
}

System.out.println("Insira um numero: ");


numero = ler.nextInt();

for(int c = 0; c < aleatorios.length; c++){

if(aleatorios[c] == numero){
System.out.println("O numero " + numero + " foi encontrado! ");
flag = true;
break;
}
}

if(flag == false || numero >= 100){


System.out.println("O numero " + numero + " nao foi encontrado! ");
}
}
}
10-) Simulação de Jogo de Dados:
import java.util.Random;

public class Main


{
public static void main(String[] args) {

Random rand = new Random();

int[] d1 = new int[100];


int[] d2 = new int[100];
int[] soma = new int[100];

int contador = 0;

for(int c = 0; c < d1.length; c++){


d1[c] = rand.nextInt(6)+1;
System.out.print(d1[c] + " + ");

d2[c] = rand.nextInt(6)+1;
System.out.print(d1[c]);
soma[c] = d1[c] + d2[c];
System.out.print(" = " + soma[c] + "\n");

if(soma[c] == 7){
contador++;
}
}

System.out.println("A soma dos dados foi igual a 7, " + contador + " vezes.");
}
}

You might also like