Java 四向交叉口仿真

Java 四向交叉口仿真,java,object,Java,Object,我正试图编写一个程序,在四路交叉口显示灯光序列 这些指标是: 在任何给定时间,车辆只能朝一个方向行驶。交通灯有三种颜色:红色、黄色和绿色。基于一个事件。车灯以同步方式改变颜色,以确保安全驾驶。定义一个名为“TrafficLight”的类,该类有两个方法。一个称为“更改”,另一个称为“getColor”。在一个单独的类“Assignment5”中,使用主入口点,我希望您创建4个TrafficLight实例,两个用于北/南交通,两个用于东/西交通。使用对象提供的两种方法,我希望您展示您已正确模拟了四

我正试图编写一个程序,在四路交叉口显示灯光序列

这些指标是:

在任何给定时间,车辆只能朝一个方向行驶。交通灯有三种颜色:红色、黄色和绿色。基于一个事件。车灯以同步方式改变颜色,以确保安全驾驶。定义一个名为“TrafficLight”的类,该类有两个方法。一个称为“更改”,另一个称为“getColor”。在一个单独的类“Assignment5”中,使用主入口点,我希望您创建4个TrafficLight实例,两个用于北/南交通,两个用于东/西交通。使用对象提供的两种方法,我希望您展示您已正确模拟了四向交通灯

到目前为止,我已经:

 public class TrafficLight {

/**
 * @param args
 */
public TrafficLight(int c){
    this.c = c;

}//end constructor 

public int getC(){
    return c;
}

java.lang.String getColor;
    String n = "GYR";
    String s = "GYR";
    String e = "GYR";
    String w = "GYR";


public void change(){
    int c = 1;

     while (c <= 4){
         if (c == 1){
            n.charAt(2);
            s.charAt(2);
            e.charAt(0);
            w.charAt(0);
            c++;
         }
         else if (c == 2){
            n.charAt(2);
            s.charAt(2);
            e.charAt(1);
            w.charAt(1);
            c++;
         }
         else if (c == 3){
            n.charAt(0);
            s.charAt(0);
            e.charAt(2);
            w.charAt(2);
            c++;
         }
         else if (c == 4){
            n.charAt(1);
            s.charAt(1);
            e.charAt(2);
            w.charAt(2);
            c++;
         }

         while (c >= 5)
             if (c-4 == 1){
                n.charAt(2);
                s.charAt(2);
                e.charAt(0);
                w.charAt(0);
                c++;
             }
             else if (c-4 == 2){
                n.charAt(2);
                s.charAt(2);
                e.charAt(1);
                w.charAt(1);
                c++;
             }
             else if (c-4 == 3){
                n.charAt(0);
                s.charAt(0);
                e.charAt(2);
                w.charAt(2);
                c++;
             }
             else if (c-4 == 4){
                n.charAt(1);
                s.charAt(1);
                e.charAt(2);
                w.charAt(2);
                c++;
             }

        System.out.println(n);
        System.out.println(s);
        System.out.println(e);
        System.out.println(w);

     }//end inner while loop

}//end 'change'

 private int c;


 }//end class

我将如何着手实施这一点?有什么想法吗

你知道
charAt
实际上是做什么的吗?我的老师告诉我们要从
java.lang.String
调用数组。我猜不是吗?嗯,它会找出字符串中的一个字符是什么;但它不会以任何方式改变字符串。因此,调用它,然后不使用它的返回值将完全无效。这适用于你的32行程序。我不是想更改值,只是想找到它们,然后打印出来。我不是想让人觉得你居高临下,但如果你不确定字段和方法之间的区别,花点时间回顾一下本课程的笔记可能是个好主意。
Traffic light event 1: 
North light: Red
South light: Red
East light: Green
West light: Green

Traffic light event 2: 
North light: Red
South light: Red
East light: Yellow
West light: Yellow

Traffic light event 3: 
North light: Green
South light: Green
East light: Red
West light: Red

Traffic light event 4: 
North light: Yellow
South light: Yellow
East light: Red
West light: Red

Traffic light event 5: 
North light: Red
South light: Red
East light: Green
West light: Green

Traffic light event 6: 
North light: Red
South light: Red
East light: Yellow
West light: Yellow

Traffic light event 7: 
North light: Green
South light: Green
East light: Red
West light: Red

Traffic light event 8: 
North light: Yellow
South light: Yellow
East light: Red
West light: Red

Traffic light event 9: 
North light: Red
South light: Red
East light: Green
West light: Green

Traffic light event 10: 
North light: Red
South light: Red
East light: Yellow
West light: Yellow

Traffic light event 11: 
North light: Green
South light: Green
East light: Red
West light: Red

Traffic light event 12: 
North light: Yellow
South light: Yellow
East light: Red
West light: Red