Reputation: 11
My homework has a problem, where i need to write a code that is supposed to check two inputted numbers, see if they are leg or a hypotenuse, then use Pythagorean theorem to find the third side.
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String check1;
String check2;
String leg = "leg";
int first;
int second;
int leg2;
int leg1;
int hypo;
first = kb.nextInt();//check the first value
check1 = kb.next();
if (check1.equals(leg)){//check if it is a leg or hypotenuse
leg1 = first;}
else{hypo = first;};
second = kb.nextInt();
check2 = kb.next();
if (check2.equals(leg)){
leg2 = second;}
else{hypo = second;};
if (Objects.equals(check1, check2)){//if both are legs, then find a square of hypotenuse
int hyposq = (int)(leg1*leg1) + (int)(leg2*leg2);
(hypo*hypo) = hyposq;
System.out.println(hypo + " Hypotenuse");}//the value of hypotenuse
else{
int legsq = (int)(hypo*hypo) - (int)(leg1*leg1);
(leg2*leg2) =legsq;
System.out.println(leg2 + " leg");}}}
After many attempts to make methods work, rewriting and replacing chunks of code, i finally was able to fix many compiling errors, leaving only these 2:
Solution.java:41: error: unexpected type
(hypo*hypo) = hyposq;
^
required: variable
found: value
Solution.java:47: error: unexpected type
(leg2*leg2) =legsq;
^
required: variable
found: value
I have no idea how to fix these two, and googling is not much of a help either.
Upvotes: 1
Views: 65
Reputation: 2670
Remember that when defining variables:
data_type variable_name = value;
The VALUE should go on the right side of the equals sign, and the data type and variable name should go on the left side.
Since, I assume, you are trying to find the value of the hypotenuse, we can do some simple algebra:
If hypo * hypo = hyposq, then hypo = square root of hyposq:
hypo = (int)(Math.sqrt(hyposq))
The same would go for leg2
:
leg2 = (int)(Math.sqrt(leg2))
EDIT:
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String check1;
String check2;
int first;
int second;
int leg1 = 0;
int leg2 = 0;
int hypo = 0;
// System.out.println(leg1);
first = kb.nextInt();//check the first value
check1 = kb.next();
if (check1.equals("leg")){//check if it is a leg or hypotenuse
leg1 = first;
}
else{
hypo = first;
}
second = kb.nextInt();
check2 = kb.next();
if (check2.equals("leg")){
leg2 = second;
}
else{
hypo = second;
}
if (check1.equals(check2)){//if both are legs, then find a square of hypotenuse
int hyposq = (int)(leg1*leg1) + (int)(leg2*leg2);
hypo = (int)(Math.sqrt(hyposq));
System.out.println(hypo + " Hypotenuse");//the value of hypotenuse
}
else{ //if one leg is missing
int legsq;
if(leg2 == 0){ //if leg2 is the missing side (is still default value)
legsq = (int)(hypo*hypo) - (int)(leg1*leg1);
}
else{ //if leg1 is missing side (is still default value)
legsq = (int)(hypo*hypo) - (int)(leg2*leg2);
}
int lastLeg = (int)(Math.sqrt(legsq));
System.out.println(lastLeg);
}
}
}
Basically just polished up your code and edited the last part. One thing to note is that your code will only receive and output INTEGER values, so if you want your code to work for doubles, basically just change wherever it says int to double.
Let me know if you need any further help or clarifications :)
Upvotes: 1