其中:Roteer = 旋转, 链接=左,法律=右......
看起来,如果您在 roteerNaarLinks 中输入“Oliebol”,3,输出将是“bololie”,这是应该发生的。但是,如果您在 roteerNaarRechts 中输入“Oliebol”,1,输出将是“lOliebo”,但它应该变成“llieboo”,我该如何解决这个问题? :)
public class Roteer {
public static String roteerNaarLinks(String invoer, int n){
String in = invoer;
int aantal = n;
for(int i = 0; i< aantal; i++){
char firstLetter = in.charAt(0);
in = in.substring(1);
in = in + firstLetter;
}
return in;
}
public static String roteerNaarRechts(String invoer, int n){
String in = invoer;
int aantal = n;
for(int y = n; y > aantal; y--){
char lastLetter = in.charAt(in.length()-1);
in = in.substring(1);
in = lastLetter + in;
}
return in;
}
}
最佳答案
for
永远不会在 roteerNaarRechts
中执行。您设置了aantal = n
,然后设置了y = n
,然后您说循环应该在y > aantal
时执行,但事实并非如此。将其更改为
for(int y = n; y >= 0; y--)
关于Java-Chars 可以向左旋转但不能向右旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26607077/