0

## JAVA 新手問題 請問我錯在哪裡 100求解~(已解決 感謝)

public class AntiNumbers
{

``````public static void main(String[] args)
{
// TODO Auto-generated method stub

System.out.println(antiNum(-23) );
}
public static int antiNum (int x)
{
if (x<0)
{
char[] numArray=new StringBuffer(x).deleteCharAt(0).toString().toCharArray();
char[] cRet = new char[numArray.length];
int j =0;
for(int i =numArray.length-1;i<0;i--)
{
cRet[j]=numArray[i];
j++;
}
int ansNum = Integer.valueOf(String.valueOf(cRet));
ansNum =Math.negateExact(ansNum);
return ansNum ;
}else if(x>0)
{
char[] numArray=new StringBuffer(x).toString().toCharArray();
char[] cRet = new char[numArray.length];
int j =0;
for(int i =numArray.length-1;i<0;i--)
{
cRet[j]=numArray[i];
j++;
}
int ansNum = Integer.valueOf(String.valueOf(cRet));
return ansNum ;
}
else
{
int ansNum=0;
return ansNum ;
}

}
``````

}

### 3 個回答

0

iT邦新手 4 級 ‧ 2021-06-09 11:43:12

``````char[] numArray=new StringBuffer(x).deleteCharAt(0).toString().toCharArray();
``````

Java Doc

StringBuffer(int capacity)
Constructs a string buffer with no characters in it and the specified initial capacity.

``````char[] numArray=new StringBuffer().append(x).deleteCharAt(0).toString().toCharArray();
``````

soloo951 iT邦新手 5 級 ‧ 2021-06-09 23:04:34 檢舉

soloo951 iT邦新手 5 級 ‧ 2021-06-09 23:22:33 檢舉

4

iT邦大師 1 級 ‧ 2021-06-09 05:43:40
``````for(int i =numArray.length-1;i<0;i--)
``````

``````for(int i =numArray.length-1;i>=0;i--)
``````

soloo951 iT邦新手 5 級 ‧ 2021-06-09 23:05:00 檢舉

0

iT邦大神 1 級 ‧ 2021-06-09 08:38:24

soloo951 iT邦新手 5 級 ‧ 2021-06-09 23:13:19 檢舉

``````public class HelloWorld{
public static void main(String []args){
System.out.println(antiNum(23) );
System.out.println(antiNum(-23) );
}
public static int antiNum (int x) {
if (x<0) {
char[] numArray = ("" + x).toCharArray();
char[] cRet = new char[numArray.length-1];
int j =0;
for(int i =numArray.length-1;i>0;i--) {
cRet[j]=numArray[i];
j++;
}
int ansNum = Integer.valueOf(String.valueOf(cRet));
ansNum =Math.negateExact(ansNum);
return ansNum ;
} else if(x>0) {
char[] numArray = ("" + x).toCharArray();
char[] cRet = new char[numArray.length];
int j =0;
for(int i=numArray.length-1;i>=0;i--) {
cRet[j]=numArray[i];
j++;
}
int ansNum = Integer.valueOf(String.valueOf(cRet));
return ansNum ;
}
else {
int ansNum=0;
return ansNum ;
}
}
}
``````