Converting number into words using if else
我是 c 的初学者。我编写了一个程序来将数字转换为单词。对于 11-19 之间的数字和像这样的数字 20、30、40、50、60、70、80、90,我得到了错误的输出。我还尝试在最后两个 if 语句之间添加 else 语句,这样即使第一个 if 为真,它也不会执行所有 if 语句,但它给了我一个错误,即 “else without a previous if”。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
//This program converts number into words
#include<iostream> using namespace std; main() { int number,unit,ten; cout<<"Please enter any number between 0-99:"; cin>>number; ten=number/10; unit=number%10; if(number<0 | number>99) cout<<"Number is out of range"; if(number>=11 && number <=19); { if(number==11) cout<<"eleven"; if(number==12) cout<<"twelve"; if(number==13) cout<<"thirteen"; if(number==14) cout<<"fourteen"; if(number==15) cout<<"fifteen"; if(number==16) cout<<"sixteen"; if(number==17) cout<<"seventeen"; if(number==18) cout<<"eighteen"; if(number==19) cout<<"ninteen"; } if(ten>=2 && ten<=9) { if(ten==1) cout<<"ten"; if(ten==2) cout<<"twenty"; if(ten==3) cout<<"thirty"; if(ten==4) cout<<"fourty"; if(ten==5) cout<<"fifty"; if(ten==6) cout<<"sixty"; if(ten==7) cout<<"seventy"; if(ten==8) cout<<"eighty"; if(ten==9) cout<<"ninty"; } if(unit>=0 && unit <=9) { if(unit==0) cout<<" zero"; if(unit==1) cout<<" one"; if(unit==2) cout<<" two"; if(unit==3) cout<<" three"; if(unit==4) cout<<" four"; if(unit==5) cout<<" five"; if(unit==6) cout<<" six"; if(unit==7) cout<<" seven"; if(unit==8) cout<<" eight"; if(unit==9) cout<<" nine"; } } |
输出1:-
1
2 |
Please enter any number between 0–99: 12
twelve two |
输出2:-
1
2 |
Please enter any number between 0–99: 30
thirty zero |
在第二个
时的情况
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
//This program converts number into words #include<iostream> using namespace std; main() { int number,unit,ten; cout<<"Please enter any number between 0-99:"; cin>>number; ten=number/10; unit=number%10; if(number<0 || number>99) { cout<<"Number is out of range"; } if(ten == 1) { if(number==10) cout<<"ten"; if(number==11) cout<<"eleven"; if(number==12) cout<<"twelve"; if(number==13) cout<<"thirteen"; if(number==14) cout<<"fourteen"; if(number==15) cout<<"fifteen"; if(number==16) cout<<"sixteen"; if(number==17) cout<<"seventeen"; if(number==18) cout<<"eighteen"; if(number==19) cout<<"ninteen"; } else { if(ten==2) cout<<"twenty"; if(ten==3) cout<<"thirty"; if(ten==4) cout<<"fourty"; if(ten==5) cout<<"fifty"; if(ten==6) cout<<"sixty"; if(ten==7) cout<<"seventy"; if(ten==8) cout<<"eighty"; if(ten==9) cout<<"ninty"; // **** |
所有情况的输出:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
Please enter any number between 0–99: –1 Number is out of range Please enter any number between 0–99: 0 Please enter any number between 0–99: 1 Please enter any number between 0–99: 2 Please enter any number between 0–99: 3 Please enter any number between 0–99: 4 Please enter any number between 0–99: 5 Please enter any number between 0–99: 6 Please enter any number between 0–99: 7 Please enter any number between 0–99: 8 Please enter any number between 0–99: 9 Please enter any number between 0–99: 10 Please enter any number between 0–99: 11 Please enter any number between 0–99: 12 Please enter any number between 0–99: 13 Please enter any number between 0–99: 14 Please enter any number between 0–99: 15 Please enter any number between 0–99: 16 Please enter any number between 0–99: 17 Please enter any number between 0–99: 18 Please enter any number between 0–99: 19 Please enter any number between 0–99: 20 Please enter any number between 0–99: 21 Please enter any number between 0–99: 22 Please enter any number between 0–99: 23 Please enter any number between 0–99: 24 Please enter any number between 0–99: 25 Please enter any number between 0–99: 26 Please enter any number between 0–99: 27 Please enter any number between 0–99: 28 Please enter any number between 0–99: 29 Please enter any number between 0–99: 30 Please enter any number between 0–99: 31 Please enter any number between 0–99: 32 Please enter any number between 0–99: 33 Please enter any number between 0–99: 34 Please enter any number between 0–99: 35 Please enter any number between 0–99: 36 Please enter any number between 0–99: 37 Please enter any number between 0–99: 38 Please enter any number between 0–99: 39 Please enter any number between 0–99: 40 Please enter any number between 0–99: 41 Please enter any number between 0–99: 42 Please enter any number between 0–99: 43 Please enter any number between 0–99: 44 Please enter any number between 0–99: 45 Please enter any number between 0–99: 46 Please enter any number between 0–99: 47 Please enter any number between 0–99: 48 Please enter any number between 0–99: 49 Please enter any number between 0–99: 50 Please enter any number between 0–99: 51 Please enter any number between 0–99: 52 Please enter any number between 0–99: 53 Please enter any number between 0–99: 54 Please enter any number between 0–99: 55 Please enter any number between 0–99: 56 Please enter any number between 0–99: 57 Please enter any number between 0–99: 58 Please enter any number between 0–99: 59 Please enter any number between 0–99: 60 Please enter any number between 0–99: 61 Please enter any number between 0–99: 62 Please enter any number between 0–99: 63 Please enter any number between 0–99: 64 Please enter any number between 0–99: 65 Please enter any number between 0–99: 66 Please enter any number between 0–99: 67 Please enter any number between 0–99: 68 Please enter any number between 0–99: 69 Please enter any number between 0–99: 70 Please enter any number between 0–99: 71 Please enter any number between 0–99: 72 Please enter any number between 0–99: 73 Please enter any number between 0–99: 74 Please enter any number between 0–99: 75 Please enter any number between 0–99: 76 Please enter any number between 0–99: 77 Please enter any number between 0–99: 78 Please enter any number between 0–99: 79 Please enter any number between 0–99: 80 Please enter any number between 0–99: 81 Please enter any number between 0–99: 82 Please enter any number between 0–99: 83 Please enter any number between 0–99: 84 Please enter any number between 0–99: 85 Please enter any number between 0–99: 86 Please enter any number between 0–99: 87 Please enter any number between 0–99: 88 Please enter any number between 0–99: 89 Please enter any number between 0–99: 90 Please enter any number between 0–99: 91 Please enter any number between 0–99: 92 Please enter any number between 0–99: 93 Please enter any number between 0–99: 94 Please enter any number between 0–99: 95 Please enter any number between 0–99: 96 Please enter any number between 0–99: 97 Please enter any number between 0–99: 98 Please enter any number between 0–99: 99 Please enter any number between 0–99: 100 |
以下
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
//This program converts number into words #include<iostream> using namespace std; int main() { int number,unit,ten; cout<<"Please enter any number between 0-99:"; cin>>number; ten=number/10; unit=number%10; if(number<0 || number>99) cout<<"Number is out of range"; if(number>=11 && number <=19) { if(number==11) cout<<"eleven"; if(number==12) cout<<"twelve"; if(number==13) cout<<"thirteen"; if(number==14) cout<<"fourteen"; if(number==15) cout<<"fifteen"; if(number==16) cout<<"sixteen"; if(number==17) cout<<"seventeen"; if(number==18) cout<<"eighteen"; if(number==19) cout<<"ninteen"; } else { if(ten>=2 && ten<=9) { if(ten==1) cout<<"ten"; if(ten==2) cout<<"twenty"; if(ten==3) cout<<"thirty"; if(ten==4) cout<<"fourty"; if(ten==5) cout<<"fifty"; if(ten==6) cout<<"sixty"; if(ten==7) cout<<"seventy"; if(ten==8) cout<<"eighty"; if(ten==9) cout<<"ninty"; } if(unit>=0 && unit <=9) { if(unit==0) cout<<" zero"; if(unit==1) cout<<" one"; if(unit==2) cout<<" two"; if(unit==3) cout<<" three"; if(unit==4) cout<<" four"; if(unit==5) cout<<" five"; if(unit==6) cout<<" six"; if(unit==7) cout<<" seven"; if(unit==8) cout<<" eight"; if(unit==9) cout<<" nine"; } } return 0; |
对于这种情况,switch 语句更有意义。试试这个代码:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#include<iostream>
using namespace std; int main() { for (int number = 1; number <= 99; number++) { int ten=number/10; int unit=number%10; if(number<0 || number>99) { cout<<"Number is out of range"; return 1; } if(ten == 1) { switch ( number ) { case 10: cout<<"ten"; break; case 11: cout<<"eleven"; break; case 12: cout<<"twelve"; break; case 13: cout<<"thirteen"; break; case 14: cout<<"fourteen"; break; case 15: cout<<"fifteen"; break; case 16: cout<<"sixteen"; break; case 17: cout<<"seventeen"; break; case 18: cout<<"eighteen"; break; case 19: cout<<"nineteen"; break; } } else { if(ten>=2 && ten<=9) { switch (ten) { case 2: cout<<"twenty"; break; case 3: cout<<"thirty"; break; case 4: cout<<"fourty"; break; case 5: cout<<"fifty"; break; case 6: cout<<"sixty"; break; case 7: cout<<"seventy"; break; case 8: cout<<"eighty"; break; case 9: cout<<"ninty"; break; } } if ( unit != 0 and ten != 0 ) { cout <<""; } switch ( unit ) { case 0: if ( ten == 0 ) cout<<"zero"; break; case 1: cout<<"one"; break; case 2: cout<<"two"; break; case 3: cout<<"three"; break; case 4: cout<<"four"; break; case 5: cout<<"five"; break; case 6: cout<<"six"; break; case 7: cout<<"seven"; break; case 8: cout<<"eight"; break; case 9: cout<<"nine"; break; } } cout <<"/ "; } return 0; } |
我还发现了以下错误:
由于您的
的分号
所以第一个 12 转到第一个
转到第二个
但它进入第三个:
你的
另外,我担心这对你有用:
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/268928.html