亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

青蛙過河問題!注釋寫的不咋地,有錯誤。希望大神解釋這個程序的rollback()和record()方法怎么算的?

青蛙過河問題!注釋寫的不咋地,有錯誤。希望大神解釋這個程序的rollback()和record()方法怎么算的?

Arnao 2017-08-30 19:56:50
package com.demo.hwj.javademo.kotlin;import java.util.Arrays;//要使用工具類;public class Testing { static int[] mInput = {1, 2, 3, 0, 9, 10, 11};//這是之前的順序;這里靜態的作用便是方便之后復制;? ? static int[] mStep = new int[100];//步數? ? static int[] mResult = {9, 10, 11, 0, 1, 2, 3};//這是之后的順序? ? static int mBridgeIndex = 0;//中間的坐標? ? static int[] mLeft = null;//左邊操作的數組? ? static int[] mRight = null;//右邊操作的數組? ? static int stepPosition = 0;//當前步的坐標? ? public static void main(String[] args) {? ? ? ? init();//初始化? ? ? ? start();//開始? ? }? ? private static void start() {? ? ? ? do {? ? ? ? ? ? switch (mStep[stepPosition]) {? ? ? ? ? ? ? ? case 1:? ? ? ? ? ? ? ? ? ? if (move(1, 1)) {? ? ? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? case 2:? ? ? ? ? ? ? ? ? ? if (move(1, 2)) {? ? ? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? case 3:? ? ? ? ? ? ? ? ? ? if (move(2, 1)) {? ? ? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? case 4:? ? ? ? ? ? ? ? ? ? if (!move(2, 2)) {? ? ? ? ? ? ? ? ? ? ? ? //reset mInput ,rollback mStep? ? ? ? ? ? ? ? ? ? ? ? rollback();? ? ? ? ? ? ? ? ? ? ? ? continue;? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? ? stepPosition++;? ? ? ? } while (!Arrays.equals(mResult, mInput));? ? ? ? outputStep();? ? }? ??//這個方法就是輸出運動的實況吧;? ??? ? private static void outputStep() {? ? ? ? int[] finallyStep = Arrays.copyOf(mStep, stepPosition);? ? ? ? reset();//? ? ? ? for (int i = 0; i < finallyStep.length; i++) {? ? ? ? ? ? switch (finallyStep[i]) {? ? ? ? ? ? ? ? case 1:? ? ? ? ? ? ? ? ? ? moveLeft(1);? ? ? ? ? ? ? ? ? ? System.out.printf(i + ": ?Left walking =>" + Arrays.toString(mInput) + "\n");? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? case 2:? ? ? ? ? ? ? ? ? ? moveLeft(2);? ? ? ? ? ? ? ? ? ? System.out.printf(i + ": ?Left jumping =>" + Arrays.toString(mInput) + "\n");? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? case 3:? ? ? ? ? ? ? ? ? ? moveRight(1);? ? ? ? ? ? ? ? ? ? System.out.printf(i + ": ?Right walking =>" + Arrays.toString(mInput) + "\n");? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? case 4:? ? ? ? ? ? ? ? ? ? moveRight(2);? ? ? ? ? ? ? ? ? ? System.out.printf(i + ": ?Right jumping =>" + Arrays.toString(mInput) + "\n");? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? }? ? ? ? }? ? }//這是初始化,初始化游戲的;? ? private static void init() {? ? ? ? mBridgeIndex = mInput.length / 2;// get middle bridge's position,用來獲取中間的坐標;? ? ? ? mLeft = Arrays.copyOfRange(mInput, 0, mBridgeIndex);//將左邊的數組用Arrays工具類的copy of range()方法將從0到中間復制到新數組中;? ? ? ? mRight = Arrays.copyOfRange(mInput, mBridgeIndex + 1, mInput.length);//同左;? ? ? ? Arrays.fill(mStep, 1);//將Setp數組用1填充;? ? }? ? /**? ? ?* move input child element .include all the way for move? ? ?*? ? ?* @param direction ? ?the direction for move. ?1 means left,2 means right? ? ?* @param stepInterval the way for move. 1 means walking,2 means jumping? ? ?*/? ? //這是運動的總類,包含了所有的移動方式;? ? private static boolean move(final int direction, final int stepInterval) {? ? //這里主要是發現是否有異常;? ? ? ? if (stepInterval != 1 && stepInterval != 2)? ? ? ? ? ? new IllegalArgumentException(" stepInterval must equal 1 or 2. ?-----> line 65");? ? ? ? if (direction != 1 && direction != 2)? ? ? ? ? ? new IllegalArgumentException(" direction must equal 1 or 2.-----> line 65");? ? ? ? boolean isWalking;? ? ? ? //這里要判斷移動的方式,有跳和走,有左有右? ? ? ? if (direction == 1) {? ? ? ? ? ? isWalking = moveLeft(stepInterval);? ? ? ? } else {? ? ? ? ? ? isWalking = moveRight(stepInterval);? ? ? ? }? ? ? ? //當被指向時候進行判斷;? ? ? ? if (isWalking) {? ? ? ? ? ? //record down step? ? ? ? ? ? record(direction, stepInterval);? ? ? ? }? ? ? ? return isWalking;? ? }? ? private static void record(int direction, int step) {? ? ? ? ? ? switch (direction) {? ? ? ? ? ? case 1:? ? ? ? ? ? ? ? mStep[stepPosition] = direction * step;? ? ? ? ? ? ? ? break;? ? ? ? ? ? case 2:? ? ? ? ? ? ? ? mStep[stepPosition] = direction + step;? ? ? ? ? ? ? ? break;? ? ? ? }? ? }? ? private static void rollback() {? ? ? ? if (mStep[stepPosition] >= 4) {? ? ? ? ? ? mStep[stepPosition] = 1;? ? ? ? ? ? stepPosition--;? ? ? ? }? ? ? ? if (stepPosition < 0)?? ? ? ? stepPosition = 0;? ? ? ? if (mStep[stepPosition] < 4) {? ? ? ? ? ? mStep[stepPosition]++;? ? ? ? ? ? reset();? ? ? ? }? ? }? ? /**? ? ?* move input child element .include ?the way for move is Left? ? ?*? ? ?* @param stepInterval the way for move. 1 means walking,2 means jumping? ? ?* @return if return false that means cannot moving? ? ?*/? ? //左向的方法? ? private static boolean moveLeft(final int stepInterval) {? ? ? ? for (int i = 0; i < mInput.length; i++) {? ? ? ? ? ? if (containOf(mLeft, mInput[i])) {// search item whether inside for mInput? ? ? ? ? ? ? ? if (i < mInput.length - stepInterval && mInput[i + stepInterval] == 0) {? ? ? ? ? ? ? ? ? ? //change item? ? ? ? ? ? ? ? ? ? int a = mInput[i];? ? ? ? ? ? ? ? ? ? mInput[i + stepInterval] = a;? ? ? ? ? ? ? ? ? ? mInput[i] = 0;? ? ? ? ? ? ? ? ? ? return true;? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? }? ? ? ? return false;? ? }? ? //右向的方法? ? private static boolean moveRight(final int stepInterval) {? ? ? ? for (int i = mInput.length - 1; i >= 0; i--) {? ? ? ? ? ? if (containOf(mRight, mInput[i])) {// search item whether inside for mInput? ? ? ? ? ? ? ? if (i >= 0 + stepInterval && mInput[i - stepInterval] == 0) {? ? ? ? ? ? ? ? ? ? //change item? ? ? ? ? ? ? ? ? ? mInput[i - stepInterval] = mInput[i];? ? ? ? ? ? ? ? ? ? mInput[i] = 0;? ? ? ? ? ? ? ? ? ? return true;? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? }? ? ? ? return false;? ? }? ? private static boolean containOf(int[] arr, int child) {? ? ? ? for (int i : arr) {? ? ? ? ? ? if (i == child)?? ? ? ? ? ? return true;? ? ? ? }? ? ? ? return false;? ? }? ? private static void reset() {? ? ? ? mInput[0] = 1;? ? ? ? mInput[1] = 2;? ? ? ? mInput[2] = 3;? ? ? ? mInput[3] = 0;? ? ? ? mInput[4] = 4;? ? ? ? mInput[5] = 5;? ? ? ? mInput[6] = 6;? ? ? ? stepPosition = 0;? ? }}
查看完整描述

1 回答

?
慕標5263832

TA貢獻11條經驗 獲得超3個贊

太長不閱

查看完整回答
反對 回復 2017-08-31
  • Arnao
    Arnao
    ......這題復雜一點,肯定長
  • 1 回答
  • 0 關注
  • 1081 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號