xdie Posted November 17, 2009 at 11:30 PM Report Share #296735 Posted November 17, 2009 at 11:30 PM Precisava de ajuda com um programa em java que estou a tentar fazer. Queria elaborar um programa que leia uma sequência de números inteiros positivos entre 10 e 10000 e que termine com a leitura dum número negativo ou zero. O programa deve ainda mostrar os números inteiros positivos cujo maior dígito (ou um dos maiores dígitos) é mais significativo (mais à esquerda) do que no correspondente número invertido. Por exemplo: no número 192683, o maior dígito (9) é mais significativo do que no respectivo número invertido, 386291. package programa1; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int[][] numeros=new int[6][7]; int digito; for (int i = 1; i < 7; i++){ for(int j = 1; j < 5; j++){ System.out.println("Numero " + i + " Introduza o "+ j +" digito "); digito = input.nextInt(); numeros[j][i] = digito; if (digito<=0) break; } } } } Até ao momento apenas consegui fazer isto... alguém me podia ajudar? Link to comment Share on other sites More sharing options...
Damon4hire Posted November 18, 2009 at 04:37 PM Report Share #296776 Posted November 18, 2009 at 04:37 PM Acho k tens de ser mais explicito. Quanto ao numero negativo ou zero, e quanto ao que consideras significativo. Link to comment Share on other sites More sharing options...
vasco16 Posted November 18, 2009 at 07:04 PM Report Share #296795 Posted November 18, 2009 at 07:04 PM Pelo que percebi vais introduzindo numeros entre 10 e 10000 e quando inserires um 0 ou um numero negativo o programa sai do ciclo. Depois dessa introdução mostras o mais significativo.. para mim o mais significativo é o que tiver mais digitios e o primeiro numero à esquerda for o maior signifigicativo, dai nao estar a perceber a tua logica de significativo, se puderes explica melhor.. Link to comment Share on other sites More sharing options...
xdie Posted November 19, 2009 at 04:55 PM Author Report Share #296908 Posted November 19, 2009 at 04:55 PM Bem estive a trabalhar nele e saiu uma coisa deste género, mas mesmo assim continua sem funcionar. import javax.swing.JOptionPane; public class Main { public static void CompNum(int num, int nnum) { int n = 0; while (nnum != 0) { n++; nnum = nnum / 10; } //JOptionPane.showMessageDialog(null, n); ColocaArrayInv(num, n); } public static void ColocaArrayInv(int num, int n) { int i, nnum, dig; int[] v = new int[n]; nnum = num; while (nnum / 10 != 0) { for (i = 0; i < v.length; i++) { dig = nnum % 10; v[i] = dig; nnum = nnum / 10; } } ColocaArray(n, v, num); } public static void ColocaArray(int n, int v[], int num) { int i, j = 1; int[] p = new int[n]; for (i = 0; i < v.length; i++) { p[n - j] = v[i]; j++; } MaisSign(p, v, n, num); } public static void MaisSign(int[] p, int[] v, int n, int num) { int i, maior = 0, ind=0; for (i = 0; i < v.length; i++) { if (p[i] > maior) { maior = p[i]; ind = i; } } if (n - (ind + 1) > ind) { JOptionPane.showMessageDialog(null, num); }else{ JOptionPane.showMessageDialog(null, "O maior numero nao é mais significativo na forma original do que " + "invertido"); } } public static void main(String[] args) { int num, nnum; String Vauxiliar; Vauxiliar = JOptionPane.showInputDialog("Insira o numero"); num = Integer.parseInt(Vauxiliar); while (num > 0) { if (num >= 10 & num <= 10000) { nnum = num; CompNum(num, nnum); } Vauxiliar = JOptionPane.showInputDialog("Insira o numero"); num = Integer.parseInt(Vauxiliar); } } } Link to comment Share on other sites More sharing options...
Damon4hire Posted November 20, 2009 at 12:22 AM Report Share #296965 Posted November 20, 2009 at 12:22 AM Acho que ninguem ainda percebeu ao certo o queres fazer. Mas quanto aos numeros invertidos usa isto, pode ser que ajude a limpar o código um bocado. import org.apache.commons.lang.StringUtils; Exemplo: String string = "Hi, How R YOU?"; String reverse = StringUtils.reverse(string); ----> reverse = "?UOY R woH ,iH" int numInvertido = (int) StringUtils.reverse(String.valueOf(numero)); Link to comment Share on other sites More sharing options...
vasco16 Posted November 20, 2009 at 04:22 PM Report Share #297021 Posted November 20, 2009 at 04:22 PM Acho que ninguem ainda percebeu ao certo o queres fazer. Mas quanto aos numeros invertidos usa isto, pode ser que ajude a limpar o código um bocado. import org.apache.commons.lang.StringUtils; Exemplo: String string = "Hi, How R YOU?"; String reverse = StringUtils.reverse(string); ----> reverse = "?UOY R woH ,iH" int numInvertido = (int) StringUtils.reverse(String.valueOf(numero)); LOL, Eu que numa aula tive de fazer um metodo para inverter uma string e tu agora chegas aqui com 2 linhas de codigo e fazes isso 🙂 bem mais simples xD Link to comment Share on other sites More sharing options...
Damon4hire Posted November 21, 2009 at 09:10 PM Report Share #297130 Posted November 21, 2009 at 09:10 PM Lolada. Achei isto na net, na verdade não cheguei a experimentar, porque não achei a biblioteca. Mas é o meu lema. Simples 🙂 Link to comment Share on other sites More sharing options...
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