r/CodingHelp • • Jan 26 '20

Calculator loop help

Post image
15 Upvotes

12 comments sorted by

View all comments

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.