因此,我正在用C打井字游戏,但遇到一个问题,我要求用户按2来撤消他们的举动。每次轮到他们时,他们都可以撤消某步或继续进行操作。通过选择另一个键选择下一轮。
它应该如何工作,但是我遇到一个问题,如果用户输入字母字符,该程序只是反复循环通过说这不是数字来检查我的public void OnTrackableStateChanged(TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus){
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED){
Debug.Log("Image Found");
}
else{
Debug.Log("Image lost!");
}
//
StateManager stateManager = TrackerManager.Instance.GetStateManager();
IEnumerable<TrackableBehaviour> trackableBehaviours = stateManager.GetActiveTrackableBehaviours();
foreach (TrackableBehaviour trackableBehaviour in trackableBehaviours)
{
string scanName = trackableBehaviour.TrackableName;
Transform.GetComponent<Text>().text = scanName;
if(scanName == "scannedImage")
{
SceneManager.LoadScene("ScannedImageScene");
}
else if(scanName == "otherImage")
{
SceneManager.LoadScene("OtherImageScene");
}
}
}
函数。没有人知道如何使它停止执行此操作,只是说这不是一个数字,如果不循环程序的话?
这是我的代码:
GetHumanMove
答案 0 :(得分:1)
scanf
不会占用与您的格式不匹配的输入。它保留在输入流中。因此,当您输入字母时,它与您想要的数字%d
不匹配,并停留在输入流中。
scanf("%d%c", &userInput, &term);
代替scanf
,使用getline
来消耗整个输入行会更简单。然后使用sscanf
(函数的基于字符串的版本)或其他方法来解析结果。
char *line = NULL;
size_t len = 0;
while ((read = getline(&line, &len, stdin)) != -1) {
// Do something with line
int scanned = sscanf(line, "%d%c", &userInput);
if (scanned == EOF || scanned != 2) {
printf("Line failed to match\n");
}
}
答案 1 :(得分:1)
只需阅读手册页:
The format string consists of a sequence of directives which describe
how to process the sequence of input characters. If processing of a
directive fails, no further input is read, and scanf() returns. A
"failure" can be either of the following: input failure, meaning that
input characters were unavailable, or matching failure, meaning that
the input was inappropriate (see below).
您不检查scanf()
的返回值。因此,您始终尝试扫描与数字相同的非数字。