关于 c :使用 if else 将数字转换为单词

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


在第二个 if 之后有一个分号 ;。此外,您应该使用 || 而不是 |。你也应该排除 ten==1.

时的情况

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";

        // ****
        if(unit==0 && ten ==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
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
 zero

Please enter any number between 0-99: 1
 one

Please enter any number between 0-99: 2
 two

Please enter any number between 0-99: 3
 three

Please enter any number between 0-99: 4
 four

Please enter any number between 0-99: 5
 five

Please enter any number between 0-99: 6
 six

Please enter any number between 0-99: 7
 seven

Please enter any number between 0-99: 8
 eight

Please enter any number between 0-99: 9
 nine

Please enter any number between 0-99: 10
ten

Please enter any number between 0-99: 11
eleven

Please enter any number between 0-99: 12
twelve

Please enter any number between 0-99: 13
thirteen

Please enter any number between 0-99: 14
fourteen

Please enter any number between 0-99: 15
fifteen

Please enter any number between 0-99: 16
sixteen

Please enter any number between 0-99: 17
seventeen

Please enter any number between 0-99: 18
eighteen

Please enter any number between 0-99: 19
ninteen

Please enter any number between 0-99: 20
twenty

Please enter any number between 0-99: 21
twenty one

Please enter any number between 0-99: 22
twenty two

Please enter any number between 0-99: 23
twenty three

Please enter any number between 0-99: 24
twenty four

Please enter any number between 0-99: 25
twenty five

Please enter any number between 0-99: 26
twenty six

Please enter any number between 0-99: 27
twenty seven

Please enter any number between 0-99: 28
twenty eight

Please enter any number between 0-99: 29
twenty nine

Please enter any number between 0-99: 30
thirty

Please enter any number between 0-99: 31
thirty one

Please enter any number between 0-99: 32
thirty two

Please enter any number between 0-99: 33
thirty three

Please enter any number between 0-99: 34
thirty four

Please enter any number between 0-99: 35
thirty five

Please enter any number between 0-99: 36
thirty six

Please enter any number between 0-99: 37
thirty seven

Please enter any number between 0-99: 38
thirty eight

Please enter any number between 0-99: 39
thirty nine

Please enter any number between 0-99: 40
fourty

Please enter any number between 0-99: 41
fourty one

Please enter any number between 0-99: 42
fourty two

Please enter any number between 0-99: 43
fourty three

Please enter any number between 0-99: 44
fourty four

Please enter any number between 0-99: 45
fourty five

Please enter any number between 0-99: 46
fourty six

Please enter any number between 0-99: 47
fourty seven

Please enter any number between 0-99: 48
fourty eight

Please enter any number between 0-99: 49
fourty nine

Please enter any number between 0-99: 50
fifty

Please enter any number between 0-99: 51
fifty one

Please enter any number between 0-99: 52
fifty two

Please enter any number between 0-99: 53
fifty three

Please enter any number between 0-99: 54
fifty four

Please enter any number between 0-99: 55
fifty five

Please enter any number between 0-99: 56
fifty six

Please enter any number between 0-99: 57
fifty seven

Please enter any number between 0-99: 58
fifty eight

Please enter any number between 0-99: 59
fifty nine

Please enter any number between 0-99: 60
sixty

Please enter any number between 0-99: 61
sixty one

Please enter any number between 0-99: 62
sixty two

Please enter any number between 0-99: 63
sixty three

Please enter any number between 0-99: 64
sixty four

Please enter any number between 0-99: 65
sixty five

Please enter any number between 0-99: 66
sixty six

Please enter any number between 0-99: 67
sixty seven

Please enter any number between 0-99: 68
sixty eight

Please enter any number between 0-99: 69
sixty nine

Please enter any number between 0-99: 70
seventy

Please enter any number between 0-99: 71
seventy one

Please enter any number between 0-99: 72
seventy two

Please enter any number between 0-99: 73
seventy three

Please enter any number between 0-99: 74
seventy four

Please enter any number between 0-99: 75
seventy five

Please enter any number between 0-99: 76
seventy six

Please enter any number between 0-99: 77
seventy seven

Please enter any number between 0-99: 78
seventy eight

Please enter any number between 0-99: 79
seventy nine

Please enter any number between 0-99: 80
eighty

Please enter any number between 0-99: 81
eighty one

Please enter any number between 0-99: 82
eighty two

Please enter any number between 0-99: 83
eighty three

Please enter any number between 0-99: 84
eighty four

Please enter any number between 0-99: 85
eighty five

Please enter any number between 0-99: 86
eighty six

Please enter any number between 0-99: 87
eighty seven

Please enter any number between 0-99: 88
eighty eight

Please enter any number between 0-99: 89
eighty nine

Please enter any number between 0-99: 90
ninty

Please enter any number between 0-99: 91
ninty one

Please enter any number between 0-99: 92
ninty two

Please enter any number between 0-99: 93
ninty three

Please enter any number between 0-99: 94
ninty four

Please enter any number between 0-99: 95
ninty five

Please enter any number between 0-99: 96
ninty six

Please enter any number between 0-99: 97
ninty seven

Please enter any number between 0-99: 98
ninty eight

Please enter any number between 0-99: 99
ninty nine

Please enter any number between 0-99: 100
Number is out of range


  • 您应该使用 int main() 而不是 main() 并在 int main() 中添加 return 0;
  • if(number<0 | number>99) 行应该是 if(number<0 || number>99)
  • if(number>=11 && number <=19); 行应该是 if(number>=11 && number <=19)
  • if(ten>=2 && ten<=9) 之前应添加 else
  • 以下 code 可以工作:

    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;
    }

    我还发现了以下错误:

  • 您在第一个 if 语句中使用按位或 | 而不是布尔值或 ||。您也不会在打印 "Number is out of range" 后停止其余代码,我已通过返回解决了这个问题。
  • 第二个 if 语句的末尾有一个分号,这个 if 语句也可以简化为 ten == 1
  • 您需要防止在打印 \\'teens 后运行其余代码,这可以通过 else 来完成。
  • 您永远不会打印 "ten",它可能应该与 \\'teens 一起打印,但会打印 20、30 等,但由于周围的 if 语句,它实际上永远不会被打印。
  • 您在 20、30 等之后打印"零"。如果 ten0,则仅打印 "zero" 可以解决此问题。
  • if 语句 if(unit>=0 && unit <=9) 将始终为真,因此可以删除。

  • 由于您的if条件,您得到的实际上是正确的。如果您写下 3 个 if 条件,它将检查其中的三个。另外,去掉第二个 if!

    的分号

    所以第一个 12 转到第一个 if 并打印 "twelve"。

    转到第二个 if12/101.2,所以作为一个 int,它是 `1。它不会进入第二个 if。由于分号,它无论如何都不会进入。

    但它进入第三个:12%10

    你的ifs就是这样!

    另外,我担心这对你有用:if(number<0 | number>99),因为它应该是 ||,而不是 |