.dsy:it.
Show 150 posts per page

.dsy:it. (http://www.dsy.it/forum/)
- Fondamenti di architettura e programmazione (http://www.dsy.it/forum/forumdisplay.php?forumid=234)
-- problemino (http://www.dsy.it/forum/showthread.php?threadid=23096)


Posted by alfoguasta on 10-12-2005 11:28:

problemino

perchè se inserisco per y=15 la x al termine del programma mi viene 4?


import prog.io.*;

class Asd {
static public void main(String[] args) {
ConsoleOutputManager video = new ConsoleOutputManager();
ConsoleInputManager tastiera = new ConsoleInputManager();

int x, y;
y= tastiera.readInt();
x=0;
while ((x*x)<y)
x++;
video.println(x);
}
}


Posted by korn on 10-12-2005 11:33:

Perché quando quando x arriva a 3 la condizione del while è ancora valida (3 * 3 = 9, quindi < 15) ed esegue ancora una volta il ciclo.

La soluzione è prendere come risultato x-1.

__________________
» Collect some stars to shine for you, and start today ‘cause there are only a few. _ (In Flames)
» Don't stop for nothing, it's full speed or nothing! I'm taking down, you know, whatever is in my way! _ ('tallica)
» I am my own god, I do as I please. _ (Pain)
» Ninetynine, ninetynine knives! Ninetynine knives inside! Nobody gets out alive! _ (The Haunted)
Web: http://www.negativesignal.com - ICQ# 171585477 - Death to software patents! And TCPA too! "e uno!", diceva il boia.


Posted by c3ru on 10-12-2005 11:34:

ops

__________________
Nessuno è troppo giovane per avere flashback dal Vietnam

Ogni generazione ha l'eroe che si merita


Posted by ciops on 10-12-2005 18:44:

Intervento del moderatore:
La risposta è stata data in maniera esaudiente, Chiudo:D

__________________
Ma che film la vita tutta una tirata storia infinita a ritmo serrato da stare senza fiato.

Ma che film la vita tutta una sorpresa attore, spettatore tra gioia e dolore tra il buio ed il colore.


All times are GMT. The time now is 14:56.
Show all 4 posts from this thread on one page

Powered by: vBulletin Version 2.3.1
Copyright © Jelsoft Enterprises Limited 2000 - 2002.