我用PHP和Java编写了一个程序,它生成所有可能的长度为2的单词.我使用了递归.为什么程序在Java中工作而在PHP中不工作?这是相同的代码.
Java的
package com.company;
public class Words {
public static void main(String[] args) {
generate("", 2);
}
static void generate(String prefix, int remainder) {
if (remainder == 0) {
System.out.println(prefix);
} else {
for (char c = 'A'; c <= 'Z'; c++) {
generate(prefix + c, remainder - 1);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
PHP
generate('', 2);
function generate($prefix, $remainder)
{
if ($remainder == 0) {
echo "$prefix\n";
} else {
for ($c = 'A'; $c <= 'Z'; $c++) {
generate($prefix …Run Code Online (Sandbox Code Playgroud) 我有一个关于 Kotlin 中泛型的使用的具体问题。
我想创建一个以泛型T作为参数的函数。name它使用它从类之一进行分配:Class1或Class2分配给局部变量testString。
不幸的是,只有当我使用 if 条件检查参数的类型时,这才有可能。
这会导致重复的代码。如果我尝试避免这种情况并使用第 12 行,我会在编译时收到此错误:Unresolved reference: name
当您要使用的类具有相同名称的相同属性时,Kotlin 中是否可以避免 if 条件并仅使用 testString 赋值一次?
代码:
fun main() {
val class1 = Class1("Foo1")
val class2 = Class2("Foo2")
}
class Class1(val name: String)
class Class2(val name: String)
fun <T> doStuff(classOneOrTwo: T) {
var testString: String
testString = classOneOrTwo.name //not working: Unresolved reference: name
if (classOneOrTwo is Class1) {
testString = classOneOrTwo.name
}
if (classOneOrTwo is Class2) { …Run Code Online (Sandbox Code Playgroud) 为什么这段代码可以毫无问题地终止?我认为它会输出一个 TypeError 异常,因为一个 Integer 不能被强制转换或转换为浮点类型的声明 strict_types。
?php
declare(strict_types=1);
function multiply(float $a, float $b): float {
return (double)$a * (double)$b;
}
$six = multiply(2, 3);
echo gettype($six);
//output: double
Run Code Online (Sandbox Code Playgroud) 我正在使用 JavaScript,我需要一个正则表达式来匹配“foo”之间的所有内容。
当我使用以下字符串时。
&foo=test1&foo=test2&foo=test3%20test4
Run Code Online (Sandbox Code Playgroud)
它应该返回
match1: test1
match2: test2
match3: test3%20test4
Run Code Online (Sandbox Code Playgroud)
我尝试了以下表达式
((&foo=)(.*))*
Run Code Online (Sandbox Code Playgroud)
但不幸的是它返回了整个字符串。
如何改进我的正则表达式?
我是 C 编程的初学者,如果我在 CLion 上开始一个项目,我会收到此错误代码:
C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\danie\CLionProjects\untitled2
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is unknown
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
CMake Error at CMakeLists.txt:2 (project):
The CMAKE_CXX_COMPILER:
g++.exe …Run Code Online (Sandbox Code Playgroud)