Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

ForLoop Example

Hi,

public class ForLoopEx {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for(int fireDept=0;fireDept<10;fireDept++){
			for (int policeDept = 0; policeDept <10; policeDept++) {
				for (int SanitationDept = 0; SanitationDept <10; SanitationDept++) {
					//System.out.println("value is-->"+fireDept+" "+middleDigit+"  "+SanitationDept);
					
				if ( (fireDept!= policeDept)&&
					 (policeDept!=SanitationDept)&&
					 (fireDept!=SanitationDept)&&
					 (fireDept+policeDept+SanitationDept==12)&&
					 (policeDept%2==0) 
					 )
					 
					 {
					System.out.println("value is-->"+fireDept+" "+policeDept+"  "+SanitationDept);
					
				}
					
				}
			}
		}

	}

}

Open in new window


i got below output for above code

value is-->0 4  8
value is-->0 8  4
value is-->1 2  9
value is-->1 4  7
value is-->1 6  5
value is-->1 8  3
value is-->2 4  6
value is-->2 6  4
value is-->3 0  9
value is-->3 2  7
value is-->3 4  5
value is-->3 8  1
value is-->4 0  8
value is-->4 2  6
value is-->4 6  2
value is-->4 8  0
value is-->5 0  7
value is-->5 4  3
value is-->5 6  1
value is-->6 2  4
value is-->6 4  2
value is-->7 0  5
value is-->7 2  3
value is-->7 4  1
value is-->8 0  4
value is-->8 4  0
value is-->9 0  3
value is-->9 2  1


How to introduce a function for checking part for logic look easier? please advise
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

public class ForLoopEx {

	static boolean isValidDepartment(int fireDept, int policeDept, int sanitationDept){
		if((fireDept!= policeDept)&&
		   (policeDept!=sanitationDept)&&
		   (fireDept!=sanitationDept)&&
		   (fireDept+policeDept+sanitationDept==12)&&
		   (policeDept%2==0) )
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int fireDept = 0; fireDept < 10; fireDept++) {
			for (int policeDept = 0; policeDept < 10; policeDept++) {
				for (int sanitationDept = 0; sanitationDept < 10; sanitationDept++) {
					// System.out.println("value is-->"+fireDept+"
					// "+middleDigit+" "+SanitationDept);

					if (isValidDepartment(fireDept, policeDept, sanitationDept))

					{
						System.out.println("value is-->" + fireDept + " " + policeDept + "  " + sanitationDept);

					}

				}
			}
		}

	}

}

Open in new window


above gave below error

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
      Syntax error on token ")", Statement expected after this token

      at ForLoopEx.isValidDepartment(ForLoopEx.java:9)
      at ForLoopEx.main(ForLoopEx.java:20)
Avatar of gudii9

ASKER

public class ForLoopEx {

	static boolean isValidDepartment(int fireDept, int policeDept, int sanitationDept){
		return ((fireDept!= policeDept)&&
		        (policeDept!=sanitationDept)&&
		        (fireDept!=sanitationDept)&&
		        (fireDept+policeDept+sanitationDept==12)&&
		        (policeDept%2==0) );
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for (int fireDept = 0; fireDept < 10; fireDept++) {
			for (int policeDept = 0; policeDept < 10; policeDept++) {
				for (int sanitationDept = 0; sanitationDept < 10; sanitationDept++) {
					// System.out.println("value is-->"+fireDept+"
					// "+middleDigit+" "+SanitationDept);

					if (isValidDepartment(fireDept, policeDept, sanitationDept))

					{
						System.out.println("value is-->" + fireDept + " " + policeDept + "  " + sanitationDept);

					}

				}
			}
		}

	}

}

Open in new window

i fixed as above to get below output

value is-->0 4  8
value is-->0 8  4
value is-->1 2  9
value is-->1 4  7
value is-->1 6  5
value is-->1 8  3
value is-->2 4  6
value is-->2 6  4
value is-->3 0  9
value is-->3 2  7
value is-->3 4  5
value is-->3 8  1
value is-->4 0  8
value is-->4 2  6
value is-->4 6  2
value is-->4 8  0
value is-->5 0  7
value is-->5 4  3
value is-->5 6  1
value is-->6 2  4
value is-->6 4  2
value is-->7 0  5
value is-->7 2  3
value is-->7 4  1
value is-->8 0  4
value is-->8 4  0
value is-->9 0  3
value is-->9 2  1


how to improve my code overall? please advise
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial