Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Figuring Out the Rest of the Logic

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The expected output is in the screenshots


(1) Given an integer n, write a for loop that outputs the numbers from -n to +n. Assume n is nonnegative. End the sequence with a newline.

Enter an integer:
2
Sequence: -2 -1 0 1 2
(2) If n is negative, treat as the absolute value. So n of -2 is the same as n of 2. Hint: Use an if statement before the for loop, to compute the absolute value of n.

Enter an integer:
-2
Sequence: -2 -1 0 1 2


This is my code:

Screen-Shot-2020-12-09-at-11.09.54-PM.png
[Thumbnail for Screen-Shot-2020-12-09-at-11.09.54-PM.png]
Screen-Shot-2020-12-09-at-11.10.03-PM.png
[Thumbnail for Screen-Shot-2020-12-09-at-11.10.03-PM.png]
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-n is your start number.
n is your end number.
You need another variable in your loop as the value that changes between -n and n.

You have also missed the bit where an input of a negative number is treated as a positive number.
 
Marshal
Posts: 76061
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

E Baller wrote:. . . If n is negative, treat as the absolute value. . . . Hint: Use an if statement . . .

Afraid that hint isn't as good as you would think. I suggest you find out about the ?: operator which allows you to calculate the absolute value of an int in one line. ?: acts as an if‑else compressed into one line. There is an example in the old Sun style guide.
Challenge 1: Find out which value of n that technique will fail for. (An explicit if‑else would also fail for the same reason.)
Challenge 2: Work out what you should do if the input is 0.
Don't use print() (line 14). Use printf() instead.
 
E Baller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my updated code I worked on but still gets an error. Can anyone help me fix?

 
Campbell Ritchie
Marshal
Posts: 76061
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the error? Why have you got the loop to prevent negative numbers? Why have you get the variable i which you haven't used? Why is the code so badly formatted?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not following the requirements.
They say that you should treat a negative value for n as a positive one.
What you're doing is askig the user for a number until they supply a positive one.
 
E Baller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my updated code, how do I get it to the output I desire?

Screen-Shot-2020-12-10-at-7.52.08-AM.png
[Thumbnail for Screen-Shot-2020-12-10-at-7.52.08-AM.png]
Screen-Shot-2020-12-10-at-7.52.14-AM.png
[Thumbnail for Screen-Shot-2020-12-10-at-7.52.14-AM.png]
 
lowercase baba
Posts: 13062
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest you stop writing code for a bit.  Programming is 90% thinking, and 10% typing.  

One of the most important things for you to learn is to code in small chunks.  Try to write as little code as possible (and still move towards your goal), then compile, test fix, repeat.

If i were doing this, i'd start with doing nothing more than printing "i'm in main".

Once that worked, i'd ask for input, and then print out that input.  

Once that worked, i might try and print the upper and lower bounds of my loop.  so if the user entered "5", i'd try and print something like "print from -5 to 5".  test it with a negative number, like -3 and make sure you can get "print from -3 to 3". test with 0 as well.

Once that worked, THEN try printing all the numbers from your lower to your upper limit...
 
E Baller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still need help writing the if statement on how to make n an absolute value.

 
fred rosenberger
lowercase baba
Posts: 13062
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would argue you don't need an if statement at all.  you need to determine the upper limit, and the lower limit.

I'd start with finding the upper limit.  There is a simple way to set this value, regardless of the sign of what is input.  I would also consider saving what is input separately from the limits.  

Once you have the upper limit, finding the lower limit is trivial.

I would further suggest better variable names than "n".  I would use something like "input", "upperLimit", and "lowerLimit". thinking about it further, you don't even need to store the lower limit.
 
Saloon Keeper
Posts: 9418
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the difference between
and

How would each of these behave if n is positive or if n is negative?
 
E Baller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my updated code but my output is now messed up for the first for statement.


Screen-Shot-2020-12-10-at-11.20.09-AM.png
[Thumbnail for Screen-Shot-2020-12-10-at-11.20.09-AM.png]
Screen-Shot-2020-12-10-at-11.20.30-AM.png
[Thumbnail for Screen-Shot-2020-12-10-at-11.20.30-AM.png]
 
Carey Brown
Saloon Keeper
Posts: 9418
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See Fred's last post.

BTW what site are these exercises coming from?
 
fred rosenberger
lowercase baba
Posts: 13062
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
focus on this:

     
If you input "5", what will it do?

if you input "-5", what will it do?
 
Campbell Ritchie
Marshal
Posts: 76061
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

E Baller wrote:. . .

Please indent your code correctly; the contents of the for and if should be 3 spaces to the right and the } should align vertically with if (lines 19 and 21). Remember who will have the most difficulty if your code becomes illegible. You.
Lines 3: Main is a poor name for a class. It doesn't let users know what the class represents.
Line 4: Don't declare n until you use it: (8) int n = ...;
Line 5: Don't declare i until you use it, which is in the loop headers.
Lines 13 and 22: Why have you got two loops? Why are you using < in one loop and <= in the other? Can you tell from the output whether that is causing you any problems?
Why didn't you use printf()? Depending on the results of the if in line 19, how many of those loops will be executed?
Lines 19‑21: Why did you put the call to Math.abs there? Why did you wrap it in the unnecessary if? I thought the suggestion about if was a hint, not a requirement.
 
E Baller
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is the problem that I am using for zyBooks the course I am paying for school. This is the prompt they gave me:


(1) Given an integer n, write a for loop that outputs the numbers from -n to +n. Assume n is nonnegative. End the sequence with a newline.

Enter an integer:
2
Sequence: -2 -1 0 1 2
(2) If n is negative, treat as the absolute value. So n of -2 is the same as n of 2. Hint: Use an if statement before the for loop, to compute the absolute value of n.

Enter an integer:
-2
Sequence: -2 -1 0 1 2



This is the code I have written:


The pictures that I attach are the expected outputs for my code.
Screen-Shot-2020-12-10-at-2.18.50-PM.png
[Thumbnail for Screen-Shot-2020-12-10-at-2.18.50-PM.png]
Screen-Shot-2020-12-10-at-2.18.55-PM.png
[Thumbnail for Screen-Shot-2020-12-10-at-2.18.55-PM.png]
 
Campbell Ritchie
Marshal
Posts: 76061
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and . . .

You will have to go through your code and wok out what is going wrong. Did you read what we have told you already?
 
reply
    Bookmark Topic Watch Topic
  • New Topic