baidu_36475690 2016-10-23 02:08 采纳率: 0%
浏览 850

求帮助!!大神哪错了

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

}

  • 写回答

1条回答 默认 最新

  • devmiao 2016-10-23 04:05
    关注
    评论

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码