我是新来的,正在从事cs50的第二个作业Caesar,看来我的大部分评论都是正确的,除了最后一个-我无法处理缺少argv [1]的情况,这意味着我只能输入。 / caesar,它将返回分段错误。我想知道为什么当argc == 1时,这段代码var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(0, window.innerWidth / window.innerHeight, 0.1, 1000);
var color = new THREE.Color(0xff0000);
scene.background = color;
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var geometry = new THREE.BoxGeometry(1,1,1);
var material = new THREE.MeshBasicMaterial(
{
color: 0x00ff00
}
)
var cube = new THREE.Mesh( geometry, material);
scene.add( cube );
camera.position.z = 5;
function animate(){
requestAnimationFrame( animate );
renderer.render( scene, camera );
}
不能返回0,但是当argc> 1时它可以工作,我觉得很奇怪。谁能帮我??预先感谢!
// BT Rename
//
final String sNewName = "Syntactics";
final BluetoothAdapter myBTAdapter = BluetoothAdapter.getDefaultAdapter();
final long lTimeToGiveUp_ms = System.currentTimeMillis() + 10000;
if (myBTAdapter != null)
{
String sOldName = myBTAdapter.getName();
if (sOldName.equalsIgnoreCase(sNewName) == false)
{
final Handler myTimerHandler = new Handler();
myBTAdapter.enable();
myTimerHandler.postDelayed(
new Runnable()
{
@Override
public void run()
{
if (myBTAdapter.isEnabled())
{
myBTAdapter.setName(sNewName);
if (sNewName.equalsIgnoreCase(myBTAdapter.getName()))
{
Log.i(TAG_MODULE, "Updated BT Name to " + myBTAdapter.getName());
myBTAdapter.disable();
}
}
if ((sNewName.equalsIgnoreCase(myBTAdapter.getName()) == false) && (System.currentTimeMillis() < lTimeToGiveUp_ms))
{
myTimerHandler.postDelayed(this, 500);
if (myBTAdapter.isEnabled())
Log.i(TAG_MODULE, "Update BT Name: waiting on BT Enable");
else
Log.i(TAG_MODULE, "Update BT Name: waiting for Name (" + sNewName + ") to set in");
}
}
} , 500);
}
}
答案 0 :(得分:0)
我知道这是怎么回事!因为我需要将一些值传递给atoi()
,所以如果我仅调用./caesar,则没有可以传递给atoi()
的值,因此会导致分段错误。这意味着我需要稍微更改代码顺序,将int numberkey = atoi(key);
放入else循环中。因此,该代码将首先运行if (argc != 2)
,如果没有,则转到下一步!这是更改后的代码。
int check_the_key(int argc, string y)
{
int number = argc;
string key = y;
if (argc != 2)
{
return 0;
}
else
{
int numberkey = atoi(key);
if (numberkey > 0)
{
return numberkey;
}
else
{
return 0;
}
}
}