problema
Dati due file f1.txt ordinati di interi produrre un terzo file f3.txt ordinato di interi contenenti tutti i numeri
di f1.txt ed f2.txt).
Esempio: Sia f1.txt 5 6 6 10 ed f2 = 4 5 12 24 si deve produrre f3.txt con 4 5 5 6 6 10 12 24
Nota: non usare array od ordinamenti.
io riesco a stampare 4 5 5 6 6 24
questo è il mio codice
cosa dovrei modificare per stampare anche il 10 e il 12 ?codice:public static void main(String[] args) throws FileNotFoundException, IOException { Scanner textfile = new Scanner(new File("f1.txt")); Scanner textfile2 = new Scanner(new File("f2.txt")); PrintWriter output = new PrintWriter(new FileWriter("f3.txt")); int n1 = textfile.nextInt(); int n2 = textfile2.nextInt(); while (textfile.hasNextInt() && textfile2.hasNextInt()) { if (n1 <= n2 { output.print(n1 + " "); n1 = textfile.nextInt(); } else { output.print(n2 + " "); n2 = textfile2.nextInt(); } } if (!textfile.hasNextInt()) { n2 = textfile2.nextInt(); output.print(n2); } else if (!textfile2.hasNextInt()) { n1 = textfile.nextInt(); output.print(n1); } textfile.close(); textfile2.close(); output.close(); }

Rispondi quotando

