在C语言中,我编写了一个名为Kernel的程序,这是一个特殊的程序,用于帮助多人工作和生育子女。
我想将每个函数转换为Python或Java,请给我一些提示,(我使用CS50沙箱,所以有些函数看起来很奇怪。)
printf("Talk is the most basic function here. I will just imitate what you typed.\n");
string speech = get_string("What to say: ");
printf("What I said: %s\n", speech);
printf("What do you think?\n");
此功能将重复输入。这非常容易,因此您不必在此给我提示。
Python版本:
print("Talk is the most basic function here. I will just imitate what you typed.")
speech = input("What to say: ")
print(f"What I said: {speech}")
print("What do you think?")
Java版本:
System.out.println("Talk is the most basic function here. I will just imitate what you typed.")
System.out.println("What to say: ");
Scanner speech;
speech = new Scanner(System.in);
System.out.println("What I said: " + speech);
printf("Wanna teach your kids how to count? Kernel can help you!\n");
int numbers = get_int("Numbers: ");
printf("Processing...\n");
for (int i = 1; i <= numbers; i ++)
{
printf("%i\n", i);
if (i == 50)
{
int COUNT_MAX = 3;
printf("ERROR_CODE_CLASS: 3 (COUNT_MAX)\n");
return COUNT_MAX;
}
}
printf("What do you think?\n");
输入一个数字,它将为您计数。如果达到50,则会出现错误消息。对于Java来说非常容易,我在使用Python时遇到了问题。由于我信任的人给出的答案,因此现在已更改。
Python版本:
print("Wanna teach your kids how to count? Kernel can help you!")
numbers = int(input("Numbers: "))
print("Processing...")
for i in range(1, numbers + 1):
print(i)
if i == 50:
COUNT_MAX = 3
print("ERROR_CODE_CLASS: 3 (COUNT_MAX)")
return COUNT_MAX
print("What do you think?")
Java版本:
System.out.println("Wanna teach your kids how to count? Kernel can help you!");
Scanner numbers;
numbers = new Scanner(System.in);
System.out.println("Processing...");
for (int i = 1; i <= numbers; i++) {
System.out.println(i)
if (i == 50) {
int COUNT_MAX = 3;
System.out.println("ERROR_CODE_CLASS: 3 (COUNT_MAX)");
return COUNT_MAX;
}
}
System.out.println("What do you think?");
更多即将推出... (将进行编辑)
这里有很多方法可以解决第二个示例。我认为最相似的一种可能是更改:
for i in range(numbers):
至
for i in range(1, numbers + 1):
与您发布的Java版本同等。在这种情况下,第一个值是起始值,第二个值是范围的终止值。它创建如下输出:
print([i for i in range(1, 3 + 1)])
# prints([1, 2, 3])
一般而言,您的问题措辞很差,将来最好提交多个不同的问题。这些也是非常简单的示例,大多数语言文档将带您通过非常相似的概念进行操作,这些概念可以推论到您的用例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句