Java '编写必要的代码来“左旋转”它们的值:'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12341042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:29:31  来源:igfitidea点击:

Java 'Write the code necessary to "left rotate" their values:'

java

提问by MOTIVECODEX

I've tried a lot of combinations and have searched on the web for something useful but unfortunately I've found nothing useful.

我尝试了很多组合,并在网上搜索了一些有用的东西,但不幸的是我没有发现任何有用的东西。

This is for my homework. Only question I cannot answer out of 69 questions.

这是我的家庭作业。在 69 个问题中,只有我无法回答的问题。

Question:

问题:

Four integer variables, pos1, pos2, pos3, pos4 have been declared and initialized. Write the code necessary to "left rotate" their values: for each variable to get the value of the successive variable, with pos4 getting pos1's value.

四个整型变量 pos1、pos2、pos3、pos4 已经声明并初始化。编写必要的代码来“左旋转”它们的值:对于每个变量来获取后续变量的值,pos4 获取 pos1 的值。

Example that I've tried:

我试过的例子:

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

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

What it shows me:

它向我展示了什么:

    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

回答by Peter Svensson

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

Sounds right?

听起来对吗?

回答by The code god

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

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

回答by InnaS

int tmp = pos1;
pos1 = pos2;
pos2 = pos3;
pos3 = pos4;
pos4 = tmp;

回答by Darren Burns

You can condense your code better by only declaring a single temp value rather than creating four different tempValues:

您可以通过仅声明一个临时值而不是创建四个不同的tempValues 来更好地压缩代码:

int tempValue = 0;


tempValue = pos1;

pos1 = pos2;

pos2 = pos3;

pos3 = pos4;

pos4 = tempValue;

回答by Tushar Paliwal

The code would be : pos4=tempP1; pos3=tempP4; pos2=tempP3; pos1=tempP2;

代码将是: pos4=tempP1; pos3=tempP4; pos2=tempP3; pos1=tempP2;

回答by InnaS

int a=pos4;
int b=pos3;
int c=pos2;
pos4=pos1;
pos3=a;
pos2=b;
pos1=c;