I have a set of checks to perform certain tasks.
// tempDouble is a (double), hour is an int
if (tempDouble > 60.0 && (hour >= 6 || hour <= 17)) { //CLEAR
NSLog(#"CLEAR");
}
else if (tempDouble > 60.0 && (hour < 6 || hour > 17)) { //NIGHT_CLEAR
NSLog(#"NIGHT_CLEAR");
}
else if (tempDouble <= 60.0 && (hour >= 6 || hour <= 17)) { //CLOUDY
NSLog(#"CLOUDY");
}
else if (tempDouble > 60.0 && (hour < 6 || hour > 17)) { //NIGHT_CLOUDY
NSLog(#"NIGHT_CLOUDY");
}
When I have a temp of 76.3 and an hour of 2, for example, I'd expect it to jump to NIGHT_CLEAR, but it actually goes to CLEAR. Did I set up my comparisons wrongly?
Thanks in advance for this simple question!
(hour >= 6 || hour <= 17)
is always true. All real numbers are either greater than or equal to 6 or less than or equal to 17 (some are both). I think you want:
(hour >= 6 && hour <= 17)
The same also applies to CLOUDY.
Some of your ||'s might be better off being &&'s.
Perhaps what you want is...
if (tempDouble > 60.0 && (hour >= 6 && hour <= 17)) { //CLEAR
NSLog(#"CLEAR");
}
else if (tempDouble > 60.0 && (hour < 6 && hour > 17)) { //NIGHT_CLEAR
NSLog(#"NIGHT_CLEAR");
}
else if (tempDouble <= 60.0 && (hour >= 6 || hour <= 17)) { //CLOUDY
NSLog(#"CLOUDY");
}
else if (tempDouble > 60.0 && (hour < 6 || hour > 17)) { //NIGHT_CLOUDY
NSLog(#"NIGHT_CLOUDY");
}
Related
With the delay function in Anylogic, is there a way to not take the delay time from the start of the simulation, but from the point at which the delay is "triggered"?
Picture of my SystemDyamics System
My code:
complexity <= 2 ?
(stressresponse + boredom) <= 2 && wellbeing >= 0.8 ?
delay(0.3, 10)
:0
:complexity > 2 && complexity <= 5 ?
(Stressreaktion + Langeweile) <= 2 && Wohlbefinden >= 0.8 ?
delay(0.6, 4)
:0
:complexity > 5 && complexity <= 8 ?
(stressresponse + boredom) <= 2 && wellbeing >= 0.8 ?
delay(0.8, 3)
:0
:complexity > 8 && complexity <= 10 ??
(stressresponse + boredom) <= 2 && wellbeing >= 0.8 ?
delay(1, 2)
:0
:0
I want the respective value (0.3 ; 0.6 ; 0.8 or 1) to take effect only after a certain time. The problem is that the delay time in the respective function does not restart every time the previous conditions become true. I need it so that the delayTime is waited every time the conditions become true.
I am designing a small game, and have decided on an initial way to distribute points based on the number of seconds elapsed between the time a question was asked and an enter button was pressed.
I understand that best practice is to create a function to call when lines are going to be repeated, but it seems to me that it may be a little excessive to call it when there are only two lines repeated four times. Using the code below, would it be "proper" or preferred for me to create a function for the last two lines in each section?
//Assigns points to score based on elapsed time and updates score
func updateScore() {
timer.invalidate()
if timeToAnswer < 3 {
questionScore = 10 //10 points for answering so quickly
score = score + questionScore //Updates score
timeToAnswer = 0 //Resets var timeToAnswer
}
else if (timeToAnswer >= 3) && (timeToAnswer < 6) {
questionScore = 7
score = score + questionScore
timeToAnswer = 0
}
else if (timeToAnswer >= 6) && (timeToAnswer < 11) {
questionScore = 10 - timeToAnswer
score = score + questionScore
timeToAnswer = 0
}
else {
questionScore = 1
score = score + questionScore
timeToAnswer = 0
}
}
Thank you very much to anybody that offers assistance or advice.
You can do it in a better way.
You can use switch statement, too. Anyway:
func updateScore() {
timer.invalidate()
if timeToAnswer < 3 {
questionScore = 10
}
else if (timeToAnswer >= 3) && (timeToAnswer < 6) {
questionScore = 7
}
else if (timeToAnswer >= 6) && (timeToAnswer < 11) {
questionScore = 10 - timeToAnswer
}
else {
questionScore = 1
}
score = score + questionScore //Updates score
timeToAnswer = 0 //Resets var timeToAnswer
}
I haven't been programming very long, so I'm practicing some logic exercises in dlang. Any ideas on what I've done wrong here. When I get to a leap year, my program just keeps looping on the WHILE.
import std.stdio;
void main()
{
bool dead;
string thing;
int phew = 5; //days
int tahr = 1; //months
int tron; //monthsDate
string[7] days = ["Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"];
int date = 28;
string[12] months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
int year = 1996;
int hours = 11;
int mins = 28;
string ampm = "pm";
bool ly;
int leap = 1996;
int cen = 996;
//writeln("This program is incomplete. Obviously.");
write("Press Enter to Continue.");
readf("%s\n",&thing);
while(!dead)
{
while(hours <= 12)
{
while(mins <= 59)
{
if(mins < 10)
write(date," ",months[tahr],", ",year,". ",days[phew],". ",hours,":0",mins,ampm,": ");
else
write(date," ",months[tahr],", ",year,". ",days[phew],". ",hours,":",mins,ampm,": ");
readf("%s\n",&thing);
mins++;
}
hours++;
if(hours == 12 && ampm == "am")
{
ampm = "pm";
}
else if (hours == 12 && ampm == "pm")
{
ampm = "am";
phew++;
date++;
if(phew > 6)
phew = 0;
if((date == 29 || date == 30) && tahr == 1)
{
while(leap <= year) //this assuming time travel doesn't work
{ //reminder: add time travel
if (leap == year)
{
ly = true;
break;
}
leap+=4;
ly = false;
}
if(!ly || date == 30)
{
date = 31;
leap-=4;
}
}
if(!ly || date == 30)
{
date = 31;
leap-=4;
}
}
if(date == 31 && (tahr == 1 || tahr == 3 || tahr == 5 || tahr == 8 || tahr == 10))
{
date = 1;
tahr++;
}
else if (tahr == 11 && date == 32)
{
tahr = 0;
date = 1;
year++;
cen++;
if(cen == 1000)
{
writeln("Happy Millennium!");
cen = 0;
}
else
writeln("Happy New Year!");
}
else if(date == 32 && (tahr == 0 || tahr == 2 || tahr == 4 || tahr == 6 || tahr == 7 || tahr == 9))
{
date = 1;
tahr++;
}
}
if(hours == 13)
{
hours = 1;
}
mins = 0;
}
}
}
The important part is this:
if((date == 29 || date == 30) && tahr == 1)
{
while(leap <= year) //this assuming time travel doesn't work
{ //reminder: add time travel
if (leap == year)///
{
ly = true;
break;
}
leap+=4;
ly = false;
}
if(!ly || date == 30)
{
date = 31;
leap=-4;
}
}
So, I figured out the issue almost immediately after posting. Basically, I wrote =+ instead of +=. Very simple mistake. That's what I get for typing too fast. So, I've fixed up the code now, if you have any other suggestions, make sure to put them in the comments. Thanks.
Decided to print out leap into the terminal, and I figured out my issue. Leap was continuously equal to 4, because I wrote =+ instead of +=. It was just a case of clicking the wrong button first. That's what I get for typing fast. The program works now, as far as I know. Feel free to correct me on anything else you may notice.
I haven't read it in depth, but I recommend you add lots of debug writelns.
Particularly just inside the block you're THINKING should get run:
if((date == 29 || date == 30) && tahr == 1) {
writefln("Handling leap years...");
while(leap <= year) //this assuming time travel doesn't work
{ //reminder: add time travel
writefln("In loop, Leap <= year? %s", leap <= year);
This will allow you to debug more what is actually happening when your program is run.
I'm working on a project that needs to check the time difference from a first launch date. I'm using the NSDayCalendarUnit and NSWeekCalendarUnit. Basically for the first 2 weeks on every second day I want to perform something. The object I'm using needs to be in a certain state for each two days at a time.
So for example
Day 1 & 2, Week 0 - State 1
Day 3 & 4, Week 0 - State 2
...
Day 1 & 2, Week 1 - State 8
Day 3 & 4, Week 1 - State 9
Here is my code:
// get the data/time difference from the first launch
int daysDifferent = [[dateDifferenceInfo objectForKey:#"days"] intValue];
int weeksDifferent = [[dateDifferenceInfo objectForKey:#"weeks"] intValue];
if(daysDifferent == 2 | daysDifferent == 3 && !weeksDifferent && _dot.age != 2){
// set state
}
if(daysDifferent == 4 | daysDifferent == 5 && !weeksDifferent && _dot.age != 3){
// set state
}
if((daysDifferent == 6 && weeksDifferent == 0 | daysDifferent == 0 && weeksDifferent == 1) && _dot.age != 4){
// set state
}
if(daysDifferent == 0 | daysDifferent == 1 && weeksDifferent == 1 && _dot.age != 5){
// set state
}
if(daysDifferent == 2 | daysDifferent == 3 && weeksDifferent == 1 && _dot.age != 6){
// set state
}
if(daysDifferent == 4| daysDifferent == 5 && weeksDifferent == 1 && _dot.age != 7){
// set state
}
if((daysDifferent == 6 && weeksDifferent == 1 | daysDifferent == 0 && weeksDifferent == 2) && _dot.age != 8){
// set state
}
if(weeksDifferent >= 2 && !(daysDifferent % 2)){
// set state
}
Side note: I know it's bad code, I plan to replace this with switch cases, I just need to logic sorted first.
My question is is there a better way of calculating this kind of pattern?
I'm not totally clear on your goal, as you say:
for the first 2 weeks on every second day I want to perform something.
So for example
Day 1 & 2, Week 0 - State 1
Day 3 & 4, Week 0 - State 2
...
Day 1 & 2, Week 1 - State 8
Day 3 & 4, Week 1 - State 9
Where I would have thought it would be
Day 1 & 2, Week 0 - State 1
Day 3 & 4, Week 0 - State 2
Day 5 & 6, Week 0 - State 3
Day 7 Week 0 & Day 1 Week 1 - State 4.
...
So if your example is right, the calculation is different.
Based on the simpler example, I would calculate the time interval since the first date, and divide by twice the length of a day, taking the floor. That is the state:
NSDate *firstDate = ...
NSDate *secondDate = ...
NSTimeInterval interval = [ secondDate timeIntervalSinceDate: firstDate ];
double secondsPerDay = 60 * 60 * 24.0; // s per min times min per hr times hrs per dy.
int state = (int) (interval / secondsPerDay * 0.5 ) + 1;
If, on the other hand, it really is as you have described, the approach I would use as fast and simple would be to do a similar calculation - leaving out the 0.5 factor in the last line, and then use a simple table lookup to get the state from the day:
int table[ 14 ] = { 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 7, 8 };
int state = table[ (int) (interval / secondsPerDay ) ];
I have an age 24, and I want to return TRUE if 24 is within the range of 20 to 38, else FALSE.
Is this even possible?
return (age >= 20 && age <= 38);
Very basic question. You know about if/else?
if (age >= 20 && age <= 38) {
return true;
} else {
return false;
}
Or
return (age >= 20 && age <= 38) ? true : false;
Or
return (age >= 20 && age <= 38);
if (age > 19 && age < 39) { return true; } else { return false; }