Results 1 to 4 of 4

Thread: [SOLVED] loop

  1. #1
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default [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?

    Java 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;
       }
        
        }

  2. #2
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

    Default

    Java 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;
       }
        
        }

  3. #3
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    For future reference post your error message to make the error easier for us to find.

    However I think your error is in
    Java Code:
    while(con=1) { ...
    which should be
    Java Code:
    while(con == 1) { ...
    Also it looks like you declare the variable "con" twice.
    Java Code:
    int con = input.nextInt();
    should be
    Java Code:
    con = input.nextInt();
    Last edited by Mr.Beans; 04-08-2009 at 02:41 AM.

  4. #4
    leonardjr is offline Member
    Join Date
    Feb 2009
    Posts
    42
    Rep Power
    0

Similar Threads

  1. While loop
    By verbazon in forum New To Java
    Replies: 5
    Last Post: 02-25-2009, 02:29 AM
  2. For Loop
    By kian_hong2000 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 03:01 PM
  3. while loop
    By Unknown1369 in forum New To Java
    Replies: 5
    Last Post: 07-08-2008, 11:15 AM
  4. How to use While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:44 PM
  5. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 11:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •