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; }
Related
How to get a number converted into something like this: 12K, 1.5M, 4.2B from a normal number like: 134900.
This is a minimalist function, of course you'll have to add validation code to verify if the number is valid before executing the function. Otherwise Enjoy ...
void main() {
double num = 1250;
var myNumber = k_m_b_generator(num);
print(myNumber);
}
String k_m_b_generator(num) {
if (num > 999 && num < 99999) {
return "${(num / 1000).toStringAsFixed(1)} K";
} else if (num > 99999 && num < 999999) {
return "${(num / 1000).toStringAsFixed(0)} K";
} else if (num > 999999 && num < 999999999) {
return "${(num / 1000000).toStringAsFixed(1)} M";
} else if (num > 999999999) {
return "${(num / 1000000000).toStringAsFixed(1)} B";
} else {
return num.toString();
}
}
You can use flutter's NumberFormat class with the compact function.
formatNumber(dynamic myNumber) {
// Convert number into a string if it was not a string previously
String stringNumber = myNumber.toString();
// Convert number into double to be formatted.
// Default to zero if unable to do so
double doubleNumber = double.tryParse(stringNumber) ?? 0;
// Set number format to use
NumberFormat numberFormat = new NumberFormat.compact();
return numberFormat.format(doubleNumber);
}
The answer is not entirely correct. If you test it, you will see what i meant. Base on the answer above, I created this solution:
String numberFormat(int n) {
String num = n.toString();
int len = num.length;
if (n >= 1000 && n < 1000000) {
return num.substring(0, len - 3) + '.' + num.substring(len - 3, 1 + (len - 3)) + 'k';
} else if (n >= 1000000 && n < 1000000000) {
return num.substring(0, len - 6) + '.' + num.substring(len - 6, 1 + (len - 6)) + 'm';
} else if (n > 1000000000) {
return num.substring(0, len - 9) + '.' + num.substring(len - 9, 1 + (len - 9)) + 'b';
} else {
return num.toString();
}
}
Sorry if this question has already been asked, but I have not managed to find any advice on the internet for my issue. I am currently trying to program a little game on the Nintendo DS, in which the player has to move a sprite (currently a square) until it reaches the exit. For this, I use a sprite I have included using a grit file, and also a background enabled in tiled mode. However, I am having a problem when it comes to checking if the sprite is going to collide with a wall. Here is the code I have both for the background configuration (where I declare the tiles and the map) and also for the sprite movements (I didn't add the condition for all cases yet, as it didn't work well) :
void configureMaze_Sub() {
int row, col;
for (row = 0; row < 24; row ++) {
for (col = 0; col < 32; col ++) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 1;
if (col == 15 && (row != 12 && row !=4 && row != 19)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if ((row == 1 || row == 22) && (col > 2 && col < 29)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if ((col == 3 || col == 28) && (row > 1 && row < 22 && row != 12)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if ((row == 3 || row == 20) && (col > 4 && col < 27)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if ((col == 5 || col == 26) && (row != 9 && row != 15 && row > 3 && row < 20)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if (row == 8 && (col > 5 && col < 15)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if (row == 16 && (col > 15 && col < 26)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
if ((row == 12) && (col > 5 && col < 26)) {
BG_MAP_RAM_SUB(3)[row * 32 + col] = 0;
}
}
}
}
void gameplayMaze() {
int x = 103, y = 41, keys;
int maze_success = 0;
while (maze_success == 0) {
scanKeys();
keys = keysHeld();
int xmod = x / 8;
int ymod = x / 8;
if ((keys & KEY_RIGHT) && BG_MAP_RAM_SUB(3)[xmod + 32 * ymod] == 1) {
x++;
printf("%d \n", x);
}
if ((keys & KEY_LEFT) && BG_MAP_RAM_SUB(3)[xmod + 32 * ymod] == 1) {
x--;
}
if (keys & KEY_UP) {
y--;
}
if (keys & KEY_DOWN) {
y++;
}
oamSet(&oamSub,
0,
x, y,
0,
0,
SpriteSize_8x8,
SpriteColorFormat_256Color,
gfxSub,
-1,
false,
false,
false, false,
false
);
swiWaitForVBlank();
oamUpdate(&oamSub);
}
The main problem I have is to try to change from the coordinates of the tiles (which are 8x8) to the ones of the map, as for the coordinates of the sprite (256x192). If any of you have any hint to help me, I would be very grateful! I am still new to programming on the NDS, so I am still struggling to get the hang of it.
If an if statement gets called and all the conditions are true, do all the else if statements get called also?
like:
if coins > 19 && speedLvl == 1 {
speedLvl = 2
coins = coins - 20
}
else if coins > 49 && speedLvl == 2 {
speedLvl = 3
coins = coins - 50
}
else if coins > 99 && speedLvl == 3 {
speedLvl = 4
coins = coins - 100
}
If the player has 1000 coins do then speedLvl the go to 4 ?
No, and you can visualize it like this:
if coins > 19 && speedLvl == 1 {
speedLvl = 2
coins = coins - 20
}
else {
if coins > 49 && speedLvl == 2 {
speedLvl = 3
coins = coins - 50
}
else {
if coins > 99 && speedLvl == 3 {
speedLvl = 4
coins = coins - 100
}
}
}
Although this code would be more easily written in Swift 4 as:
switch (speedLvl, coins) {
case (1, 20..<50):
speedLvl += 1
coins -= 20
case (2, 50..<100):
speedLvl += 1
coins -= 50
case (3, 100...):
speedLvl += 1
coins -= 100
default: break;
}
or better yet, perhaps:
let levelUpCosts = [0, 20, 50, 100]
let levelUpCost = levelUpCosts[speedLvl]
if levelUpCost < coins {
coins -= levelUpCost
speedLvl += 1
}
If you want to multiple level ups to be possible, all in one shot, then you can do something like this:
let levelUpCosts = [0, 20, 50, 100]
var affordedLevelUpsCost = 0
let affordedLevelUps = levelUpCosts.lazy.prefix(while: { cost in
let newCost = affordedLevelUpsCost + cost
let canAffordLevelUp = newCost < coins
if canAffordLevelUp { affordedLevelUpsCost = newCost }
return canAffordLevelUp
})
speedLvl += affordedLevelUps.count
coins -= affordedLevelUpsCost
No, the if-else conditions will move on after a successful condition is reached.
So, if coins > 19 && speedLvl == 1 if this condition is true, the rest of the else-if conditions won't even be checked.
If the first if-statement is not true, then it will go to each if-else statement until it reaches a true condition. Once it reaches a true if-else condition, it will not check any remaining if-else conditions.
If you would like each of these conditions to be checked, remove the else and have each as a stand alone if-statement to check each condition individually without any dependency on the other conditions.
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 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");
}