Exercicios String
parent
0f6d39da76
commit
d876c142c0
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
package com.mycompany.stringexercicio1;
|
package com.mycompany.stringexercicio1;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -39,24 +40,208 @@ public class StringExercicio1 {
|
||||||
// nomeDaString.lenght() - retorna o tamanho da string
|
// nomeDaString.lenght() - retorna o tamanho da string
|
||||||
|
|
||||||
|
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// Scanner scanner = new Scanner(System.in);
|
||||||
|
// System.out.println("Introduza o seu nome");
|
||||||
|
// String nome = scanner.nextLine();
|
||||||
|
// System.out.println("A quantidade de caracteres é " + nome.length());
|
||||||
|
// System.out.println("Em maiusculas: " + nome.toUpperCase());
|
||||||
|
// System.out.println("Em minusculas: " + nome.toLowerCase());
|
||||||
|
// System.out.println("Contem campos? " + nome.contains("campos"));
|
||||||
|
// System.out.println("O primeiro caracter é: " + nome.charAt(0));
|
||||||
|
// System.out.println("O caracter do meio é: " + nome.charAt(nome.length() / 2));
|
||||||
|
// int contador = 0;
|
||||||
|
// for(int i = 0; i < nome.length(); i++)
|
||||||
|
// {
|
||||||
|
// if(nome.charAt(i) == 'a')
|
||||||
|
// {
|
||||||
|
// contador++;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// System.out.println("O nome tem " + contador + " a's");
|
||||||
|
|
||||||
|
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// //conversão entre tipos de dados
|
||||||
|
// //conversão de um tipo de dado para String
|
||||||
|
// String.valueOf(valorAConvertter)
|
||||||
|
// //conversão de String para int
|
||||||
|
// Integer.parseInt(stringAConverter)
|
||||||
|
// //conversão de String para double
|
||||||
|
// Double.parseDouble(stringAConverter)
|
||||||
|
// //conversão de String para float
|
||||||
|
// Float.parseFloat(stringAConverter)
|
||||||
|
// //conversão de String para long
|
||||||
|
// Long.parseLong(stringAConverter)
|
||||||
|
|
||||||
|
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// Scanner scanner = new Scanner(System.in);
|
||||||
|
// System.out.println("Introduza um número");
|
||||||
|
// String numero = scanner.nextLine();
|
||||||
|
// int numeroInteiro = Integer.parseInt(numero);
|
||||||
|
// double numeroDouble = Double.parseDouble(numero);
|
||||||
|
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// for(int i = 0; i < 11; i++)
|
||||||
|
// {
|
||||||
|
// System.out.println(i);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// for(int i = 10; i > 0; i--)
|
||||||
|
// {
|
||||||
|
// System.out.println(i);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// Scanner scanner = new Scanner(System.in);
|
||||||
|
// System.out.println("NIF?");
|
||||||
|
// String nif = scanner.nextLine();
|
||||||
|
// if(nif.length() != 9)
|
||||||
|
// {
|
||||||
|
// System.out.println("Inválido");
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// int multiplicador = 9;
|
||||||
|
// int soma = 0;
|
||||||
|
// for(int i = 0; i < nif.length() - 1; i++)
|
||||||
|
// {
|
||||||
|
// int digito = Integer.parseInt(String.valueOf(nif.charAt(i)));
|
||||||
|
// int multiplicacao = digito * multiplicador;
|
||||||
|
// soma += multiplicacao;
|
||||||
|
// multiplicador--;
|
||||||
|
// }
|
||||||
|
// int resto = soma % 11;
|
||||||
|
// int digitoControlo;
|
||||||
|
// if (resto == 0 || resto == 1)
|
||||||
|
// {
|
||||||
|
// digitoControlo = 0;
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// digitoControlo = 11 - resto;
|
||||||
|
// }
|
||||||
|
// int nonoDigito = Integer.parseInt(String.valueOf(nif.charAt(8)));
|
||||||
|
// if(nonoDigito == digitoControlo)
|
||||||
|
// {
|
||||||
|
// System.out.println("Válido");
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// System.out.println("Inválido");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// Scanner scanner = new Scanner(System.in);
|
||||||
|
// Random random = new Random();
|
||||||
|
// for (int j = 0; j < 5; j++)
|
||||||
|
// {
|
||||||
|
// for (int i = 0; i < 5; i++)
|
||||||
|
// {
|
||||||
|
// int numeroAleatorio = random.nextInt(99);
|
||||||
|
// System.out.print(numeroAleatorio + "\t");
|
||||||
|
// }
|
||||||
|
// System.out.println();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// Scanner scanner = new Scanner(System.in);
|
||||||
|
// for(int i = 50; i <= 50001; i++)
|
||||||
|
// {
|
||||||
|
// System.out.println(i);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// Scanner scanner = new Scanner(System.in);
|
||||||
|
// int primeiro;
|
||||||
|
// int segundo;
|
||||||
|
// System.out.print("Introduza o primeiro número positivo: ");
|
||||||
|
// primeiro = scanner.nextInt();
|
||||||
|
// while (primeiro <= 0)
|
||||||
|
// {
|
||||||
|
// System.out.print("Valor inválido. Introduza um número positivo: ");
|
||||||
|
// primeiro = scanner.nextInt();
|
||||||
|
// }
|
||||||
|
// System.out.print("Introduza o segundo número positivo: ");
|
||||||
|
// segundo = scanner.nextInt();
|
||||||
|
// while (segundo <= 0)
|
||||||
|
// {
|
||||||
|
// System.out.print("Valor inválido. Introduza um número positivo: ");
|
||||||
|
// segundo = scanner.nextInt();
|
||||||
|
// }
|
||||||
|
// if (primeiro > segundo)
|
||||||
|
// {
|
||||||
|
// int temp = primeiro;
|
||||||
|
// primeiro = segundo;
|
||||||
|
// segundo = temp;
|
||||||
|
// }
|
||||||
|
// System.out.println("Números pares entre " + primeiro + " e " + segundo + ":");
|
||||||
|
// for (int i = primeiro; i <= segundo; i++)
|
||||||
|
// {
|
||||||
|
// if (i % 2 == 0)
|
||||||
|
// {
|
||||||
|
// System.out.println(i);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// Scanner scanner = new Scanner(System.in);
|
||||||
|
// double soma = 0;
|
||||||
|
// double maiorFatura = 0;
|
||||||
|
// int mesMaior = 0;
|
||||||
|
// for (int mes = 1; mes <= 12; mes++)
|
||||||
|
// {
|
||||||
|
// System.out.print("Introduza o valor da fatura do mês " + mes + ": ");
|
||||||
|
// double valor = scanner.nextDouble();
|
||||||
|
// soma += valor;
|
||||||
|
// if (mes == 1 || valor > maiorFatura)
|
||||||
|
// {
|
||||||
|
// maiorFatura = valor;
|
||||||
|
// mesMaior = mes;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// System.out.println("Soma total das faturas: " + soma);
|
||||||
|
// System.out.println("O mês com maior valor é o mês " + mesMaior + " com " + maiorFatura);
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// Scanner scanner = new Scanner(System.in);
|
||||||
|
// System.out.print("Introduza um número inteiro positivo: ");
|
||||||
|
// int numero = scanner.nextInt();
|
||||||
|
// while (numero <= 0)
|
||||||
|
// {
|
||||||
|
// System.out.print("Valor inválido. Introduza um número inteiro positivo: ");
|
||||||
|
// numero = scanner.nextInt();
|
||||||
|
// }
|
||||||
|
// System.out.println("Tabela de multiplicação de " + numero + ":");
|
||||||
|
// for (int i = 1; i <= 10; i++)
|
||||||
|
// {
|
||||||
|
// int resultado = numero * i;
|
||||||
|
// System.out.println(numero + " x " + i + " = " + resultado);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Scanner scanner = new Scanner(System.in);
|
System.out.println("Matriz 5x5 com valores aleatórios entre 1 e 100:");
|
||||||
System.out.println("Introduza o seu nome");
|
for (int linha = 1; linha <= 5; linha++)
|
||||||
String nome = scanner.nextLine();
|
|
||||||
System.out.println("A quantidade de caracteres é " + nome.length());
|
|
||||||
System.out.println("Em maiusculas: " + nome.toUpperCase());
|
|
||||||
System.out.println("Em minusculas: " + nome.toLowerCase());
|
|
||||||
System.out.println("Contem campos? " + nome.contains("campos"));
|
|
||||||
System.out.println("O primeiro caracter é: " + nome.charAt(0));
|
|
||||||
System.out.println("O caracter do meio é: " + nome.charAt(nome.length() / 2));
|
|
||||||
int contador = 0;
|
|
||||||
for(int i = 0; i < nome.length(); i++)
|
|
||||||
{
|
{
|
||||||
if(nome.charAt(i) == 'a')
|
for (int coluna = 1; coluna <= 5; coluna++)
|
||||||
{
|
{
|
||||||
contador++;
|
int numero = (int) (Math.random() * 100) + 1;
|
||||||
|
System.out.print(numero + "\t");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("O nome tem " + contador + " a's");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue