2012-09-09 26 views
0

我已經嘗試了很多組合,並在網上搜索了一些有用的東西,但不幸的是我沒有發現任何有用的東西。Java'編寫必要的代碼以「左旋」它們的值:'

這是我的作業。只有問題我不能回答69個問題。

問:

四個整數變量,POS1,POS2,POS3,POS4已宣佈和初始化。編寫必要的代碼以「左旋」它們的值:爲每個變量獲取連續變量的值,並且pos4獲得pos1的值。

的例子,我試過:

int tempP1 = pos1; 
int tempP2 = pos2; 
int tempP3 = pos3; 
int tempP4 = pos4; 

pos4 = tempP1; 
pos3 = tempP2; 
pos2 = tempP3; 
pos1 = tempP4; 

其顯示的內容我:

Remarks: 
     ⇒     Your code had an error during execution 

More Hints: 
     ⇒     Are you sure you want to use: tempP1 
     ⇒     Are you sure you want to use: tempP2 
     ⇒     Are you sure you want to use: tempP3 

Problems Detected: 
     ⇒     pos1 has wrong value 
     ⇒     pos3 has wrong value 

回答

3
pos4 = tempP1; 
pos2 = tempP3; 
pos3 = tempP4; 
pos1 = tempP2; 

聽上去不錯?

+0

謝謝你,像魅力一樣工作 – MOTIVECODEX

0

的代碼將是: pos4=tempP1; pos3=tempP4; pos2=tempP3; pos1=tempP2;

2
int tempP1 = pos1; 
int tempP2 = pos2; 
int tempP3 = pos3; 
int tempP4 = pos4; 

pos4 = tempP1; 
pos3 = tempP4; 
pos2 = tempP3; 
pos1 = tempP2; 
0

您可以只聲明一個單一的溫度值,而不是創造四個不同tempValue小號凝結代碼更好:

int tempValue = 0; 


tempValue = pos1; 

pos1 = pos2; 

pos2 = pos3; 

pos3 = pos4; 

pos4 = tempValue; 
-1
int a=pos4; 
int b=pos3; 
int c=pos2; 
pos4=pos1; 
pos3=a; 
pos2=b; 
pos1=c; 
1
int tmp = pos1; 
pos1 = pos2; 
pos2 = pos3; 
pos3 = pos4; 
pos4 = tmp; 
相關問題