Spero ti vada benecodice:import javax.swing.JOptionPane; import java.util.StringTokenizer; public class numbers{ public static void major( int a[] ){ int max1 = 0; int max2 = 0; for ( int i = 0; i < a.length; i++ ) // cerca il valore maggiore nell'array { if (a[i]>0 && Math.abs(max1) < Math.abs(a[ i ])) max1 = a[ i ]; }//for1 for ( int i = 0; i < a.length; i++ ) // cerca il secondo valore nell'array { if (a[i] <0 && Math.abs(max2) < Math.abs(a[i]) ) max2 = a[ i ]; }//for2 JOptionPane.showMessageDialog( null, "I numeri aventi valori assoluti maggiori in ordine sono: "+ max1 + " e " + max2 + "\n(0 = valore non trovato)", "Ecco i risultati", JOptionPane.INFORMATION_MESSAGE ); // restituisce i risultati }//major public static void main( String args[] ){ String input; String temp = ""; int num; int i = 0; int box[]; do { input = JOptionPane.showInputDialog( "Inserisci un numero, 9999 per terminare" ); temp += input + " "; }while ( Integer.parseInt( input ) != 9999 ); // immette i dati in una stringa temporanea StringTokenizer tokens = new StringTokenizer( temp ); // crea un'istanza della classe StringTokenizer box = new int[ tokens.countTokens() ]; // allocazione dell'array while ( tokens.hasMoreTokens() ){ num = Integer.parseInt( tokens.nextToken() ); if( num != 9999){ box[ i ] = num; i++;}//while } // inserisce i dati dalla stringa temporanea all'array major( box ); // avvia il metodo per la ricerca dei 3 valori piu' grandi System.exit( 0 ); }//main }
Ciauz
![]()



Rispondi quotando