Fórum Exercicios - Java Cifra de Cesar [RESOLVIDO] #569811
20/11/2012
0
import java.util.Scanner;
public class CesarYuriFernando {
public static void main(String[] args) {
Scanner entrada = new Scanner(System.in);
String CP;
System.out.println("Digite seu texto aqui:");
String texto = entrada.nextLine();
System.out.println("Digite sua chave aqui:");
int chave = entrada.nextInt();
int i = args.length;
if ( i <= 0) {
System.out.println("Não foi informado uma chave" );
}
if (i == 1) {
CP = criptografa ( texto, chave);
System.out.printf("\nTexto criptografado:\n");
System.out.printf("%s", CP);
}
if (i > 1) {
System.out.println("Você digitou dois numeros");
}
}
public static String criptografa(String texto, int chave) {
int i, n = texto.length();
String saux = "";
for (i=0; i<n; i++) {
saux = saux + (char)(texto.charAt(i) + chave);
}
return(saux);
}
}
Yuri Silva
Curtir tópico
+ 0Posts
20/11/2012
Tiago
Gostei + 0
20/11/2012
Tiago Azevedo
import java.util.Scanner;
public class cesar {
public static void main(String[] args) {
Scanner entrada = new Scanner(System.in);
String CP;
int chave, i = 0;
System.out.println("Digite seu texto aqui:");
String texto = entrada.nextLine();
System.out.println("Digite sua chave aqui:");
chave = entrada.nextInt();
while (chave>26){
chave = chave - 26;
}
i = args.length;
if ( i <= 0) {
System.out.println("Não foi informado uma chave" );
}
if (i == 1) {
CP = criptografa ( texto, chave);
System.out.printf("\nTexto criptografado:\n");
System.out.printf("%s", CP);
}
if (i > 1) {
System.out.println("Você digitou dois numeros");
}
}
public static String criptografa (String texto, int chave){
StringBuilder textoCifrado = new StringBuilder();
int tamanhoTexto = texto.length();
for(int c=0; c < tamanhoTexto; c++){
int letraCifradaASCII = ((int) texto.charAt(c)) + chave;
if (texto.charAt(c) > 97 && texto.charAt(c) < 122 ) {
while (letraCifradaASCII > 122) {
letraCifradaASCII = 97; }
}
if (texto.charAt(c) > 65 && texto.charAt(c) < 60 ) {
while (letraCifradaASCII > 90) {
letraCifradaASCII = 65;
}
}
textoCifrado.append( (char)letraCifradaASCII );
}
return textoCifrado.toString();
} }
Gostei + 0
20/11/2012
Tiago
Gostei + 0
20/11/2012
Tiago Azevedo
Gostei + 0
20/11/2012
Tiago Azevedo
if (texto.charAt(i) > 97 && texto.charAt(i) < 122 ) {
while (saux + chave > 122) {
totalm = saux + chave;
menosm = totalm - 122;
saux = 96+menosm;
}
}
if (texto.charAt(i) > 65 && texto.charAt(i) < 90 ) {
while (saux + chave > 90) {
saux = 65+chave;
totalM = saux + chave;
menosM = totalM - 90;
saux = 64+menosM;Gostei + 0
21/11/2012
Tiago
Gostei + 0
22/11/2012
Tiago Azevedo
import java.util.Scanner;
public class Yuri_CESAR {
public static void main(String[] args) {
Scanner entrada = new Scanner(System.in);
String CP;
int chave, i = 0;
System.out.println("Digite seu texto aqui:");
String texto = entrada.nextLine();
System.out.println("Digite sua chave aqui:");
chave = entrada.nextInt();
while (chave>26){
chave = chave - 26;
}
i = args.length;
if ( i <= 0) {
System.out.println("Não foi informado uma chave" );
}
if (i == 1) {
CP = criptografa ( texto, chave);
System.out.printf("\nTexto criptografado:\n");
System.out.printf("%s", CP);
}
if (i > 1) {
System.out.println("Você digitou dois numeros");
}
}
public static String criptografa (String texto, int chave){
StringBuilder textoCifrado = new StringBuilder();
int tamanhoTexto = texto.length();
int totalm, totalM, menosm, menosM;
for(int c=0; c < tamanhoTexto; c++){
int letraCifradaASCII = ((int) texto.charAt(c)) + chave;
if (((int) texto.charAt(c)) > 97 && ((int) texto.charAt(c)) < 122 ) {
while (((int) texto.charAt(c)) + chave > 122) {
totalm = texto.charAt(c) + chave;
menosm = totalm - 122;
letraCifradaASCII = 96+menosm;
}
}
if (((int) texto.charAt(c)) > 65 && ((int) texto.charAt(c)) < 90 ) {
while (((int) texto.charAt(c)) + chave > 90) {
totalM = texto.charAt(c) + chave;
menosM = totalM - 90;
letraCifradaASCII = 64+menosM;
}
}
textoCifrado.append( (char)letraCifradaASCII );
}
return textoCifrado.toString();
} }
Gostei + 0
22/11/2012
Tiago
i = chave;
if ( i == 0) {
System.out.println("Não foi informado uma chave" );
}
else if (i < 10) {
CP = criptografa ( texto, chave);
System.out.printf("\nTexto criptografado:\n");
System.out.printf("%s", CP);
}
else {
System.out.println("Você digitou dois numeros");
}
if (((int) texto.charAt(c)) > 97 && ((int) texto.charAt(c)) < 122 ) {
while (((int) texto.charAt(c)) + chave > 122) {
totalm = texto.charAt(c) + chave;
menosm = totalm - 122;
letraCifradaASCII = 96+menosm;
}
} Gostei + 0
22/11/2012
Tiago Azevedo
Gostei + 0
22/11/2012
Tiago
Gostei + 0
22/11/2012
Tiago Azevedo
import java.util.Scanner;
public class T01_Cesar {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("A chave para ecriptação não informado");
} else if (args.length > 1) {
System.out.println("Foi informado mais du um argumento");
} else {
int k = Integer.parseInt(args[0]);
//Loop para chave ficar entre 0-25
while (k >= 26) {
k=k-26;
}
Scanner entrada = new Scanner(System.in);
System.out.println("Informe a mensagem a ser criptografada:");
String texto = entrada.nextLine();
int totalm, totalM, menosM, menosm, letraCifradaASCII;
for(int i = 0; i < texto.length(); i++){
// Tratamento Letras minusculas
if ((int) texto.charAt(i) >= 97 && (int) texto.charAt(i) <= 122 ) {
if ((int) (texto.charAt(i) + k) > 122) {
totalm = (texto.charAt(i) + k);
menosm = totalm - 122;
letraCifradaASCII = 96+menosm;
System.out.print(letraCifradaASCII);
} // Caso nao precisa de tratamento fazer isso
else {
letraCifradaASCII = (int) (texto.charAt(i) + k);
System.out.print(letraCifradaASCII);
}
}
// Tratamento Letras Maiusculas
if ((int) texto.charAt(i) >= 65 && (int) texto.charAt(i) <= 90 ) {
if ((int) (texto.charAt(i) + k) > 90) {
totalM = (texto.charAt(i) + k);
menosM = totalM - 90;
letraCifradaASCII = 64+menosM;
System.out.print(letraCifradaASCII);
}
// Caso nao precisa de tratamento fazer isso
else {
letraCifradaASCII = (int)(texto.charAt(i) + k);
System.out.print(letraCifradaASCII);
}
// CONSEGUIIIIIIIIIIIII depois de muito esforço!
}
}}
}
}
Gostei + 0
22/11/2012
Tiago
Gostei + 0
22/11/2012
Tiago Azevedo
Gostei + 0
22/11/2012
Tiago Azevedo
Gostei + 0
22/11/2012
Tiago
Gostei + 0