我试图让它返回一个值。我想要的是它返回在程序的其他部分自动生成的一堆数字的总和。
Error: [PDOException] SQLSTATE[HY000] [14] unable to open database file
Request URL: /mycontroller/
Stack Trace:
#0 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Database/Driver/PDODriverTrait.php(48): PDO->__construct('sqlite:/var/www...', NULL, NULL, Array)
#1 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Database/Driver/Sqlite.php(61): Cake\Database\Driver\Sqlite->_connect('sqlite:/var/www...', Array)
#2 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Database/Schema/BaseSchema.php(46): Cake\Database\Driver\Sqlite->connect()
#3 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Database/Dialect/SqliteDialectTrait.php(169): Cake\Database\Schema\BaseSchema->__construct(Object(Cake\Database\Driver\Sqlite))
#4 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Database/Schema/Collection.php(52): Cake\Database\Driver\Sqlite->schemaDialect()
#5 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Database/Schema/CachedCollection.php(44): Cake\Database\Schema\Collection->__construct(Object(Cake\Database\Connection))
#6 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Database/Connection.php(319): Cake\Database\Schema\CachedCollection->__construct(Object(Cake\Database\Connection), true)
#7 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/debug_kit/src/Model/Table/LazyTableTrait.php(40): Cake\Database\Connection->schemaCollection()
#8 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/debug_kit/src/Model/Table/RequestsTable.php(43): DebugKit\Model\Table\RequestsTable->ensureTables(Array)
#9 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/ORM/Table.php(285): DebugKit\Model\Table\RequestsTable->initialize(Array)
#10 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/ORM/TableRegistry.php(196): Cake\ORM\Table->__construct(Array)
#11 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/debug_kit/src/Routing/Filter/DebugBarFilter.php(186): Cake\ORM\TableRegistry::get('DebugKit.Reques...')
#12 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Event/EventManager.php(389): DebugKit\Routing\Filter\DebugBarFilter->afterDispatch(Object(Cake\Event\Event), Object(Cake\Network\Request), Object(Cake\Network\Response))
#13 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Event/EventManager.php(355): Cake\Event\EventManager->_callListener(Array, Object(Cake\Event\Event))
#14 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Event/EventManagerTrait.php(78): Cake\Event\EventManager->dispatch(Object(Cake\Event\Event))
#15 /var/www/vhosts/example.com/httpdocs/vendor/cakephp/cakephp/src/Routing/Dispatcher.php(92): Cake\Routing\Dispatcher->dispatchEvent('Dispatcher.afte...', Array)
#16 /var/www/vhosts/example.com/httpdocs/webroot/index.php(37): Cake\Routing\Dispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))
#17 {main}
当我这样做时,我得到:
public static double totalAmount(double probability, int numOfSuccesses){
for (int i = 0; i < numOfSuccesses; i++){
double total= 0;
total += probability;
}
}
return total;
答案 0 :(得分:3)
您的示例中有两个错误:return语句在函数范围之外,并且在循环中声明了total
。
它应该是这样的:
public static double totalAmount(double probability, int numOfSuccesses){
double total= 0;
for (int i = 0; i < numOfSuccesses; i++){
total += probability;
}
return total;
}
答案 1 :(得分:0)
您的函数只计算一个简单的乘法:
public static double totalAmount(double probability, int numOfSuccesses) {
return probability * numOfSuccesses;
}
嗯,有点复杂的方式。