我已经写成了一个小小的(命令式) java程序,它基本上输出了从1到1000之间的所有法语书写的数字。(我正在学习如何写)
基于第一个数字(1-16)和几十个数字,它将下一个数字的单词链接在一起,并将它们写入一个一维数组中。
法国的计数系统有点不寻常,例如,从60到99的数字是基于20,而不是普通的小数点10。这意味着,在69之后的下一个数字(“soixante-中性”=> 60+9)是写出来的60+10 ("soixante-dix"),而不是仅仅70。
在程序中,我基本上使用了模块(例如,70 % 20 => 10)操作符来检测和打印特定的数字。
我的问题是
“守则”:
public class Main {
public static void main(String args[]) {
// i want to print also 1000
String[] french_num = new String[1001];
french_num[0] = "zero";
french_num[1] = "un";
french_num[2] = "deux";
french_num[3] = "trois";
french_num[4] = "quatre";
french_num[5] = "cinq";
french_num[6] = "six";
french_num[7] = "sept";
french_num[8] = "huit";
french_num[9] = "neuf";
french_num[10] = "dix";
french_num[11] = "onze";
french_num[12] = "douze";
french_num[13] = "treize";
french_num[14] = "quatorze";
french_num[15] = "quinze";
french_num[16] = "seize";
french_num[20] = "vingt";
french_num[30] = "trente";
french_num[40] = "quarante";
french_num[50] = "cinquante";
french_num[60] = "soixante";
french_num[80] = "quatre-vingt";
french_num[100] = "cent";
french_num[200] = "deux-cent";
french_num[300] = "trois-cent";
french_num[400] = "quatre-cent";
french_num[500] = "cinq-cent";
french_num[600] = "six-cent";
french_num[700] = "sept-cent";
french_num[800] = "huit-cent";
french_num[900] = "neuf-cent";
french_num[1000] = "mille";
int french_prefix;
int french_suffix;
// write only the numbers from 16 to 70 into the array
// 16 - 70
for (int i = 16; i < 70; i++) {
// use existing values if possible
if (french_num[i] == null) {
/*
in this context, french_suffix contains the last decimal digit, as 16-70 is base 10.
in this context, french_prefix contains the first decimal digit
for instance, the number 17:
=> french_suffix == 7 ("sept")
=> french_prefix == 10 ("dix")
*/
french_suffix = i % 10;
french_prefix = i - (i % 10);
// 0 - 69
if ((0 < i) && (i < 70)) {
if (i % 10 == 1) {
// 21, 31, etc. need to have "-et-un"
french_num[i] = french_num[french_prefix] + "-et-" + french_num[french_suffix];
} else {
french_num[i] = french_num[french_prefix] + "-" + french_num[french_suffix];
}
}
}
}
// write only the numbers from 69 to 99 into the array; as it can use the existing values of the
// former calculations. => needs just to add the tens
// 69 - 99
for (int i = 69; i < 100; i++) {
// use existing values if possible
if (french_num[i] == null) {
/*
in this context, french_suffix contains the last vigesimal digit
in this context, french_prefix contains the first vigesimal digit
for instance, the number 74:
=> french_suffix == 17 ("dix-sept")
=> french_prefix == 60 ("soixante")
*/
french_suffix = i % 20;
french_prefix = i - (i % 20);
french_num[i] = french_num[french_prefix] + "-" + french_num[french_suffix];
}
}
// write only the numbers from 100 to 1000 into the array; as it can use the existing values of the
// former calculations. => needs just to add the hundreds
// 100 - 1000
for (int i = 100; i < french_num.length; i++) {
// use existing values if possible
if (french_num[i] == null) {
/*
in this context, french_suffix contains the last vigesimal digit
in this context, french_prefix contains the first vigesimal digit
for instance, the number 343:
=> french_suffix == 43 ("quarante-trois")
=> french_prefix == 300 ("trois-cent")
*/
french_suffix = i % 100;
french_prefix = i - (i % 100);
if (i % 100 == 1) {
// prints all numbers, which have a one as the trailing digit. (e.g. 361, 101, etc.)
french_num[i] = french_num[french_prefix] + "-et-" + french_num[french_suffix];
} else {
// print all other numbers, which don't need special treatment. (e.g. 355, 693)
french_num[i] = french_num[french_prefix] + "-" + french_num[french_suffix];
}
}
}
// print the result
for (int i = 0; i < french_num.length; i++) {
System.out.println(french_num[i] + "," + i);
}
}
}发布于 2015-10-18 12:40:08
在这个特殊的例子中,我是如何从面向对象的编程中获益的,因为通常的oop优势,比如安全性、分离的前端/后端,在这里对我没有很好的帮助。(至少在我看来)
从更好地组织它开始。目前,您可以在一个main方法中完成所有操作。这并不理想。理想情况下,您应该遵循单一责任原则,并拥有诸如方法和类之类的程序元素,并负责单独完成一件事情。
例如:
FrenchCounting这样的东西会更好。main方法不同,有一个名为toFrench(int num)的方法是有意义的,它接受一个数字并返回一个String。这样,您实现的逻辑就会从其他类中变得更有用。french_num数组可以是类的(私有)静态字段。toFrench方法可以重用它,而不是每次重新创建它。使用上面的应用程序,类应该最终得到一个更类似于以下结构的结构:
public class FrenchCounting {
private static final Map<Integer, String> french;
static {
french = new HashMap<>();
french.put(0, "zero");
// ...
}
public static String toFrench(int num) {
// ...
}
public static void main(String args[]) {
for (int i = 0; i < 1000; ++i) {
System.out.println(toFrench(i) + " , " + i);
}
}
}这是更可重用的,有一个明确的目的,并更好地分开责任。
如何使程序总体上“更好”=>
填充1000个字符串数组,听起来不太好。您不需要存储所有计算出来的法国数字来打印它们。您可以在计算时打印它们,从而减少内存占用。
只存储不规则的数字就足够了,而那些可以作为组成其他数据的积木的数字也就足够了。在这个网站上有很多类似的关于用英语打印数字的问题,你可以从中学到很多东西,并采用类似法语的逻辑。
在Java中,变量的命名约定是camelCase,而不是snake_case。
https://codereview.stackexchange.com/questions/107929
复制相似问题