mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Fixed some variable names
This commit is contained in:
@@ -55,7 +55,9 @@ public class Problem17 extends Problem{
|
||||
//Start with 1 and increment
|
||||
for(int num = START_NUM;num <= STOP_NUM;++num){
|
||||
//Pass the number to a function that will create a string for the number
|
||||
String currentNumString = getStringFromNum(num);
|
||||
String currentNumString = makeWordFromNum(num);
|
||||
//int count = getNumberChars(currentNumString);
|
||||
//System.out.println(String.format("%4d.Word: %-35s Letters: %d", num, currentNumString, count));
|
||||
//Pass the string to the function that will count the number of letters in it, ignoring whitespace and punctuation and add that number to the running tally
|
||||
letterCount += getNumberChars(currentNumString);
|
||||
}
|
||||
@@ -73,42 +75,43 @@ public class Problem17 extends Problem{
|
||||
letterCount = 0;
|
||||
}
|
||||
//This function makes a word out of the number passed into it
|
||||
private String getStringFromNum(int number){
|
||||
private String makeWordFromNum(int number){
|
||||
int num = number;
|
||||
StringBuilder numberString = new StringBuilder();
|
||||
//Starting with the largest digit create a string based on the number passed in
|
||||
//Check for negative
|
||||
if(number < 0){
|
||||
if(num < 0){
|
||||
numberString = numberString.append("negative ");
|
||||
}
|
||||
|
||||
//Check if the number is zero
|
||||
if(number == 0){
|
||||
if(num == 0){
|
||||
numberString = numberString.append("zero");
|
||||
}
|
||||
|
||||
//Start with the thousands place
|
||||
if((number / 1000D) >= 1D){
|
||||
numberString = numberString.append(getStringFromNum((int)Math.floor(number / 1000D)));
|
||||
if((num / 1000D) >= 1D){
|
||||
numberString = numberString.append(makeWordFromNum((int)Math.floor(num / 1000D)));
|
||||
numberString = numberString.append(" thousand");
|
||||
number -= ((int)Math.floor(number / 1000D)) * 1000;
|
||||
num -= ((int)Math.floor(num / 1000D) * 1000);
|
||||
}
|
||||
|
||||
//Check the hundreds place
|
||||
if((number / 100D) >= 1D){
|
||||
numberString = numberString.append(getStringFromNum((int)Math.floor(number / 100D)));
|
||||
if((num / 100D) >= 1D){
|
||||
numberString = numberString.append(makeWordFromNum((int)Math.floor(num / 100D)));
|
||||
numberString = numberString.append(" hundred");
|
||||
number -= ((int)Math.floor(number / 100D)) * 100;
|
||||
num -= ((int)Math.floor(num / 100D) * 100);
|
||||
}
|
||||
|
||||
//Insert an and if there is need
|
||||
if((!numberString.toString().isBlank() && (number > 0))){
|
||||
if((!numberString.toString().isBlank() && (num > 0))){
|
||||
numberString = numberString.append(" and ");
|
||||
}
|
||||
|
||||
//Check for tens place
|
||||
if((number / 10D) >= 2D){
|
||||
if((num / 10D) >= 2D){
|
||||
//For the tens you need to do something special
|
||||
int tensPlace = (int)Math.floor(number / 10D);
|
||||
int tensPlace = (int)Math.floor(num / 10D);
|
||||
switch(tensPlace){
|
||||
case 9: numberString.append("ninety"); break;
|
||||
case 8: numberString.append("eighty"); break;
|
||||
@@ -119,15 +122,15 @@ public class Problem17 extends Problem{
|
||||
case 3: numberString.append("thirty"); break;
|
||||
case 2: numberString.append("twenty"); break;
|
||||
}
|
||||
number -= (tensPlace * 10);
|
||||
num -= (tensPlace * 10);
|
||||
//If there is something left in the number you will need a dash to separate the tens and ones place
|
||||
if(number > 0){
|
||||
if(num > 0){
|
||||
numberString = numberString.append("-");
|
||||
}
|
||||
}
|
||||
//Check for teens
|
||||
else if((number / 10D) >= 1D){
|
||||
int onesPlace = (number % 10);
|
||||
else if((num / 10D) >= 1D){
|
||||
int onesPlace = (num % 10);
|
||||
switch(onesPlace){
|
||||
case 9: numberString.append("nineteen"); break;
|
||||
case 8: numberString.append("eighteen"); break;
|
||||
@@ -141,12 +144,12 @@ public class Problem17 extends Problem{
|
||||
case 0: numberString.append("ten"); break;
|
||||
}
|
||||
//If this was hit the number was completed
|
||||
number = 0;
|
||||
num = 0;
|
||||
}
|
||||
|
||||
//Check for the ones place
|
||||
if(number >= 1){
|
||||
switch(number){
|
||||
if(num >= 1){
|
||||
switch(num){
|
||||
case 9: numberString.append("nine"); break;
|
||||
case 8: numberString.append("eight"); break;
|
||||
case 7: numberString.append("seven"); break;
|
||||
@@ -158,7 +161,12 @@ public class Problem17 extends Problem{
|
||||
case 1: numberString.append("one"); break;
|
||||
}
|
||||
//If this was hit the number was completed
|
||||
number = 0;
|
||||
num = 0;
|
||||
}
|
||||
|
||||
//If the number is not 0 there was a problem
|
||||
if(num != 0){
|
||||
throw new Unsolved("The number " + number + "was not completely reduced!\nRemainder is " + num);
|
||||
}
|
||||
|
||||
//Return the string
|
||||
|
||||
Reference in New Issue
Block a user