Win a copy of liveProject: Protecting User Data with Spring Security and OAuth2 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Is this a loop? or what? Help me please.

 
Greenhorn
Posts: 9
1
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help again. I am creating a program, that asks a question, "how much love do you need?". The user adds a number, to how much they need. I need to make the code, say, if the user puts in the number 3, " I love you one time", I love you two times", "I love you three times", and this depends on the number they enter. Can I get some assistance on how to do this, please? I need the number entered to be printed out each time starting at one going all the way up to 9.
Thanks for the help in advance.

 
Saloon Keeper
Posts: 8061
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll want to use an int for 'n', not a double.

Try Googling "java for loop tutorial" or "java for loop examples".
 
Master Rancher
Posts: 4281
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some holes in the if statement tests
if the statement on line 14 is true then the statement on line 16 can never be true.
What is supposed to happen if n is 0, 9 or 10?
 
Jay Rem
Greenhorn
Posts: 9
1
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:There are some holes in the if statement tests
if the statement on line 14 is true then the statement on line 16 can never be true.
What is supposed to happen if n is 0, 9 or 10?




I totally understand where you're coming from. After looking at it, but like this it runs. Im gonna look into looping, I guess that's where I need to be at anyway. When I enter 2, or anything between 0 and 9 it runs how it is intended. Try it yourself.
 
Marshal
Posts: 72595
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Rem wrote:. . . . . .

A bit of thought will show you, that line doesn't need any more work. It needs parts deleting
 
Carey Brown
Saloon Keeper
Posts: 8061
70
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

Campbell Ritchie wrote:

Jay Rem wrote:. . . . . .

A bit of thought will show you, that line doesn't need any more work. It needs parts deleting

The body of the "else" needs a loop.
 
Campbell Ritchie
Marshal
Posts: 72595
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The body might need a loop, but the line I showed (31) could do with some deletions.
 
Norm Radder
Master Rancher
Posts: 4281
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can line 25 ever be executed?
It will when n = 0;
 
Carey Brown
Saloon Keeper
Posts: 8061
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Campbell Ritchie
Marshal
Posts: 72595
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
....partially because you are mixing floating‑point and integer arithmetic. That isn't actually wrong, but something you need to be wary about. I shall challenge you to work out what love numbers (now, that's a weird phrase!) will cause line 25 to be executed.
 
Ranch Hand
Posts: 33
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jay.

A loop would be the simplest way to achieve the intended result. However, it is possible to achieve that with a switch statement and basic arithmetic if a loop isn't desired for some reason.

First, as Carey has already mentioned, it is best to use integers to store user input in this specific case (unless there's some spec we don't know about). From what you've written, the output only requires integers. Floating point numbers are strange. The number one might be stored as 1.000000000000001 or 0.9999999999999. As is clearly seen, using doubles might introduce errors and if used correctly, with the Double class for comparisons, would increase complexity unnecessarily. For example, instead of using the comparison

it's preferable to use something like


Anyway, without further ado, here's a switch statement implementation that takes advantage of the fall-through behavior of switches. It also requires n to be an integer to function and uses the ternary operator to append an "s" to the end of "time" when necessary.



If you really wanted, you could use a series of if statements with the Double class for comparisons to get the same result using n as type double.

Obviously, this is bad form given the same statement is repeated nine times so a loop is preferable here from a cleanliness perspective, but I thought I would share. Hope it helps a little, food for thought, stuff like that.
 
Campbell Ritchie
Marshal
Posts: 72595
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Gillette wrote:. . . as Carey has already mentioned, it is best to use integers . . .

Agree.

Floating point numbers are strange. The number one might be stored as 1.000000000000001 or 0.9999999999999. . . .

They aren't that strange; it you enter 1.0, it can be represented exactly as a double and can be stored as something equal to 1 (integer).


it's preferable to use something like

That is only necessary of there is any possibility of either operand being NaN. Can Scanner#nextDouble() return NaN? I have never tried it. I think the correct answeer is to go back to integer arithmetic throughout.

Anyway, without further ado, here's a switch statement implementation that takes advantage of the fall-through behavior of switches.

