-
[SOLVED] loop
I'm coming up with an error on the loop its telling me it is already defined and incompatible what am I doing wrong?
Code:
import java.util.Scanner;
public class CheckTriangles {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int con;
do{
//Prompt user to enter side values
System.out.print("Enter side 1 value: ");
double side1=input.nextDouble();
System.out.print("Enter side 2 value: ");
double side2=input.nextDouble();
System.out.print("Enter side 3 value: ");
double side3=input.nextDouble();
if(isValid(side1,side2,side3)){
//invoke the area of the valid triangle
System.out.println("The area is: " + area(side1,side2,side3));}
else{
System.out.println("The triangle is invalid and therefore area cannot be found");
}
System.out.print("Would you like to continue? 1=yes 2=no ");
int con=input.nextInt();
}while(con=1);
System.out.println("This program was written by Justeena Leonard");
}
public static boolean isValid(double side1, double side2, double side3){
if(side1+side2>side3&&side2+side3>side1&&side1+side3>side2)
return true;
else
return false;
}
public static double area(double side1, double side2, double side3){
double s=(side1+side2+side3)/2;
double area=Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}
}
-
Code:
import java.util.Scanner;
public class CheckTriangles {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
[COLOR="Yellow"][COLOR="Red"]int con;
do{[/COLOR][/COLOR]
//Prompt user to enter side values
System.out.print("Enter side 1 value: ");
double side1=input.nextDouble();
System.out.print("Enter side 2 value: ");
double side2=input.nextDouble();
System.out.print("Enter side 3 value: ");
double side3=input.nextDouble();
if(isValid(side1,side2,side3)){
//invoke the area of the valid triangle
System.out.println("The area is: " + area(side1,side2,side3));}
else{
System.out.println("The triangle is invalid and therefore area cannot be found");
}
[COLOR="red"]System.out.print("Would you like to continue? 1=yes 2=no ");
int con=input.nextInt();
}while(con=1);[/COLOR]
System.out.println("This program was written by Justeena Leonard");
}
public static boolean isValid(double side1, double side2, double side3){
if(side1+side2>side3&&side2+side3>side1&&side1+side3>side2)
return true;
else
return false;
}
public static double area(double side1, double side2, double side3){
double s=(side1+side2+side3)/2;
double area=Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}
}
-
For future reference post your error message to make the error easier for us to find.
However I think your error is in
which should be
Code:
while(con == 1) { ...
Also it looks like you declare the variable "con" twice.
Code:
int con = input.nextInt();
should be
Code:
con = input.nextInt();
-