当前位置:编程学习 > JAVA >>

新手进制转换问题

public class TestScale {

public static void main(String[] args) {
int a = 26;                    //想要转换的十进制数
int b = 8;                     //想要转换为八进制
MyScale m = new MyScale();
m.change(a, b);
}

}

class MyScale {
int s[];
int i = 0;
public void change (int a,int b) {
do{
s[i] = (a%b);
a = (a/b);
i++;
}while(a/b!=0);
System.out.println(s);
}
}

为什么总会报错啊
Exception in thread "main" java.lang.NullPointerException
at MyScale.change(TestScale.java:18)
at TestScale.main(TestScale.java:8)
--------------------编程问答-------------------- s[] 没有 初始化。。。 --------------------编程问答-------------------- 其实十进制转八进制一句话搞定。。。

System.out.println(Integer.toOctalString(26));
--------------------编程问答--------------------
引用 2 楼 flagiris 的回复:
其实十进制转八进制一句话搞定。。。

System.out.println(Integer.toOctalString(26));

知道,就想写写 --------------------编程问答--------------------
引用 1 楼 flagiris 的回复:
s[] 没有 初始化。。。

这个怎么初始化啊 --------------------编程问答--------------------
引用 4 楼 jgh563825026 的回复:
Quote: 引用 1 楼 flagiris 的回复:

s[] 没有 初始化。。。

这个怎么初始化啊

类似
int s[] = new int[2];

不过你的逻辑也对吧。。。 --------------------编程问答--------------------
引用 4 楼 jgh563825026 的回复:
Quote: 引用 1 楼 flagiris 的回复:

s[] 没有 初始化。。。

这个怎么初始化啊

直接定义s[20] --------------------编程问答--------------------
引用 5 楼 flagiris 的回复:
Quote: 引用 4 楼 jgh563825026 的回复:

Quote: 引用 1 楼 flagiris 的回复:

s[] 没有 初始化。。。

这个怎么初始化啊

类似
int s[] = new int[2];

不过你的逻辑也对吧。。。

没有反着输出,不过这样都出问题了,就没有写那个。能在代码上改改吗 --------------------编程问答--------------------
引用 7 楼 jgh563825026 的回复:
Quote: 引用 5 楼 flagiris 的回复:

Quote: 引用 4 楼 jgh563825026 的回复:

Quote: 引用 1 楼 flagiris 的回复:

s[] 没有 初始化。。。

这个怎么初始化啊

类似
int s[] = new int[2];

不过你的逻辑也对吧。。。

没有反着输出,不过这样都出问题了,就没有写那个。能在代码上改改吗



public class TestScale {

public static void main(String[] args) {
int a = 26; // 想要转换的十进制数
int b = 8; // 想要转换为八进制
MyScale m = new MyScale();
m.change(a, b);
}
}

class MyScale {
int s[] = new int[2];

int i = 0;

public void change(int a, int b) {
do {
s[i] = (a % b);
a = (a / b);
i++;
} while (a != 0);
for (int i = s.length - 1; i >= 0; i--) {
System.out.print(s[i]);
}
}
}


另外因为位数不一定,最好用list型的,不要用数组。。。 --------------------编程问答--------------------
引用 8 楼 flagiris 的回复:
Quote: 引用 7 楼 jgh563825026 的回复:

Quote: 引用 5 楼 flagiris 的回复:

Quote: 引用 4 楼 jgh563825026 的回复:

Quote: 引用 1 楼 flagiris 的回复:

s[] 没有 初始化。。。

这个怎么初始化啊

类似
int s[] = new int[2];

不过你的逻辑也对吧。。。

没有反着输出,不过这样都出问题了,就没有写那个。能在代码上改改吗



public class TestScale {

public static void main(String[] args) {
int a = 26; // 想要转换的十进制数
int b = 8; // 想要转换为八进制
MyScale m = new MyScale();
m.change(a, b);
}
}

class MyScale {
int s[] = new int[2];

int i = 0;

public void change(int a, int b) {
do {
s[i] = (a % b);
a = (a / b);
i++;
} while (a != 0);
for (int i = s.length - 1; i >= 0; i--) {
System.out.print(s[i]);
}
}
}


另外因为位数不一定,最好用list型的,不要用数组。。。

为什么输出的会是[I@6cd9c6e2呢 --------------------编程问答--------------------
引用 9 楼 jgh563825026 的回复:
Quote: 引用 8 楼 flagiris 的回复:

Quote: 引用 7 楼 jgh563825026 的回复:

Quote: 引用 5 楼 flagiris 的回复:

Quote: 引用 4 楼 jgh563825026 的回复:

Quote: 引用 1 楼 flagiris 的回复:

s[] 没有 初始化。。。

这个怎么初始化啊

类似
int s[] = new int[2];

不过你的逻辑也对吧。。。

没有反着输出,不过这样都出问题了,就没有写那个。能在代码上改改吗



public class TestScale {

public static void main(String[] args) {
int a = 26; // 想要转换的十进制数
int b = 8; // 想要转换为八进制
MyScale m = new MyScale();
m.change(a, b);
}
}

class MyScale {
int s[] = new int[2];

int i = 0;

public void change(int a, int b) {
do {
s[i] = (a % b);
a = (a / b);
i++;
} while (a != 0);
for (int i = s.length - 1; i >= 0; i--) {
System.out.print(s[i]);
}
}
}


另外因为位数不一定,最好用list型的,不要用数组。。。

为什么输出的会是[I@6cd9c6e2呢


我的还是你的代码?你直接print数组,又没重写tostring方法,当然打印的是对象啊。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,