Unfortunately it suffers badly from repeated code.

. . . uses the ternary operator to append an "s" to the end of "time" when necessary.

Why? The presence of the s depends on the number of times entered.

If that is what OP wants to print, then either a loop or a Stream will do nicely. I am afraid other solutions won't work at all well.
 
David Gillette
Ranch Hand
Posts: 33
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


They aren't that strange; it you enter 1.0, it can be represented exactly as a double and can be stored as something equal to 1 (integer).


This is my lazy way of thinking about floats. Normally, there are calculations being performed so in most situations an exact comparison isn't precise.


Why? The presence of the s depends on the number of times entered.


The way the switch statement works is "1 time" will be printed to console for whatever value was received by the scanner (the expression n - count determines which case displays 1). For all others, "times" should print. For example, if the user enters 4, the case 4 block is executed, "1 time" is displayed and for the remaining cases "times" is displayed. So whether "time" or "times" is displayed is dependent upon the scanner value not the case block executed.


If that is what OP wants to print, then either a loop or a Stream will do nicely.



I agree a loop is best. But the crazy-dirty coded switch will work. I was just sayin' for the sake of just sayin'. I just want to participate. :'(
 
Carey Brown
Saloon Keeper
Posts: 8061
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Gillette wrote:I just want to participate. :'(

Ah, yes. You've got to have thick skin around here. There's ten ways to do things and more than likely someone's thought of a way that's better than yours. That's the way it goes for all of us. Don't get discouraged.
 
lowercase baba
Posts: 12981
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Gillette wrote:


Well, the OP said you are supposed to print the words "one", "two", "three", etc. not "1", "2", "3".  

 
Saloon Keeper
Posts: 4377
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like David's switch, if only because it is certainly not the first thing that comes up! I was thinking of using java 11 String.repeat, but the way to get a different string for every repeat is a bit ugly.
 
Campbell Ritchie
Marshal
Posts: 72595
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which case, you ought to write, “once,” “twice,” “thrice,” “four times,” etc., which simply makes the program harder to write.
 
Jay Rem
Greenhorn
Posts: 9
1
Eclipse IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The forums here are very helpful. Thanks to everyone who posted and added to the discussion. I had finished the program and made a good grade for it. I'll post the final code here. everything worked as intended with a few adjustments, I changed double to int, and added way more if statements to the code. it ended up being a lot of extra lines, I feel like I could have saved a few lines somehow, but am not sure. Anyway, here it is. Thanks again everyone.
 
Jay Rem
Greenhorn
Posts: 9
1
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Can line 25 ever be executed?
It will when n = 0;


and yes line 25 can be, and was.
 
Campbell Ritchie
Marshal
Posts: 72595
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Gillette wrote:. . . This is my lazy way of thinking about floats. Normally, there are calculations being performed so in most situations an exact comparison isn't precise.

You are right to be wary about floating‑point arithmetic, but I never talk about floats if I can help it. I only use the float datatype if I can't avoid it. Anyway, we seem to have decided the program would work better with int arithmetic. You can use ints in switch‑case statements, too.

. . . I was just sayin' for the sake of just sayin'. I just want to participate. :'(

Hahahahahahaha! Yes, the more participation the better
 
Campbell Ritchie
Marshal
Posts: 72595
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Rem wrote:. . . a good grade for it. . . .

Well done
Let's see what I would have done, but I shall omit the documentation comments because David Gillette isn't the only lazy person round here I went through Coxhoe last week, where Elizabeth Barrett Browning came from, who wrote most of the quote (Sonnet 43). The strange version of the text is a text block; see the JLS (=Java┬« Language Specification). David Gillette reminded me about the use of 5: which is a low‑precedence operator which requires an extra pair of (). Come to think of it, String#format would have been easier to use. Or a cached String because that class is immutable.

[edit] 5: Really? Surely that shoiuld read ?:
 
Piet Souris
Saloon Keeper
Posts: 4377
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But hey: where's the loop?
 
Always! Wait. Never. Shut up. Look at this tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic