Você está na página 1de 5

Resoluo da ficha n 7 - Linguagem JAVA

Array
1. Construa um programa que l uma matriz de 2x3 e que permita
consultar os dados inseridos nas respectivas linhas e colunas.
Dica:

System.out.println(" Qual a linha a consultar?");


lin=Integer.parseInt(in.readLine());
System.out.println(" Qual a coluna a consultar?");
col=Integer.parseInt(in.readLine());
System.out.println(" Valor da linha: " + lin + "
,coluna: " + col + " : " + b[lin][col]);

Resoluo:
import java.io.*;
public class ex4 {
public static void main (String args []) throws
java.io.IOException
{
BufferedReader in = new BufferedReader (new
InputStreamReader (System.in));
int b [][]=new int [2][3];
int lin,col;
for (lin=0;lin<2;lin++)
{
for (col=0;col<3;col++)
{
System.out.println(" Introduza o valor da linha
- "+lin+" coluna - "+col);
b [lin][col]=Integer.parseInt(in.readLine());
}
}
System.out.println(" Qual a linha a consultar?");
lin=Integer.parseInt(in.readLine());
System.out.println(" Qual a coluna a consultar?");
col=Integer.parseInt(in.readLine());
System.out.println(" Valor da linha: " + lin + " ,coluna: "
+ col + " : " + b[lin][col]);
}
}

2. Construa um programa que leia um conjunto de alunos - 10 alunos cada um com o seu nome e a respectiva nota. Em seguida exiba o
nome dos alunos que obtiveram a nota maior do que a mdia da
turma.

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ex15string {
public
static
java.io.IOException

void

main

(String

args

[])

throws

{
BufferedReader
in
InputStreamReader (System.in));

new

BufferedReader

(new

String [] nomes=new String[10];;


double [] notas= new double[10];;
int i;
double media;
double total = 0;
System.out.print("\n" + "o nome do aluno (a): ");
for(i = 0; i < 10; i++){
nomes[i] = in.readLine();
System.out.print("nota deste aluno: ");
notas[i] = Double.parseDouble(in.readLine());
total = total+notas[i];
}
media = total / 10;
System.out.println("\n" + "Nome dos alunos que ficaram acima da mdia:
");

for(i = 0; i < 10; i++){


if(notas[i] > media)
System.out.println(nomes[i]);
}
}
}

3. Construa um programa leia um conjunto de notas, cuja quantidade


seja

determinada

pelo

utilizador

[ex.

Quantos

dados

pretende inserir?:]. Em seguida exiba o nome dos alunos


que obtiveram a nota maior do que a mdia da turma.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ex16string {
public
static
java.io.IOException

void

main

(String

args

[])

throws

{
BufferedReader
in
InputStreamReader (System.in));

new

BufferedReader

(new

String [] nomes;
double [] notas;
int i,qtde;
double media;
double total = 0;
System.out.print("Quantos dados pretende inserir?: ");
qtde =Integer.parseInt(in.readLine());
//preencher os vetores
nomes = new String[qtde];
notas = new double[qtde];
for(i = 0; i < qtde; i++){

System.out.print("\nNome do aluno (a): ");


nomes[i] = in.readLine();
System.out.print("A nota deste aluno: ");
notas[i] = Double.parseDouble(in.readLine());
total = total+notas[i];
}

media = total / qtde;


//if(notas[i] > media)

for(i = 0; i < qtde; i++)


{
if(notas[i] > media)
System.out.println("\n Nome dos alunos que ficaram acima da mdia:
"+nomes[i]);
}
}
}

4. Programa que leia um conjunto de valores inteiros e em seguida


exiba-os na ordem inversa do que foram digitados.
import java.io.*;
public class ex17 {
public static void main (String args []) throws
java.io.IOException
{
BufferedReader in = new BufferedReader (new InputStreamReader
(System.in));
int [] valores;
int qte,i;
System.out.print("Quantidade de valores desejado: ");
qte =Integer.parseInt(in.readLine());
valores = new int[qte];
for(i = 0; i < qte; i++){
System.out.print("\nDigite o " + (i+1) + " valor: ");
valores[i] =Integer.parseInt(in.readLine());
}

System.out.println("\nValores em ordem inversa: ");


for(i = qte - 1; i >= 0; i--){
System.out.println(valores[i]);
}
}
}

5. Programa que leia um conjunto de nomes de pessoas. Exiba-os em


ordem alfabtica crescente.
import java.io.*;
import java.util.*;
public class ex18 {
public static void main (String args []) throws
java.io.IOException
{
BufferedReader in = new BufferedReader (new InputStreamReader
(System.in));
String[] listaNomes;
int qtd_nomes;
int i,x;
System.out.println("Quantos nomes?");
qtd_nomes = Integer.parseInt(in.readLine());
listaNomes = new String[qtd_nomes];
for (i = 0; i < qtd_nomes; i++) {
System.out.println("Digite o nome " + (i+1));
listaNomes[i] = in.readLine();
}
Arrays.sort(listaNomes);
for (x = 0; x < listaNomes.length; x++) {
System.out.println(listaNomes[x]);
}
}
}

Você também pode gostar