publicclassmyScanner{ Scanner sc = new Scanner(System.in); publicstaticvoidmain(String[] args){ System.out.println("输入:"); Scanner sc = new Scanner(System.in); int m = sc.nextInt(); int n = sc.nextInt(); int[] num1 = newint[m]; int[] num2 = newint[n]; // 换成其他数据类型也一样,其他数值类型就修改int跟nextInt就可以了, //String就把nextInt()换成next() for(int i = 0; i < m; i ++) { num1[i] = sc.nextInt(); // 一个一个读取 } for(int i = 0; i < n; i ++) { num2[i] = sc.nextInt(); } } }
classZeroEvenOdd{ privateint n; private Object lock = new Object(); privateint flag = 0; publicZeroEvenOdd(int n){ this.n = n; }
// printNumber.accept(x) outputs "x", where x is an integer. publicvoidzero(IntConsumer printNumber)throws InterruptedException { synchronized (lock) { for (int i = 0; i < n; i++) { while (flag != 0) { lock.wait(); } printNumber.accept(0); flag = i % 2 == 1 ? 2 : 1; lock.notifyAll(); } } }
publicvoideven(IntConsumer printNumber)throws InterruptedException { synchronized (lock) { for (int i = 2; i <= n; i = i+2) { while (flag != 2) { lock.wait(); } printNumber.accept(i); flag = 0; lock.notifyAll(); } } }
publicvoidodd(IntConsumer printNumber)throws InterruptedException { synchronized (lock) { for (int i = 1; i <= n; i = i+2) { while (flag != 1) { lock.wait(); } printNumber.accept(i); System.out.println(flag); flag = 0; lock.notifyAll(); } } } }
publicvoidfoo(Runnable printFoo)throws InterruptedException { for (int i = 0; i < n; i++) { synchronized(lock) { if (run) { lock.wait(); } // printFoo.run() outputs "foo". Do not change or remove this line. printFoo.run(); run = true; lock.notify(); }
state += 1; } }
publicvoidbar(Runnable printBar)throws InterruptedException { for (int i = 0; i < n; i++) { synchronized(lock) { if (!run) { lock.wait(); } // printFoo.run() outputs "foo". Do not change or remove this line. printBar.run(); run = false; lock.notify(); } } } }