i writing program convert base 10 integer base 2-16. here terms:
"the method convert decimal value user selected whatever base user selected , print converted value 1 place value @ time using index array of characters 0-9 amd a-f initialized contain characters. in method find largest place value (i.e. power of base) divide decimal number. can set loop operate power down , including 0th power determine how many times each place value goes decimal number. using loop counter, index character array print character corresponds quotient number , subtract product of place value , quotient decimal number. power (loop index) decreased, repeat until finish 0th place value.
here's have far:
import java.io.*; import java.util.scanner; public class convertit {//start program public static void main(string[] args) {//start main scanner input = new scanner(system.in); system.out.println("enter positive integer 0 10000."); int number = input.nextint(); system.out.println("enter base convert to."); int base = input.nextint(); convertit(number, base); }//end main public static void convertit(int number, int base) {//start method int array = {0123456789abcdef}; while (number != 0) {//start while }//end while (int = array.length-1; > -1; i--) {//start }//end }//end while }//end method }//end program
i think have set correctly, i'm not sure how attack it. understand concept of i'm doing, not execution of it.
what this:
import java.lang.stringbuilder; import java.util.scanner; public class convertit { public static void main(string[] args) { scanner input = new scanner(system.in); system.out.println("enter positive integer 0 10000."); int number = input.nextint(); system.out.println("enter base convert to."); int base = input.nextint(); input.close(); convertit(number, base); } public static void convertit(int number, int base) { if ( base <= 1 || base > 16) return; char[] array = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; string convertednum = ""; { convertednum += array[number % base]; number = number / base; } while (number != 0); convertednum = new stringbuilder(convertednum).reverse().tostring(); system.out.println(convertednum); } }
edit: @ first didn't catch wanted without using string. try provide solution well, think it's kind of messy. have preferred using string instead, maybe because don't know how store values in cleaner way.
import java.util.arraylist; import java.util.scanner; public class convertit { public static void main(string[] args) { scanner input = new scanner(system.in); system.out.println("enter positive integer 0 10000."); int number = input.nextint(); system.out.println("enter base convert to."); int base = input.nextint(); input.close(); convertit(number, base); } public static void convertit(int number, int base) { if (base <= 1 || base > 16) return; char[] array = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; arraylist<integer> converted = new arraylist<integer>(); // convert number { converted.add(number % base); number = number / base; } while (number != 0); int count = converted.size() - 1; int temp; // revert numbers (int = (count - 1) >> 1; < 0; --i) { temp = converted.get(i); converted.set(i, converted.get(count - i)); converted.set(count - i, temp); } // print numbers (int = 0; < converted.size(); i++) { system.out.print(array[converted.get(i)]); } } }
Comments
Post a Comment