[英]Calling a class in Android Studio
我在android studio中创建了一个将数字从10改为2的应用程序。我创建了以下算法,但是在调用该类并使其打印结果时遇到了麻烦。 谁能帮我 ? 提前致谢。
这是我的代码,
public class baseconv {
float m = Float.parseFloat(number.getText().toString());
int n = (int) m;
int pow = 1;
int x = 0;
public void calculate () {
while (n > 0) {
x = x + (n % 2)*pow;
n = n / 2;
pow = pow * 10;
}
}
}
要将以10为底的整数转换为以2为底的整数,请尝试以下操作:
String baseTwoString = Integer.toString(your_integer, 2);
但是如果您特别需要使用自定义算法,请继续阅读。
在您的活动中
String input = number.getText().toString();
try {
int n = (int) Float.parseFloat(input);
BaseConvert converter = new BaseConvert();
int output = converter.calculate(n);
Log.e("output : ", output);
} catch(NumberFormatException e) {
e.printStackTrace();
}
您的转换器课程,
public class BaseConvert {
public int calculate (int n) {
int pow = 1;
int x = 0;
while (n > 0) {
x = x + (n % 2) * pow;
n = n / 2;
pow = pow * 10;
}
return pow;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.