我是C ++的初学者。我写了一个程序,将数字转换成单词。对于11-19和20、30、40、50、60、70、80、90之类的数字,我的输出错误。我还尝试在最后两个if语句之间添加else语句,以便即使第一个if值为true也不执行所有if语句,但是它给我一个错误,“否则就没有前面的if”。
//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:-
Please enter any number between 0-99: 12
twelve two
输出2:-
Please enter any number between 0-99: 30
thirty zero
答案 0 :(得分:2)
第二个;
之后是分号if
。另外,您应该使用||
而不是|
。另外,您应排除ten==1
时的情况。
//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";
}
}
所有情况下的输出:
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
答案 1 :(得分:1)
int main()
而不是main()
,并在return 0;
中添加int main()
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
可能会起作用:
//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;
}
答案 2 :(得分:0)
由于您的if
条件,您得到的实际上是正确的。如果写下3个if
条件,它将检查三个条件。另外,如果第二个要用分号!
因此第一个数字12转到第一个if
并显示“十二”。
转到第二个if
。 12/10
是1.2
,因此作为整数,它是`1。如果第二个则不行。由于分号,它无论如何都不会进行。
但它在第三项中使用:12%10
。
就像您在ifs
中一样!
此外,我担心这对您有用:if(number<0 | number>99)
,应该是||
,而不是|
。
答案 3 :(得分:0)
在这种情况下,使用switch语句更有意义。尝试以下代码:
#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 << "\n";
}
return 0;
}
我还发现了以下错误:
|
而不是布尔值或||
。在打印"Number is out of range"
之后,您也不会停止其余代码,我已经通过返回修复了该问题。ten == 1
。else
来完成。"ten"
,它可能应该用'teens来打印,但是要用20、30等打印。但是由于周围的if语句,它实际上不会被打印。"zero"
为ten
,则只能通过打印0
来解决此问题。if(unit>=0 && unit <=9)
将始终为true,因此可以将其删除。