Imagine a row of N light switches, each attached to a light bulb. All the bulbs are off to start with. You are going to release your inner child so they can run back and forth along this row of light switches, flipping bunches of switches from on to off or vice versa. The challenge will be to figure out the state of the lights after this fun happens.
INPUT:
1) The number of switches/bulbs (N) is specified between 4-10.
2) On the remaining lines, there will be 4 pairs of integers indicating ranges of switches that your inner child turns on/off as they run back and forth. These ranges are inclusive (both their end points, along with everything between them is included), and the positions of switches are zero-indexed (so the possible positions range from 0 to N-1).
OUTPUT:
The number of light bulbs on.
0123456789
10 ..........
3-6 ||||
...XXXX...
0-4 |||||
XXX..XX...
7-3 |||||
XXXXX..X..
9-9 |
XXXXX..X.X
7 lights are ON
import java.io.*;
public class FinAssignment {
public static void main (String args[])throws IOException{
String name;
int switches;
int num1,num2;
String off="X";
String on="O";
int count;
final String FLAG="end";
BufferedReader br= new BufferedReader (new InputStreamReader(System.in));
System.out.println("Please enter user's name");
name=br.readLine();
System.out.println("Hello "+ name+ ", the number of switches has to be between 4-10");
System.out.println("Please enter the number of switches");
switches=Integer.parseInt(br.readLine());
while (switches >10 ||switches <4 ){
System.out.println("Please enter the number of switches again");
switches=Integer.parseInt(br.readLine());
boolean row[] = new boolean[switches];
}
System.out.println("Please enter the first range");
String input1=br.readLine();
String input2=br.readLine();
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
if(num1 <= num2) {
for (int i = num1; i <= num2; i++) {
System.out.print(on);
switches[i] = !switches[i];
}
} else {
for (int i = num2; i <= num1; i++) {
switches[i] = !switches[i];
}
for (int i=0;i<switches;i++);
if (switches[i]){
System.out.print(on);
}
else{
System.out.print(off);
}
}
System.out.println();
count=0;
for(int b=0;b<=switches-1;b++){
if(switches[b]){
count++;
}
}
}
}