Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package questão1_8;
- import java.util.Scanner;
- public class Questão1_8 {
- public static void main(String[] args) {
- Scanner dado = new Scanner (System.in);
- int qtdAlunos;
- do{
- System.out.print("Digite a quantidade de alunos da turma: ");
- qtdAlunos = dado.nextInt();
- }while(qtdAlunos <= 0);
- String nomes [] = new String [qtdAlunos];
- double notas [][] = new double[qtdAlunos][3];
- preencherDados(nomes,notas);
- ordenarLista(nomes,notas);
- exibir(nomes,notas);
- buscarAluno(nomes,notas);
- }
- public static void preencherDados(String nomes[], double notas[][]){
- Scanner dado = new Scanner(System.in);
- int i,j;
- for(i=0; i < nomes.length; i++){
- System.out.print("Digite o nome do aluno " + (i+1) + ": ");
- nomes[i] = dado.nextLine();
- for(j=0;j < notas[i].length;j++){
- if( j < notas[0].length -1){
- System.out.print("Digite a nota do(a) " + nomes[i] + " da prova " + (j+1) + ": ");
- notas[i][j] = dado.nextDouble();
- }else if( j == notas[0].length-1) {
- notas[i][j] = (notas[i][0] + notas[i][1]) /2;
- }
- }
- dado.nextLine();
- }
- }
- public static void exibir(String nomes[],double notas[][]){
- int i,j,w;
- for(i=0; i < nomes.length; i++){
- System.out.print(nomes[i] + " ");
- for(j=0; j < notas[0].length; j++){
- System.out.print(notas[i][j] + " ");
- }
- System.out.println();
- }
- }
- public static void ordenarLista(String nomes[],double notas[][]){
- int i,j,w;
- String ponte;
- double ponte2 [] = new double[3];
- int fim = nomes.length - 2;
- int pos = 0;
- boolean troca = true;
- while (troca == true) {
- troca = false;
- for (i = 0; i <= fim; i++) {
- if (nomes[i].compareToIgnoreCase(nomes[i+1]) > 0 ) {
- ponte = nomes[i];
- nomes[i] = nomes[i+1];
- nomes[i+1] = ponte;
- pos = i;
- troca = true;
- for(j=0; j < notas[0].length;j++){
- ponte2[j] = notas[i][j];
- notas[i][j] = notas[i+1][j];
- notas[i+1][j] = ponte2[j];
- }
- }
- }
- fim = pos-1;
- }
- }
- public static void buscarAluno(String nomes[],double notas[][]){
- int inicio = 0;
- int meio = 0;
- int fim = nomes.length -1;
- int achou = 0;
- int i;
- int resposta=0;
- int compare = 0;
- Scanner dado = new Scanner(System.in);
- String name;
- while(resposta!=2){
- do{
- System.out.print("1-Se quiser pesquisar algum nome\n2-Se quiser parar\n");
- System.out.print("Digite sua resposta: ");
- resposta = dado.nextInt();
- }while(resposta < 1 && resposta > 2);
- if(resposta !=2){
- dado.nextLine();
- System.out.print("Digite o nome que voce queira pesquisar: ");
- name = dado.nextLine();
- do {
- meio = (inicio + fim) / 2;
- compare = name.compareToIgnoreCase(nomes[meio]);
- if (compare == 0) {
- achou = 1;
- break;
- }
- else if (compare > 0) {
- fim = meio -1;
- }
- else {
- inicio = meio + 1;
- }
- } while (inicio <= fim);
- if (achou == 1){
- System.out.println("Notas do aluno(a) " + name);
- for(i=0; i < notas[0].length;i++){
- if(i<=1){
- System.out.println("Nota da prova " + (i+1) + " " + notas[meio][i]);
- }else{
- System.out.print("Media das duas provas " + notas[meio][i] );
- }
- }
- meio = 0;
- fim = nomes.length -1;
- System.out.println();
- }else{
- System.out.println("Não existem ninguem com esse nome.");
- meio = 0;
- fim = nomes.length -1;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement