Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 keyboardthe 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");
}
}

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

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

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

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~

![]() |
![]() |
![]() |

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