williamjda Posted September 27, 2013 at 02:52 PM Report #526323 Posted September 27, 2013 at 02:52 PM Olá amigos, tenho uma duvida. Preciso conseguir imprimir apenas os caracteres impa de uma palavra. EXEMPLO: 01234 Diogo = Dig No código abaixo, eu consigo imprimir apenas a quantidade de caracteres. Mas eu quero impmirir apenas os caracteres Impares. import javax.swing.JOptionPane; public class Exercises_2 { public static void main(String[] args) { String total; int tamanho; int posicao = 1; total = JOptionPane.showInputDialog(null, "Entre com uma palavra: "); tamanho = total.length(); JOptionPane.showMessageDialog(null, total + " possui " + tamanho); } }
HappyHippyHippo Posted September 27, 2013 at 03:05 PM Report #526326 Posted September 27, 2013 at 03:05 PM (edited) sabes o que é um ciclo ? se sim, quais é que conheces ? Edited September 27, 2013 at 03:05 PM by HappyHippyHippo IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
williamjda Posted September 27, 2013 at 03:07 PM Author Report #526328 Posted September 27, 2013 at 03:07 PM while, for, do while...
HappyHippyHippo Posted September 27, 2013 at 03:20 PM Report #526333 Posted September 27, 2013 at 03:20 PM e com nenhum desses ciclos consegues iterar os caracteres da string para apresentar somente os que necessitas ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
williamjda Posted September 27, 2013 at 03:23 PM Author Report #526334 Posted September 27, 2013 at 03:23 PM Eu não estou sabendo como utilizar.... Tem algum exemplo referente a String....
HappyHippyHippo Posted September 27, 2013 at 06:41 PM Report #526386 Posted September 27, 2013 at 06:41 PM estive a ponderar não apresentar pela simples razão que se o fizesse a solução é de caras no entanto, tive pr'aqui a majicar um exemplo que te possa indicar no caminho certo sem, ao mesmo tempo, dizer-te como fazer: public static void main(String[] args) { String str = "sopapos"; int i; for (i = 0; i < str.length() / 2; i++) { if (str.charAt(i) != str.charAt(str.length() - i - 1)) { break; } } if (i == str.length() / 2) { System.out.println("a palavra é um palindroma"); } else { System.out.println("a palavra não é um palindroma"); } } IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now