Computing.Net > Forums > Programming > java problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

java problem

Reply to Message Icon

Name: peterx
Date: September 28, 2004 at 12:45:00 Pacific
OS: win xp
CPU/Ram: 256
Comment:

Hi!
I am trying to solve a java problem. I have solved the problem, but just some thing does not work.
I am trying to find n , and this ( n x n) must be greater then a number(nbr).
EX: nbr=100;

n=10 and nxn=100; // from keyboard
n=11 and nxn=121; // from keyboard
n=12 and nxn=144; // from keyboard

the out put must be 11 not 12, or 13.... because the 11 is the first greater nbr.
I solved the problem,but when I write 12,or 13,or 22, or 5. the program finishs and I must run it again and put a new n. I want that the program keep going until I put n=11(ex).

class Test{
public static void main(String[]args){
System.out.println("writre a nbr:");
int nbr=100;
int n= Input.readInt();

if((n*n)<=nbr)
System.out.println(n+"\t"+"is wrong");

else if((n*n)>(nbr)+(n+n))
System.out.println(n+"\t"+"is wrong");

else if((n*n)< (nbr)+(n+n))
System.out.println(n+"\t"+"is correct");

}
}



Sponsored Link
Ads by Google

Response Number 1
Name: BlueRaja
Date: September 28, 2004 at 14:02:27 Pacific
Reply:

bool finished = false;
while (finished==false)
{
//program code
if (/*correct*/)
finished=true;
}


However, if you're just going to be inputting integers, you could always have the program cycle integers on its own (assuming I understand the program correctly..the problem being find the smallest integer n for a given number nbr that satifies n^2 > nbr.

int n=1;
for (;n*n<nbr;n++){}
System.out.println(n+"\t is correct");

AKhalifman@hotmail.com


0

Response Number 2
Name: BlueRaja
Date: September 28, 2004 at 14:03:25 Pacific
Reply:

)

AKhalifman@hotmail.com


0

Response Number 3
Name: peterx
Date: September 28, 2004 at 15:28:32 Pacific
Reply:

Hi!
The program does not work.
thanks
peter


0

Response Number 4
Name: BlueRaja
Date: September 28, 2004 at 19:08:14 Pacific
Reply:

Oh well in that case just let me whip out my magic wand and make everything better </sarcasm>

Seriously, though, what's the matter?

AKhalifman@hotmail.com


0

Response Number 5
Name: peterx
Date: September 29, 2004 at 03:22:48 Pacific
Reply:

Hi again!
The program: Find the smallest integer n for a given number nbr that satifies n^2 > nbr.
when I wrote your code, the program finishs and I must run it again and put a new n. I want that the program keep going until I put n^2 > nbr.
Maybe I missunderstood your code.
Please write again to me.
Thanks
Peter



0

Related Posts

See More



Response Number 6
Name: Tugg
Date: September 29, 2004 at 05:48:54 Pacific
Reply:

The code from Blue should work from what I can tell so maybe you have the loop a little mess up. Lets just go over that one more time.

class Test {
public static void main(String args[])
{
bool finished = false;
while (finished == false)
{
System.out.println("writre a nbr:");
int nbr=100;
int n= Input.readInt();

if((n*n)<=nbr)
System.out.println(n+"\t"+"is wrong");

else if((n*n)>(nbr)+(n+n))
System.out.println(n+"\t"+"is wrong");

else if((n*n)< (nbr)+(n+n))
{
System.out.println(n+"\t"+"is correct");
finished == true;
}
}
}

This should do the trick. I didnt count the closing } since i only looked over this quickly so make sure you have the right amount to close all the code blocks
Other than that it should work (dont have my java compiler at hand or i would have checked myself)

Hope it helps
~TuGG~


0

Response Number 7
Name: peterx
Date: September 29, 2004 at 09:04:02 Pacific
Reply:

Hi!
Thank you very much.
The program is working now.
Regards
Peter


0

Response Number 8
Name: BlueRaja
Date: September 29, 2004 at 21:34:48 Pacific
Reply:

Your program doesn't handle the case when n^2 == nbr + 2n ...

AKhalifman@hotmail.com


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: java problem

Java Problem www.computing.net/answers/programming/java-problem/7796.html

RealJ for Java (problem compiling) www.computing.net/answers/programming/realj-for-java-problem-compiling/1900.html

SQL / Oracle / Java Problem with results www.computing.net/answers/programming/sql-oracle-java-problem-with-results/2315.html