![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
Comparing Numbers
import java.awt.*;
import hsa.Console;
public class comparism
{
static Console c = new Console ();
static public void main (String[] args)
{
int a, b;
c.clear ();
do
{
c.print ("A = ? ");
a = c.readInt ();
c.print ("B = ? ");
b = c.readInt ();
if (a > b)
{
c.println (a + " is geater than " + b);
}
else
if (a == b)
{
c.println (a + " equals to " + b);
}
else
if (a < b)
{
c.println (a + " is less that " + b);
}
else
if (a == -999)
{
break;
}
}
while (a > b && a == b && a < b || a != -999);
}
}I want my program to end when i enter the number -999. Can you tell me what i'm doing wrong? i don't wanna change and of the following: import java.awt.*;
import hsa.Console;
public class comparism
{
static Console c = new Console ();
static public void main (String[] args)
{can someone help me? |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Mmm.. in the while put just a!=-999. like this: while(a!=-999).
Also, the last if .. i don't think it's necesary after that.
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
a > b && a == b && a < b that will always evaluate to false, if a <b then a !> b if a == b etc etc etc so that statement is not required, just is while(a != -999) and yes the last if else is not required :/ just you had that odd thing in your while loop.
import java.awt.*;
import hsa.Console;
public class comparism
{
static Console c = new Console ();
static public void main (String[] args)
{
int a, b;
c.clear ();
do
{
c.print ("A = ? ");
a = c.readInt ();
c.print ("B = ? ");
b = c.readInt ();
if (a > b)
{
c.println (a + " is geater than " + b);
}
else if (a == b){
c.println (a + " equals to " + b);
}
else if (a < b){
c.println (a + " is less that " + b);
}
}
while (a != -999);
}
} |
|
|
|
|
|
#4 |
|
Professional Programmer
|
or, if you want to exit faster, put if(a==-999)break; - before if(a>b)
![]()
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
Thanks yo i got it.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|