Simple Java Program - hasTeen [HELP]
I am trying to create a program that does the following:
"We'll say that a number is "teen" if it is in the range 13..19 inclusive. Given 3 int values, return true if 1 or more is teen.
My code:
Code :
public class hasTeen
{
public static void main()
{
System.out.println(" hasTeen(13, 20, 10) =" + hasTeen(13, 20, 10));
System.out.println(" hasTeen(20, 19, 10) =" + hasTeen(20, 19, 10));
System.out.println(" hasTeen(20, 10, 13)=" + hasTeen(20, 10, 13));
System.out.println("\n---end hasTeen---\n");
}
public static boolean hasTeen(int a, int b, int c)
{
if ( a >= 13 && a<= 19 || b >= 11 && b<= 19 || c >= 13 || c<= 19)
{
return false;
}
else
{
return true;
}
}
}
And when I run it they all come up false. Here's the original problem:
hasTeen(13, 20, 10) = true
hasTeen(20, 19, 10) = true
hasTeen(20, 10, 13) = true
I get false for all of these. What's wrong with my code?
Re: Simple Java Program - hasTeen [HELP]
You put "return false" in the wrong position, where the "return true" should be. Try inverting them like this:
Code :
public static boolean hasTeen(int a, int b, int c)
{
if ( a >= 13 && a<= 19 || b >= 11 && b<= 19 || c >= 13 || c<= 19)
{
return true;
}
else
{
return false;
}
Re: Simple Java Program - hasTeen [HELP]
Quote:
Originally Posted by
Henry518
You put "return false" in the wrong position, where the "return true" should be. Try inverting them like this:
Code :
public static boolean hasTeen(int a, int b, int c)
{
if ( a >= 13 && a<= 19 || b >= 11 && b<= 19 || c >= 13 || c<= 19)
{
return true;
}
else
{
return false;
}
Yes, they now return true, but when I input 3 numbers that aren't "teen", like (1, 4, 3), it still comes out as true, when it should be false as none of those numbers are "teen".
Error:
http://i54.tinypic.com/353446a.jpg
Re: Simple Java Program - hasTeen [HELP]
Alright, true... I found the problem:
public static boolean hasTeen(int a, int b, int c)
{
if ( a >= 13 && a<= 19 || b >= 11 && b<= 19 || c >= 13 || c<= 19)
{
return true;
}
else
{
return false;
}
Fixing that, it should look like this:
Code :
public static boolean hasTeen(int a, int b, int c)
{
if ( a >= 13 && a<= 19 || b >= 13 && b<= 19 || c >= 13 && c<= 19)
{
return true;
}
else
{
return false;
}
Re: Simple Java Program - hasTeen [HELP]
Quote:
Originally Posted by
Henry518
Alright, true... I found the problem:
public static boolean hasTeen(int a, int b, int c)
{
if ( a >= 13 && a<= 19 ||
b >= 11 && b<= 19 ||
c >= 13 || c<= 19)
{
return true;
}
else
{
return false;
}
Fixing that, it should look like this:
Code :
public static boolean hasTeen(int a, int b, int c)
{
if ( a >= 13 && a<= 19 || b >= 13 && b<= 19 || c >= 13 && c<= 19)
{
return true;
}
else
{
return false;
}
Thanks so much!!
Re: Simple Java Program - hasTeen [HELP]
Quote:
Originally Posted by
Henry518
Alright, true... I found the problem:
public static boolean hasTeen(int a, int b, int c)
{
if ( a >= 13 && a<= 19 ||
b >= 11 && b<= 19 ||
c >= 13 || c<= 19)
{
return true;
}
else
{
return false;
}
Fixing that, it should look like this:
Code :
public static boolean hasTeen(int a, int b, int c)
{
if ( a >= 13 && a<= 19 || b >= 13 && b<= 19 || c >= 13 && c<= 19)
{
return true;
}
else
{
return false;
}
It's good to help but don't spoon feed. Try to provide hints.
THanks
Re: Simple Java Program - hasTeen [HELP]
Quote:
Originally Posted by
Mr.777
It's good to help but don't spoon feed. Try to provide hints.
THanks
Thanks for the suggestion, i'll try to keep that in mind. ^^