我新的Java和我的任務是用掃描儀在陣列中,而在另一個method.This一個int閱讀是我到目前爲止有:如何使用掃描儀(BlueJ的)
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n: ");
int sz = scanner.nextInt();
System.out.println("Enter locations of stones: ");
int[] array = new int[sz];
for (int i=0;i<array.length;i++){
array[i] = scanner.nextInt();
}
jump(array,sz);
}
我想從開始這樣的閱讀方法:
public static void jump(int [] array, int amtStone){
int x =0, moveOK =1, jumpedLoc =0, jumpedCount =1;
//x: counter, moveOK: when 1, it is ok to continue. Otherwise, the jump is impossible.
//jumpedLoc: to keep track of the current location of the rabbit
//jumpCount: to count the amount of jumps
while (x<amtStone)
{if(array[x+1]-array[x]<=50){
jumpedLoc = array[x+1];
jumpedCount++;}
}
if (moveOK ==1)
System.out.println(jumpedCount);
else
System.out.println("-1");
}
我在做什麼是計算的最小數量的跳躍需要一隻兔子到達河對岸。數組中的整數表示從起點開始的石頭距離作爲河流的一側,另一個int表示石塊的數量。兔子可以跳的最大距離是50。
用於輸入和輸出:
輸入n:7(輸入,結石在河數) 32 46 70 85 96 123 145(輸入,距離在石頭和起點之間,最後一個數字是河流的寬度,即目的地(河流的另一邊)和起點之間的距離) 輸出:3(這是最小的次數a兔子可以跳)
如果不可能,則輸出爲-1。
當我運行主要方法,輸入int和數組後,沒有輸出,程序沒有繼續。我不確定接下來要做什麼。
你期望的輸出?爲什麼? – 2014-09-27 01:46:54
是什麼讓你認爲問題不在'jump()'方法?你有什麼例外嗎?您發佈的代碼沒有問題。你可以發佈跳轉方法嗎? – qbit 2014-09-27 01:49:39
那麼,這是一個漫長的故事。我正在做的是計算達到最後一個數字所需的最小跳數(作爲河流的另一邊)。數組中的整數代表河流中的石頭位置,另一個int代表石頭的數量。跳躍的最長距離爲50. – Melody 2014-09-27 01:55:22