- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在学习 C,现在我想从键盘获得输入(2 个数字范围 [0,99] 并比较它们)。我发现了很多获取输入的选项:
我尝试了其中的一些:
扫描:
#include<stdio.h>
main()
{
int flag=0,num1, num2;
printf("Give a number between 0-999");
do{
if(flag == 1){
printf("Wrong Input. Plz give a number between 0-99 ");
}
printf("\nnumber 1: ");
scanf("%d", &num1);
flag=1;
}while(num1<0 || num1>1000);
printf("Give one more");
flag=0;
do{
if(flag == 1){
printf("Wrong Input. Plz give a number between 0-99 ");
}
printf("\nnumber 2: ");
scanf("%d", &num2);
flag=1;
}while(num2<0 || num2>1000);
if(num1>num2){
printf("the max number is : %d\n", num1);
}else
if (num1<num2){
printf("the max number is : %d\n", num2);
}else
printf("they are equals\n");
但是,如果我按的不是数字,它会无限打印:
number 2: Wrong Input. Plz give a number between 0-99
number 2: Wrong Input. Plz give a number between 0-99
number 2: Wrong Input. Plz give a number between 0-99
// and goes on..
然后我尝试使用getchar:
#include<stdio.h>
main()
{
int flag=0;
char input1,input2;
printf("Give a number between 0-999");
do{
if(flag == 1){
printf("Wrong Input. Plz give a number between 0-99 ");
}
printf("\nnumber 1: ");
input1 = getchar();
flag=1;
}while(input1<0 || input1>1000);
printf("Give one more");
flag=0;
do{
if(flag == 1){
printf("Wrong Input. Plz give a number between 0-99 ");
}
printf("\nnumber 2: ");
input1 = getchar();
flag=1;
}while(input2<0 || input2>1000);
if(input1>input2){
printf("the max number is : %c\n", input1);
}else
if (input1<input2){
printf("the max number is : %c\n", input2);
}else
printf("they are equals\n");
}
但是这段代码不起作用。有什么建议吗?
最佳答案
scanf
返回成功扫描的值的数量,或返回 -1
错误。因此,如果 scanf
没有返回 1
,则说明出现了问题,您应该采取相应措施。
getchar
的问题在于它会返回一个字符(或 EOF
出错)。因此,如果您输入数字,则只会返回第一个数字,并且作为字符而不是数字。第二次调用 getchar
将返回第一个输入的第二个数字,作为一个字符。
我建议输入函数看起来像这样:
int get_input(void)
{
/* Infinite loop */
for (;;)
{
printf("Please enter a value between 1 and 9999 (inclusive): ");
fflush(stdout);
char buffer[256];
/* Read input from the user */
if (fgets(buffer, sizeof(buffer), stdin) == NULL)
{
perror("Error getting input");
return -1;
}
int value;
/* Extract an integer value from the input */
if (sscanf(buffer, "%d", &value) != 1)
{
printf("Your input was not a valid value.\n");
continue; /* Try to get input again */
}
/* Check for a valid value */
if (value >= 1 && value <= 9999)
return value; /* Got a value that is allowed */
printf("Not a valid value.\n");
/* Since we're in a loop, will continue from the beginning by asking for a value */
}
return -1; /* Must return a value */
}
调用此函数获取一个值,如果返回-1则出错:
int value1;
if ((value1 = get_input()) == -1)
exit(1); /* Error reading value */
关于c - 我应该使用哪个函数从键盘获取输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19540338/
我遇到了一个问题。我正在使用 pyinstaller 将代码导出到 .exe。代码包括 tkinter、PIL 和 keyboard 模块。软件在我使用 Python 的 PC 上运行完美,而在没有
我正在尝试将 STM32F0-disco 用作 Windows PC 的键盘。正在打印的字符有问题。 下面的代码等到板载按钮被按下,然后应该打印一次这三个字符。 /* USER CODE BE
我想在单击键盘上的“完成”按钮时退出键盘。我怎样才能做到这一点?我有以下代码=> textView.returnKeyType = UIReturnKeyDone; 我有这个代码用于限制 TextVi
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我想调出一个用于输入电话号码的键盘。有没有办法在我自己的 Activity 中显示此对话框并捕获用户输入的内容。我需要能够控制用户点击调用时发生的情况。所以我很感兴趣自定义允许用户输入电话号码的 Ac
我希望软键盘位于我的布局之上,而不是在它弹出时四处移动我的布局。我该怎么做呢?我查看了 list ,但从未设置 android:windowSoftInputMode="adjustResize" 此
主题:将某些键替换为另一个键值。 例如如果我按 P,它应该是 F24。 当我尝试从 .ini 文件加载键值时, Hook 不再是全局的。它仅在 winapi 窗体处于焦点时才有效。 我的 DLL 代码
当我点击 EditText 时,是否可以用小写字母启动 android 键盘?基本上,我想要的是将 EditText 中的第一个字母小写,但也让用户可以根据需要将其变为大写...... (EditTe
我想监控 IOS 上的键盘隐藏按钮并触发一个事件。我在说: 我只想在用户按下实际按钮时进行监控。我不想要键盘隐藏时的事件。 最佳答案 用户键盘通知观察者.. 对于 swift NSNotificati
在经历了这一切之后就像认真的...... Easy way to dismiss keyboard? ...我有多个 TextFields 和一些 TextViews。有没有办法对所有文本字段进行批处
所以我想在我的应用程序中添加一个键盘,其中包含表情符号,就像 Whatsapp 或环聊一样。我怎样才能做到这一点?我想保留我的键盘,因为我只想添加标签来放置表情符号。我认为软键盘很容易支持它,但到目前
首先,我会让您知道,我在 StackOverflow 以及 Google 和 Github 上查看了很多很多不同的帖子。我已经到处寻找对我有帮助的任何。但是,似乎没有任何效果。它要么已经过时(超过 1
当按下相应的键时,我试图让我的游戏中的宇宙飞船 (PlayerShip.gif) 向左、向右、向上和向下移动。我知道我需要一个 keyboardListener 但我在弄清楚它的去向以及它是如何实际实
我正在尝试制作一个 HID USB 设备。我搜索了一下,发现键盘的输出有 8 个字节。第一个字节是修饰符,第二个字节是保留字节,其余 6 个字节是关键代码。我认为在某些情况下,例如“prtsc”,需要
我最近开始学习 Vim,在深入学习之前,我有一个问题需要回答。 使用 AZERTY 键盘,我是否应该重新映射命令和快捷方式的键以适应 QWERTY 键盘的键位置? 我之所以问这个,是因为显然在创建这些
我一直认为在使用 Dvorak 布局之前,我需要购买 Dvorak 键盘。但是我在亚马逊上找不到。仅仅是从 Qwerty 键盘上弹出键并移动它们吗? 最佳答案 为了帮助您了解键盘布局,您可以重新排列
我不敢相信我还没有找到任何关于此的文档,但我想知道如何命令键盘激活并接收来自它的输入。我可以找到在编辑文本字段时操作弹出键盘的所有示例。谢谢 最佳答案 您还可以使用 UIKeyInput 协议(pro
我有一个 UITextField,其中弹出的键盘已禁用其 Shift 键。键盘类型设置为 UIKeyboardTypeNamePhonePad,看起来应该允许大写。 如果我将键盘类型更改为“默认”但保
背景:我的表单有一个 TWebBrowser。我想用 ESC 关闭表单,但 TWebBrowser 吃掉了击键 - 所以我决定使用键盘 Hook 。 问题是表单可以同时在多个实例中打开。 无论我做什么
我需要(即客户要求)提供自定义键盘,供用户在文本字段和区域中输入文本。我已经有一些可以执行键盘操作并将测试附加到文本字段的东西,但是我想让它更通用并让它像标准的 iphone 键盘一样工作,即当用户选
我是一名优秀的程序员,十分优秀!