Could you help me find the reason
İ did methods that automaticly finds the number we have to add and adding it to our sum. My program finds the solution as 21151 but it suppose to be 21124 Theer is obviously a mistake but i couldn't find what causes this. İ putted small explanations in my code you can read it. I am not seeking for a new solution. I wanna know what is wrong in the code
[enter link description here][1] [1]: https://ift.tt/1r6qgHz
Here is my code :
public class Main {
public static void main(String[] args) {
// answer suppose to be 21124
// Problem's reason may be the exceptions like eleven twelve etc.
int toplam = 11; //onethousand
for (int i = 1; i < 1000; i++) {
// the place where we decide which method we are going to use
String sayiStr = String.valueOf(i);
if (sayiStr.length() == 1) {
toplam += birlik(sayiStr.charAt(0));
} else if (i < 20) {
toplam += betweenValues(String.valueOf(i));
} else if (sayiStr.length() == 2) {
toplam += onluk(sayiStr.charAt(0)) + birlik(sayiStr.charAt(1));
} else if (Integer.parseInt(sayiStr.substring(1)) < 20 && Integer.parseInt(sayiStr.substring(1)) > 10) {
toplam += betweenValues(sayiStr.substring(1)) + birlik(sayiStr.charAt(0)) + 10;
} else {//in this part we are adding +10 because we dont count and and hundred (andhundred) contains 10 letter
toplam += birlik(sayiStr.charAt(0)) + 10 + onluk(sayiStr.charAt(1)) + birlik(sayiStr.charAt(2));
}
}
System.out.println(toplam);
}
// The methods where we decide which number we will take
//eleven twelve 6
//sixteen fifteen 7
// thirteen fourteen eighteen nineteen 14
// seventeen 9
static int betweenValues(String value) {
if (value.equals("13") || value.equals("14") || value.equals("18") || value.equals("19")) {
return 8;
}
if (value.equals("11") || value.equals("12")) {
return 6;
}
if (value.equals("16") || value.equals("15")) {
return 7;
}
if (value.equals("10")) {
return 3;
}
if (value.equals("17")) {
return 9;
}
return -22000;
}
//1Ten 3
//4forty 5fifty 6sixty 5
//2Twenty 3Thirty 8eighty 9ninety 6
//7seventy 7
static int onluk(char deger) {
if (deger == '2' || deger == '3' || deger == '8' || deger == '9') {
return 6;
}
if (deger == '4' || deger == '5' || deger == '6') {
return 5;
}
if (deger == '1') {
return 3;
}
if (deger == '7') {
return 7;
}
return 0;
}
// One Two Six 3
//Four Five Nine 4
// Three Eight Seven 5
static int birlik(char deger) {
if (deger == '1' || deger == '2' || deger == '6') {
return 3;
}
if (deger == '4' || deger == '5' || deger == '9') {
return 4;
}
if (deger == '3' || deger == '8' || deger == '7') {
return 5;
}
return 0;
}
}
Aucun commentaire:
Enregistrer un commentaire