...

View Full Version : triangles program



gingergib
10-29-2006, 03:47 PM
i need to write a program in java script which asks the user to enter 3 whole numbers representing the angles of a triangle. the program will then say wether the triangle is equalateral, isoseles or right angled. jus the most basic program using java things is needed. i wud appreciate any help as all i can come up with is the following:
public class triangles1
{
public static void main(string []args)
{
system.out.println("Please note that all angles must total 180!");
int ang_1 = getScannerInput.anInt("Enter a number from 1-180");
int ang_2 = getScannerInput.anInt("Enter a second number from 1-180");
int ang_3 = getScannerInput.anInt("Enter a third number from 1-180");
int tot = ang_1+ang_2+ang_3;
{
if (int tot >= 180)
system.out.println("invalid angles entered");
}
}
}// end class

Vladdy
10-29-2006, 05:27 PM
First thing first: learn the difference between Java and Javascript......

gingergib
10-29-2006, 05:43 PM
and the second thing?

jakbo
10-29-2006, 06:11 PM
and the second thing?

if(total>=180)
should be changed to:
if(total>180)
because a triangle has 180 degrees on the inside.
if(total==180){
if(ang_1==90||ang_2==90||ang_3==90){
system.whatever("right triangle");
}
if((ang_1==ang_2&&ang_1!=ang_3)||(ang_1==ang_3&&ang_1!=ang_2)||(ang_2==ang_3&&ang_2!=ang_1){
system.blahblah("isosceles triangle");
}
if(ang_1==ang_2&&ang_2==ang_3){
system.c++isbetterthanjava("equilateral triangle");
}
}
is one way to do it. I'm sure there are other ways, though.

gingergib
10-29-2006, 06:50 PM
cheers mate. big help. if i posted wrongly here im sorry but good to c ppl r still helpful these days. thanks again, il try that

gingergib
10-29-2006, 07:22 PM
i now got the following but it still wont work. any1 know how i cud touch it up?
:
public class triangles1
{
public static void main(String [] args)
{
int ang_1 = input from keyboard.anInt("enter value here");
int ang_2 = input from keyboard.anInt("enter number here");
int ang_3 = input from keyboard.anInt("enter digit here");
int total = ang_1+ang_2+ang_3;
{
if(total!=180)
System.out.println("please re-enter angles so as they equal 180");
}
{
if(ang_1==90||ang_2==90||ang_3==90)
System.out.println("right triangle");
}
{
if((ang_1==ang_2&&ang_1!=ang_3)||(ang_1==ang_3&&ang_1!=ang_2)||(ang_2==ang_3&&ang_2!=ang_1))
System.out.println("isosceles triangle");
}
{
if(ang_1==ang_2&&ang_2==ang_3)
System.out.println("equilateral triangle");
}
}}}// end class triangles1

jakbo
10-29-2006, 08:24 PM
i now got the following but it still wont work. any1 know how i cud touch it up?
:
public class triangles1
{
public static void main(String [] args)
{
int ang_1 = input from keyboard.anInt("enter value here");
int ang_2 = input from keyboard.anInt("enter number here");
int ang_3 = input from keyboard.anInt("enter digit here");
int total = ang_1+ang_2+ang_3;
{
if(total!=180)
System.out.println("please re-enter angles so as they equal 180");
}
{
if(ang_1==90||ang_2==90||ang_3==90)
System.out.println("right triangle");
}
{
if((ang_1==ang_2&&ang_1!=ang_3)||(ang_1==ang_3&&ang_1!=ang_2)||(ang_2==ang_3&&ang_2!=ang_1))
System.out.println("isosceles triangle");
}
{
if(ang_1==ang_2&&ang_2==ang_3)
System.out.println("equilateral triangle");
}
}}}// end class triangles1



Your braces are all messed up. You need to make sure they all match up.

jakbo
10-29-2006, 08:30 PM
public class triangles1{
public static void main(String [] args){
int ang_1 = input from keyboard.anInt("enter value here");
int ang_2 = input from keyboard.anInt("enter number here");
int ang_3 = input from keyboard.anInt("enter digit here");
int total = ang_1+ang_2+ang_3;
if(total!=180){
System.out.println("please re-enter angles so as they equal 180");
}
else{
if(ang_1==90||ang_2==90||ang_3==90){
System.out.println("right triangle");
}
if((ang_1==ang_2&&ang_1!=ang_3)||(ang_1==ang_3&&ang_1!=ang_2)||(ang_2==ang_3&&ang_2!=ang_1)){
System.out.println("isosceles triangle");
}
if(ang_1==ang_2&&ang_2==ang_3){
System.out.println("equilateral triangle");
}
}
}
}//end class

gingergib
10-29-2006, 08:39 PM
ur a legend jakbo!! things finally workin fine. so after a sleepless weekend and a lot of beer cigs an takeaway food i finnally beginnin to understand this stuff. cheers again boss

_Aerospace_Eng_
10-29-2006, 10:25 PM
I hope you get an A on your assignment. You should read this thread on how homework assignments should be handled here: http://www.codingforums.com/showthread.php?t=53446

jakbo
10-29-2006, 10:44 PM
I hope you get an A on your assignment. You should read this thread on how homework assignments should be handled here: http://www.codingforums.com/showthread.php?t=53446

Yeah, I shouldn't have done that. After I posted, I realized people need to think about this stuff, if they truly want to learn about it.

After thinking about it a little, though, it's still not quite right, because it doesn't take into consideration if someone enters 0 for one of the angles.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum