double r = 11.631;
double theta = 21.4;
Run Code Online (Sandbox Code Playgroud)
在调试器中,这些显示为11.631000000000000和21.399999618530273.
我怎么能避免这个?
public String addLetter(char letter, int position, char[] word){
char[]newWord = new char[word.length+1];
if(position == 0){
for(int i = position+1; i<word.length+1; i++){
newWord[i] = word[i-1];
}
newWord[position] = letter;
}else{
}
return new String(newWord);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个方法,它在字符串中添加一个字母,然后返回它.到目前为止,我已经能够在字符串的前面添加一个字符,但我不太确定如何在中间/结尾处这样做.在if条件下,我把每个字母都推到后面,所以前面有新信的空间.但是,如果我要在中间添加一些东西,任何提示,我不知道该怎么办?
我有一堆看起来像这样的字符串
/files/etc/hosts/2/ipaddr
/files/etc/hosts/2/canonical
/files/etc/hosts/2/alias[1]
/files/etc/hosts/3/ipaddr
/files/etc/hosts/3/canonical
/files/etc/hosts/3/alias[1]
/files/etc/hosts/4/ipaddr
/files/etc/hosts/4/canonical
/files/etc/hosts/4/alias[1]
Run Code Online (Sandbox Code Playgroud)
我想在/和之间的任何数字前面加一个0 /.在追加之后,结果看起来应该是这样的......
/files/etc/hosts/02/ipaddr
/files/etc/hosts/02/canonical
/files/etc/hosts/02/alias[1]
/files/etc/hosts/03/ipaddr
/files/etc/hosts/03/canonical
/files/etc/hosts/03/alias[1]
/files/etc/hosts/04/ipaddr
/files/etc/hosts/04/canonical
/files/etc/hosts/04/alias[1]
Run Code Online (Sandbox Code Playgroud)
我很确定我需要使用简单的正则表达式进行搜索.我认为/\d*/应该足够但我不知道如何修改字符串以插入数字0.有人可以给我一些建议吗?
我试图将一个字符串粘贴到另一个字符串的中间,例如:
String One = "MonkeyPony";
String Two = "Monkey";
Run Code Online (Sandbox Code Playgroud)
我如何将字符串二放入字符串1中,以便读取类似MonkeMonkeyyPony的内容?
编辑:我应该更清楚,基本上我要做的是将"Monkey"插入"MonkeyPony"中间很多次,所以第一次它会在第二次读取"MonkeMonkeyyPony"时会读到"MonkeMonMonkeykeyyPony"等