大家早上好,我写了一个非常简单的应用程序,我必须等待用户按下某个键(例如 F2)才能继续处理。按下按钮的等待周期工作正常,但我需要帮助的问题是:如果在按下按钮的等待周期中,我点击了另一个应用程序,我的应用程序失去了焦点,我无法再重新获得控制权。即使我点击应用程序的任何部分,表单的标题始终保持灰色,应用程序仍然在等待按键的压力,如果我按下所需的键,显然什么也没有发生。如何重新控制我的应用程序?我想我会在 while 循环中放置诸如 SetFocus、StayOnTop、SetForegroundWindow 等指令,以始终保持我的应用程序处于活动状态,但我无法组合任何东西...
var
Form2: TForm2;
mkey: word;
implementation
{$R *.dfm}
procedure TForm2.FormActivate(Sender: TObject);
begin
label1.Visible:= false;
mkey:= 0;
keyPreview:= true;
while mkey <> VK_F2 do begin
label1.Visible:= true;
label1.Caption:= datetimetostr(now); // for example
application.ProcessMessages;
end;
label1.Visible:= false;
end;
procedure TForm2.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
mkey:= key;
end;
end.
Run Code Online (Sandbox Code Playgroud) 所以我正在学习如何在 C 中使用 while 和 for 循环,但这段代码似乎不起作用。scanf 语句似乎被忽略了,循环只是重复自己而不需要我输入“Y”来重复。这是代码:
void showCommission();
void main() {
char keepGoing='y';
while(keepGoing=='y') {
showCommission();
printf("Do you want to calculate another?\n");
scanf("%c",&keepGoing);
}
}
void showCommission() {
float sales,commission;
const float COM_RATE=0.10;
printf("Enter the amount of sales\n");
scanf("%f",&sales);
commission=sales*COM_RATE;
printf("The commission is $%f.\n",commission);
}
Run Code Online (Sandbox Code Playgroud)
这是运行代码给我的内容:
Enter the amount of sales
5000
The commission is $500.000000.
Do you want to calclulate another?
...Program finished with exit code 10
Press ENTER to exit console.
Run Code Online (Sandbox Code Playgroud)
它从不提示我输入 y 并且代码只是出于某种原因退出。
对不起,我的愚蠢问题,我已经 7 年没有编程了,几乎忘记了一切。
我想做一个可以表达音符的系统,例如我创建了以下枚举,
public enum Notes { C = 0, D, E, F, G, A, B };
Notes N = Notes.C;
while (true)
{
Console.WriteLine(N);
N++;
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中,一旦 Note 的 N 到达 Note.B,值就不会回到 C。如何强制值回到 Note.C?
你的回答对我很有帮助。
此致。
我声明了一个向量并尝试放置大小和值并打印它
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int s;
cin>>s; //taking size of vector
vector <int> arr(s);
int input;
while (cin >> input)
{arr.push_back(input);} //inserting the values in array
for(int i=0;i<s;i++)
cout<<" "<<arr[i]; //printing the values
}
Run Code Online (Sandbox Code Playgroud)
我的输入 5
1 2 3 4 5
预期输出
1 2 3 4 5
电流输出 0 0 0 0 0
对不起,如果这是一个重复的问题或听起来很愚蠢,但我对 c# 真的很陌生,并且在整个论坛中查看并找不到任何我能真正理解的东西。
所以我正在尝试编写一个简单的程序,让用户尝试猜测 1 到 25 之间的数字。除了每次循环运行而不是更新循环最后一次运行的分数,如 0+1= 1、1+1=2、2+1=3,每次都是1加0。这是我的代码。我该如何解决?谢谢!
int score = 0;
int add = 1;
while (add == 1)
{
Console.WriteLine("Guess A Number Between 1 and 25");
string input = Console.ReadLine();
if (input == "18")
{
Console.WriteLine("You Did It!");
Console.WriteLine("Not Bad! Your Score was " + score + add);
break;
}
else
{
Console.WriteLine("Try Again. Score: " + score + add);
}
}
Run Code Online (Sandbox Code Playgroud) 我读到当迭代次数未知时使用 while 循环,当迭代次数已知时使用 for 循环......例如当我必须打印 5 次语句时,我必须在 while 和 for 循环中初始化变量并且我还需要在 (i++) 中都给出迭代条件,我还必须提到迭代条件,以便它只循环 5 次......那么有什么区别?有人可以举例说明吗
for(int i=0;i<5;i++){
cout<<"Hello";
}
int i=0;
while(i<5){
cout<<"hello";
i++;
}
Run Code Online (Sandbox Code Playgroud) 我是编程新手,我试图在 LeetCode 上解决这个问题,其中:
给定一个非负整数 num,重复将其所有数字相加,直到结果只有一位。输入:38 输出:2 解释:过程就像:3 + 8 = 11, 1 + 1 = 2。因为 2 只有一位数,所以返回它。
我的解决方案:
class Solution {
public:
int addDigits(int num) {
int sum =0,a =0;
while(num!=0){
a = num%10;
sum = sum + a;
num = num/10;
}
if(sum>9){
addDigits(sum);
}else{
return sum;
}
}
};
Run Code Online (Sandbox Code Playgroud)
此代码与其他编译器配合良好,但当我尝试在 LeetCode 提供的 IDE 上运行时出错
错误:
Line 18: Char 5: error: control may reach end of non-void function [-Werror,-Wreturn-type]
}
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
请提出一种克服此错误的方法
while如果n等于 3,我想打破这个循环,但我该怎么做?
n = 0;
while(true){
switch (n){
case 3:
break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud) 我想做类似的事情:
def myfunc(p1, p2, p3):
time_step = 0
if p1 <= 0
while (p2 > 0 or p3 > 0)
stuff that updates p2, p3
timestep+=1
else
while (time_step < p1)
stuff that updates p2, p3
timestep+=1
Run Code Online (Sandbox Code Playgroud)
基本上,我希望能够让用户决定他们是否希望 while 循环运行直到 p2 和 p3 小于或等于 0,或者他们是否希望 while 循环运行到所需的 time_step。在任何一种情况下,“更新 p2、p3 的东西”都是完全相同的。但是,while 循环中的内容由很多行组成,我只会复制和粘贴“更新 p2、p3 的内容”。我觉得一定有更好的方法。
我希望以下方法可行:
def myfunc(p1, p2, p3):
time_step = 0
if p1 <= 0
conditional_statement = (p2 > 0 or p3 > 0)
else
conditional_statement = (time_step < p1) …Run Code Online (Sandbox Code Playgroud) 我知道 while 循环的主体部分有两个花括号{ }。而且我知道 while 循环是如何工作的。但是当我在看一本关于 C++ 的书时,我发现了这段代码:
#include <iostream>
int main()
{
int sum = 0, value = 0;
// read until end-of-file, calculating a running total of all values read
while (std::cin >> value)
sum += value; // equivalent to sum = sum + value
std::cout << "Sum is: " << sum << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是没有大括号{ },这些代码的工作方式与:
#include <iostream>
int main()
{
int sum = 0, value = 0;
// read until …Run Code Online (Sandbox Code Playgroud)