eccomi qua, questa volta sono le stringhe a darmi problemi..
ho scritto un programma che dati 3 nomi e relative età, dovrebbe dire chi è il più vecchio e chi il più giovane.
se non inizializzo le stringhe "nome1", "nome2"e "nome3", mi dà errore e dice che devo inizializzarle.
ma se le inizializzo (o meglio se lascio a netbeans di inizializarmele in automatico come correzione)
(nome1 = null, nome2 =null, nome3 = null)
i risultati saranno che il più vecchio è "null" e il più giovane anche!
devo inizializzare in maniera diversa o ho fatto qualche errore più grande??
codice:/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package vecchiogiovane; import java.io.*; /** * * @author filipporezzadore */ public class Vecchiogiovane { /** * @param args the command line arguments */ public static void main(String[] args) throws Exception{ BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); // TODO code application logic here String nome1 = null; int eta1; String nome2 = null; int eta2; String nome3 = null; int eta3; String oldest; String youngest; int etamax = 0; int etamin = 0; System.out.println("ins first name:"); String line = input.readLine(); System.out.println("ins first age:"); line = input.readLine(); eta1 = Integer.parseInt(line); System.out.println("ins second name:"); line = input.readLine(); System.out.println("ins second age:"); line = input.readLine(); eta2 = Integer.parseInt(line); System.out.println("ins third name:"); line = input.readLine(); System.out.println("ins third age:"); line = input.readLine(); eta3 = Integer.parseInt(line); if (eta1 > eta2) eta1 = etamax; if (eta3 > etamax) eta3 = etamax; if (eta1 < eta2) eta1 = etamin; if (eta3 < etamin) eta3 = etamin; if (etamax == eta1) oldest = nome1; else if (etamax == eta2) oldest = nome2; else oldest = nome3; if (etamin == eta1) youngest = nome1; else if (etamin == eta2) youngest = nome2; else youngest = nome3; System.out.println("oldest is:" + oldest); System.out.println("youngest is:" + youngest); } }
esecuzione:
codice:run: ins first name: aldo ins first age: 35 ins second name: mino ins second age: 67 ins third name: carlo ins third age: 32 oldest is:null youngest is:null BUILD SUCCESSFUL (total time: 25 seconds) grazie mille ancora!!

Rispondi quotando