r/CodingHelp • • Jan 26 '20

Calculator loop help

Post image
15 Upvotes

12 comments sorted by

2

u/[deleted] Jan 26 '20

Hello all,

I made a calculator program. And I am trying to get it to repeat when you are done. IE you use it, it asks do you want to go again, enter yes, the original program runs again. Can someone explain the while loop to me, I feel this is what I am asking for but I've been scratching my head in how to make it work properly. I am fairly new at coding so please forgive me for anything I missed here.

1

u/[deleted] Jan 27 '20

I gotcha, thank you so much for the help. I've actually never written python. Indents just seemed like a nice way to keep things straight

1

u/jonassjoh Jan 27 '20

Indents are indeed a great way to keep things straight. It also improves readability alot and you should continue to do it. Some languages even require it (like Python).

1

u/jonassjoh Jan 26 '20

I get the feeling that you've written some Python before.

Anyways, in Java, you need to wrap if/else/while blocks with curly bracets ({}), otherwise only the following line will exist inside the block.

For example:

while (true)
  System.out.println("Foo");
  System.out.println("Bar");

Will only print "Foo" over and over while:

while (true) {
  System.out.println("Foo");
  System.out.println("Bar");
}

will print "Foo" and "Bar" over and over.

1

u/[deleted] Jan 26 '20

I am still a little confused, adding brackets allows me to bring up my first prompt line again, "enter values and operation", but it no longer lets me reenter the values I'd like to run for this time? Just curious what makes you think I've done python?

1

u/jonassjoh Jan 27 '20

You compare int name = readInput.nextInt() to a char '1', which will not be true if name is 1. The char value of '1' is 49 (I've not written Java in a while so I could be wrong about it being a char comparison, but I'm pretty sure). You could try to enter 49 instead and see what happends.

Regarding Python, the equivalent expression for printing "Foo" and "Bar" over and over is:

while True:
  print("Foo")
  print("Bar")

Note the missing brackets. Python uses indentation rather than brackets, like you did.

Edit: Here is an ascii table where you can see that 49 is the ascii value for the character 1.

1

u/FallToEarth Jan 27 '20

Just a side note please use the enter to key separate logical blocks of code. It doesn’t impact the code itself but it makes it much more readable.

2

u/[deleted] Jan 27 '20

Thank you for the tip, I'm sure I'll be back here to ask more questions

2

u/SupremeRedditBot Professional Bot || Mod Jan 26 '20

Please Add A Flair To Your Post!

Suggested Flair: [Random] or [Meta]

 


To add a flair:

  • Click flair underneath your post

  • Select a flair

  • Click save

 


I am a bot run by /u/SupremeDesigner for /r/CodingHelp || This was an automated ^response

-5

u/jackandjill22 Jan 27 '20

Wow

1

u/[deleted] Jan 27 '20

What's a wow?

2

u/brandondyer64 Professional Coder Jan 27 '20 edited Jan 27 '20

I guess they're surprised a beginner isn't writing the best code ever written on day one.