Focus on the code inside the While Loop
System.out.println("\nThis program is built to enhance your Base Changing skills"); System.out.println("Change the Base 10 number to the Base n number"); System.out.println("Enter the number of equations to work on below:"); int amount = scan.nextInt(); int a = 0; int correct = 0; int wrong = 0; int score = 0; double start = System.currentTimeMillis(); while(a < amount){ int x = 100 + (int)(Math.random()*899); int n = 2 + (int)(Math.random()*7); int last = x % n; int next1 = (int)((x/n))%n; int next2 = (int)((x/(n*n)))%n; int next3 = (int)((x/(n*n*n)))%n; int next4 = (int)((x/(n*n*n*n)))%n; int next5 = (int)((x/(n*n*n*n*n)))%n; int next6 = (int)((x/(n*n*n*n*n*n)))%n; int next7 = (int)((x/(n*n*n*n*n*n*n)))%n; int next8 = (int)((x/(n*n*n*n*n*n*n*n)))%n; int next9 = (int)((x/(n*n*n*n*n*n*n*n*n)))%n; int first = (int)((x/(n*n*n*n*n*n*n*n*n*n)))%n; int answer1 = (last*1)+(next1*10)+(next2*100); int answer2 = (next4*1000)+(next5 *10000)+(next6*100000)+(next7*1000000); int answer3 = (next8*10000000)+(next9*100000000)+(first*1000000000); int answer = answer1 + answer2 + answer3; System.out.println(x + " base 10 to base " + n + " _____"); System.out.println(answer1); System.out.println(answer2); System.out.println(answer3); System.out.println(answer); System.out.println(last); System.out.println(next1); System.out.println(next2); System.out.println(next3); System.out.println(next4); System.out.println(next5); System.out.println(next6); System.out.println(next7); System.out.println(next8); System.out.println(next9); System.out.println(first); int reply = scan.nextInt(); if(reply == answer){ System.out.println("Correct!"); correct++; score += 5; } else{ System.out.println("Wrong!"); wrong++; score -= 9; } amount--; } double now = System.currentTimeMillis(); double time = (now-start)/1000; System.out.println("\nSTATISTICS:"); System.out.println("Correct: " + correct); System.out.println("Wrong: " + wrong); System.out.println("Score: " + score); System.out.println("Time taken: " + time + " seconds"); System.out.println("Time per Question: " + time/(wrong + correct) + " seconds");
I've made this (admittedly poorly done) algorithm to change from Base 10 to Base n. However, once I get around the 1,000's place. Some issues begin to arise. I printed out all the values for the variables... say I get:
last = 1
next1 = 2
next2 = 2
next3 = 1
The answer will give 221, instead of 1221. Any help please? (Also, sometimes it works perfectly fine. Usually anything past 3 digits will result in a "false" answer"