السلام عليكم
ممكن مساعدة في تصحيح المحاولة لحل السؤال ( السؤال في المرفقات ) ..
For example:
if the input was "BAD" and 16, the output would be 2989
if the input was "1011" and 2, the output would be 11
تكملة السؤال ...
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
import java.lang.*;
public class HelloWorld
{
public static void main(String[] args)
{
String theAnswer = HelloWorld.decimalToBase(2989, 2);
System.out.println(HelloWorld.binaryToDecimal("1011"));
}
static int binaryToDecimal(String s)
{
int sum = 0;
int place = 1;
for(int i = s.length()-1; i >= 0; i = i - 1)
{
if(s.charAt(i) == '1')
{
sum = sum + place;
}
place = place * 2;
}
return sum;
}
static String decimalToBase(int n, int radix)
{
String answer = "";
while(n > 0)
{
answer = "0123456789ABCDEF".charAt(n%radix) + answer;
n = n / radix;
}
return answer;
}
static void printNumber(int num)
{
System.out.println(num);
}
}
وشكرا