#include<iostream> #include<math.h> using namespace std; int num1(int a) { int t = a; int num = 1;//纪录输入数据的位数 for (int i = 0; i < 100; i++) { if (t / 10 > 0) { num++; t /= 10; } else { break; } } return num; } void show(int a){ int t = a; int n; int num = num1(a); while (num-- > 0) { n = a / (int)pow(10.0, num); switch (n) { case 0: cout << " ling"; break; case 1: cout << " yi"; break; case 2: cout << " er"; break; case 3: cout << " san"; break; case 4: cout << " si"; break; case 5: cout << " wu"; break; case 6: cout << " liu"; break; case 7: cout << " qi"; break; case 8: cout << " ba"; break; case 9: cout << " jiu"; break; } a = a % (int)pow(10.0, num); } cout << "\n"; } int main() { int a;//输入的数据 int t;//暂时储存a的值 cin >> a; t = a; if (a < 0) { cout << "fu"; a *= (-1); show(a); } else { int num = num1(a); int n = a / (int)pow(10.0, (num-1)); switch (n) { case 0: cout << "ling"; break; case 1: cout << "yi"; break; case 2: cout << "er"; break; case 3: cout << "san"; break; case 4: cout << "si"; break; case 5: cout << "wu"; break; case 6: cout << "liu"; break; case 7: cout << "qi"; break; case 8: cout << "ba"; break; case 9: cout << "jiu"; break; } a = a % (int)pow(10.0,(num-1)); show(a); } return 0; }
这个题目一直没能完全实现,难点在于他的输出格式和适用性
我打的这个代码虽然格式正确但是适用性不足,如果出现1025这种数据中间有0存在就无法输出正确结果
网课今天看了类的构造方法和C不同的是JAVA在构造方法前也要加上public
还看了私有成员,私有成员的每一个成员前都要加上private
标签:7.11,构造方法,int,num,include,num1 From: https://www.cnblogs.com/xuxingkai/p/17545866.html