正如所描述的标题我想要创建一个函数,告诉我两个数字我给出的是友好的,但由于某种原因我得到了错误的答案,我希望有人会看到问题.
public class Amicable{
public static void main(String[] args){
int n, m;
int ndivisorsSum = 0;
int mdivisorsSum = 0;
n = Integer.parseInt(args[0]);
m = Integer.parseInt(args[1]);
for(int i = 1; i < n; i++){
if (n % i == 0){
ndivisorsSum = ndivisorsSum + i;
}
}
for(int i = 1; i < m; i++){
if (m % i == 0){
mdivisorsSum = mdivisorsSum + i;
}
}
if (ndivisorsSum == mdivisorsSum) {
System.out.println(n + " and " + m …Run Code Online (Sandbox Code Playgroud) 就像标题所说,我做了一个小函数,从用户字符串1和0取出,输出应该是它的十进制数,但由于某种原因我得到了错误的答案,希望你能帮我找到问题.
public class ToDecimal{
public static void main(String[] args){
String string = args[0];
int length = string.length();
int power = length - 1;
int decimal = 0;
while(length > 0){
if (string.charAt(length - 1) == '1'){
decimal = decimal + (int)(Math.pow(2, length - 1));
} else if (string.charAt(length - 1) == '0'){
} else {
System.out.println("Wrong string spelling...");
}
length--;
}
System.out.println(decimal);
}
Run Code Online (Sandbox Code Playgroud)
}