The problem requires me to: test the given array(s) and see if there's number 5 in it. If there is a 5, return False. Otherwise, return true. I got it to work... well, I know it's not in an efficient way. The only problem is that in case 1 and 6 I got:Error java.lang.ArrayIndexOutOfBoundsException
, and I don't even know why. The rest works just fine.
import java.util.*;
public class testCases
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.print("Which case do you wish to test? ");
int testCase = kb.nextInt();
switch(testCase)
{
case 1:
System.out.println("noFives(1, 2, 3, 4) = " + noFives(1, 2, 3, 4, 6));
break;
case 2:
System.out.println("noFives(1, 2, 3, 4, 5) = " + noFives(1, 2, 3, 4, 5));
break;
case 3:
System.out.println("noFives(1, 2, 5, 3, 4) = " + noFives(1, 2, 5, 3, 4));
break;
case 4:
System.out.println("noFives(5, 1, 2, 3, 4) = " + noFives(5, 1, 2, 3, 4));
break;
case 5:
System.out.println("noFives(27, 82, 4, 71, 6, 23, 9, 18) = " + noFives(27, 82, 4, 71, 6, 23, 9, 18));
break;
case 6:
System.out.println("noFives(0) = " + noFives(0));
break;
case 7:
System.out.println("noFives(5) = " + noFives(5));
break;
default:
System.out.println("noFives() = " + noFives());
}
}
public static boolean noFives(int ... n)
{
if(n.length == 0)
return true;
else if(n[0] == 5 || n[1] == 5 || n[2] == 5 || n[3] == 5 || n[4] == 5 || n[5] == 5 || n[6] == 5 || n[7] == 5)
return false;
else
return true;
}
}