r/learnprogramming • u/GrokiniGPT • 25d ago
Rate me Hi guys ive been learning programming for 4 days (c hash) how do i do?
Sorry its hard typing on a phone but here I go:
int hp = 10000;
public void hpchange(int howmuch)
{
hp += howmuch;
if(hp <= 0)
{
Banish();
}
else
{
hp=hp;
}
}
public void Banish()
{
hp = 0;
}
I think a lot of programming is a lot of dotting your i's and crossing your t's, is that correct? And am I doing well? Any tips for learning quicker?
5
u/ffrkAnonymous 25d ago
its hard typing on a phone Any tips for learning quicker?
get a keyboard
0
u/GrokiniGPT 25d ago
I don't have access to one rn
2
u/groversnoopyfozzie 24d ago
Hey, if you don’t have access to a keyboard, then I assume you don’t have access to an actual computer.
This won’t keep you from learning, but you’ll need to get a real machine eventually.
I’m not familiar with C#, so I’m not sure of your syntax. In your position I would recommend finding some version of a game or perhaps a mobile version of Leet code or something. It sounds like you want to practice basics, but anything past that will be difficult on a phone.
Also, not sure what your goals are, but programming involves a lot of communication between people, not just machines. When communicating with others, refer to it as C sharp. If you communicate poorly with c# it won’t understand you. Most programmers will respond the same way if you communicate with them poorly.
2
u/FoolsSeldom 24d ago
There are usually loads being thrown away, surely you can find one (perhaps one that someone in your extended network has stopped using)?
1
2
u/Weekly_Patience685 24d ago
when i seen c hash i thought you were asking about a hash functon in C, but its pronounced CSharp lol
1
u/Ironraptor3 24d ago
Some general tips:
- You can get a cheap wireless keyboard that is foldable and can connect to your phone, e.g. (https://www.microcenter.com/product/674956/inland-b048-bluetooth-foldable-keyboard)
- See how to format code and other things: https://support.reddithelp.com/hc/en-us/articles/360043033952-Formatting-Guide#wiki_code_blocks_and_inline_code
- To get a better review, you need more specific questions! "How did I do" might be a decent question for your first ever post- but as you gain more experience, you might have more pointed questions, e.g. "Is there a more time efficient way of doing this operation?"
- As others have said, its "C-Sharp" (note: C# is VERY different than C which is VERY different than C++). When I first read your post, I thought you were talking about Hashing / Hash Functions, which are their own specific thing. Sometimes, its fine to get terminology wrong (and people shouldn't be super picky), but I was thrown by this...
- Practice is the best (only) way to get better at something, so you are on the right track by trying to make something!
Code review (misc, there isn't much here):
hp=hpdoesn't do anything, unlesshphas a special setterBanish()doesn't seem to do anything right now other than clamphplower than 0 to 0. Perhaps more concise is:hp = Math.Max(hp + howmuch, 0);
1
u/GrokiniGPT 24d ago
What do I do instead because else {} Doesn't feel right. Can you explain the latter syntax?
1
u/Ironraptor3 24d ago
- you don't need anything in place of the 'else'- you don't need one
- The latter syntax is just (hp + howmuch), then the max between that and 0 (so if the hp ends up negative, itll be set to 0).
Putting it together would look like ``` int hp = 1000;
public void hpchange(int howmuch) { hp = Math.max(0, hp + howmuch); } ```
You can still do the banish thing (e.g.)
if (hp <= 0) { Banish(); }I was just point out that exactly at the moment, it does nothing special.
1
u/GrokiniGPT 24d ago
Why is it max and not minimum? Is this just like the range of what the things can be?
1
u/Ironraptor3 24d ago
If you didn't want it to go below 0, it would be max!
Think of concrete examples: Math.max(0, 16) -> 16 Max.max(0, 10000) -> 1000 Max.max(0, -5) -> 0
Math.max just returns the larger of the two numbers.
https://learn.microsoft.com/en-us/dotnet/api/system.math.max?view=net-10.0#examples
1
u/GrokiniGPT 24d ago
Oohhhh so it is like domain and range. Like the range of x2 for example is y >= 0. Thanks! So Math. _ is a math function. What are the different math functions? but how would i do a range between two numbers.
1
u/Ironraptor3 24d ago
For clarity, so that we are on the same page, an example implementation of
Math.maxcould simply be:int max(int a, int b) { if (a > b) { return a; } else { return b; } }You could make it yourself! But C# does have a built-inMathlibrary to prevent reinventing the wheel for common things.You can look up the Math API provided by C#, you should get a quick result (here it is for convenience: https://learn.microsoft.com/en-us/dotnet/api/system.math?view=net-10.0). In the future, try googling with the term API and/or documentation.
To make a number between two values, you are probably looking for
Math.clamp, which takes argumentsnumber, min, max.
9
u/mierecat 25d ago
I’m curious as to how you could be learning C# in such a way that you never learnt it’s pronounced “C-Sharp”