我正在开展一个项目,我必须阅读一个日期,以确保它是一个有效的日期.例如,2月29日只是闰年的有效日期,或者6月31日不是有效日期,因此计算机将根据输入输出该信息.我的问题是,我无法弄清楚如何解析字符串,以便用户可以输入"05/11/1996"作为日期(例如),然后取出并将其放入单独的整数.我正在考虑尝试用while循环和字符串流做一些事情,但我有点卡住了.如果有人可以帮助我,我会非常感激.
我想知道在 python 中是否有一种方法,当我的 games.screen.mainloop() 内的图形部分正在运行时,我是否可以执行诸如从控制台通过 raw_input() 获取用户输入之类的操作。
我正在研究一个程序,我正在使用二次公式.这是单击按钮时执行的代码,用于解决问题,
private void button1_Click(object sender, EventArgs e)
{
double aVal, bVal, cVal, xVal1, xVal2;
xVal1 = 0;
xVal2 = 0;
aVal = Convert.ToDouble(a.Text);
bVal = Convert.ToDouble(b.Text);
cVal = Convert.ToDouble(c.Text);
xVal1 = (bVal + Math.Sqrt(Math.Pow(bVal, 2) - (4 * aVal * cVal))) / 2 * aVal; //Positive Calculation
xVal2 = (bVal - Math.Sqrt(Math.Pow(bVal, 2) - (4 * aVal * cVal))) / 2 * aVal; //Negative Calculation
xPos.Text = Convert.ToString(xVal1);
xNeg.Text = Convert.ToString(xVal2);
}
Run Code Online (Sandbox Code Playgroud)
经过一些调试后,我相信我已将问题缩小到第9行和第10行,实际数学发生在第9行和第10行.但是,我不太确定是什么问题.如您所见,数字是双精度数,并且已初始化,因此它们不为空或截断.当我运行程序并输入say,6,4和3的ab和c值时,负责输出负值和正值的xPos和xNeg标签只显示NaN.我应该使用标签来做这类事吗?
有没有办法使用for循环作为计时器?我试过这个:
a = 0
for i in range(1, 10000):
a += 1
print "Hello World"
Run Code Online (Sandbox Code Playgroud)
但出于某种原因,它立即切入"Hello World".我认为Python会在每个滴答或1/1000秒增加.如果是这样,那么10000/1000 = 10,那么for循环应该持续10秒吧?如果有人能帮助我理解这一点,我会非常感激.
我正在开发一个程序,需要在程序启动时加载文件.我有单独的代码来加载文件,但我一直在使用一些基本方法来尝试找出一种在Windows Presentation Form Application启动时运行代码的方法.目前,我只是尝试在启动此应用程序时运行MsgBox函数.但我无法弄清楚如何做到这一点.
我是一名自学成才的程序员,我刚开始使用python.当我执行这段代码时,我遇到了一些问题:
x = 0
while x == 0:
question = raw_input("Would you like a hint? ")
if question == "y" or "yes":
print "Ok"
first.give_hint("Look over there")
x = 1
elif question == "n" or "no":
print "Ok"
x = 1
else:
print "I'm Sorry, I don't understand that"
Run Code Online (Sandbox Code Playgroud)
只是你知道,这first.give_hint("Look over there")是在程序早期的一个类中定义的,我只是为了空间而离开了那一部分.当我运行程序时,无论我输入什么,我都会得到第一个案例"Look over There",我一直试图弄清楚问题是什么,但我只是不明白.如果你们能帮助我,我会非常感激.
我曾经在c ++中做过一些编程,但我暂时还没有,最近才重新开始.我决定写一个基本程序,看看我在哪里,就语言知识而言.我写的程序询问用户的年龄,接受输入,然后有3个选项可供选择,具体取决于用户的输入.这是我的代码
#include <iostream>
using namespace std;
int main()
{
int age;
cout << "Please enter your age\n";
cin >> age;
bool error = false;
while (error = false)
{
if (age > 105)
{
cout << "You're Too Old\n";
continue;
}
else if (age < 1)
{
cout << "You haven't been born yet!\n";
continue;
}
else
{
error = true;
continue;
}
}
cout << "Your age is: " << age << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我输入的年龄超出1-105的范围时,我的问题就来了.出于某种原因,它会跳过整个if语句,只运行底部的代码.如果有人可以帮助我,我会很感激.
我正在开发一个允许用户跟踪省钱目标的应用程序.
public static int CalcProg(int userGoal, int userBalance, int userProg)
{
userProg = userBalance / userGoal;
userProg = userProg * 100
return userProg;
}
private void Form1_Load(object sender, EventArgs e)
{
//Calls the FileVerification Method
FileVerification();
//Sets the label1 transparency to true
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
LoadData();
CalcProg(userGoal, userBalance, userProg);
progressBar1.Value = userProg;
progLabel = Convert.ToString(userProg);
label3.Text = progLabel;
}
Run Code Online (Sandbox Code Playgroud)
这只是代码的一小部分,但它是我遇到问题的地方.
我使用方法来读取和写入变量userBalance和userGoal中使用的数据的文件.这一切都运行正常,因为当我在下面的转换函数中使用其中一个变量而不是"userProg"时,它就像在文本文件中一样显示.
当我尝试进行转换时,问题出现了.我的公式是CalcProg.当我实际启动程序时,在变量userProg上设置其值的两个元素(进度条和标签),无论在文本文件中输入什么值,都只显示零.
我已经尝试使用双重作为CalcProg方法并将userProg设置为double,但这不起作用.我有点卡住了,如果有人可以帮助我,我会很感激.