• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

find the min of 3 numbers

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i want your help!

i want to find the min of 3 numbers named a,b,c without to give them initial value from input.
And i want NOT to use the math method. Can be solved with using for-while-do while?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teo kokos wrote:i want to find the min of 3 numbers named a,b,c without to give them initial value from input.
And i want NOT to use the math method. Can be solved with using for-while-do while?


Probably, but there's no need.

The minimum of two numbers can be produced with
a < b ? a : b
so how do you think that might help to produce the minimum of 3?

Winston
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teo kokos wrote:i want to find the min of 3 numbers named a,b,c without to give them initial value from input.


Didn't get this part very well.

You can use any loop you want if you put them in an array. Suppose the first number is the smallest one, iterate through all others and if you find smaller then the current min, then set the current min to that value. That's the simplest way to achive your task, and you'll have a method that works for any number of values you want to find min from.

And please, be more specific in future when you post your questions.
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

The minimum of two numbers can be produced with
a < b ? a : b
so how do you think that might help to produce the minimum of 3?

Winston



Is this correct?
smallest=(a>b)?((a>c)?a:c) : ((b>c)?b:c);

Kemal Sokolovic wrote:

teo kokos wrote:i want to find the min of 3 numbers named a,b,c without to give them initial value from input.


Didn't get this part very well.



I mean without using the scanner method.

this way is correct?


I want this also with for loop or while loop but how?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teo kokos wrote:Is this correct?
smallest=(a>b)?((a>c)?a:c) : ((b>c)?b:c);


Not quite.
1. You got the condition wrong.
2. You don't really need a lot of those brackets.
So:
smallest = a < b ? (a < c ? a : c) : (b < c ? b : c);
I'd also urge you to space out your code a bit. There are no prizes for "compactness" in Java, and it makes it much easier for others to read.

Winston
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thank you!

I want this also with for loop or while loop but how?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teo kokos wrote:I want this also with for loop or while loop but how?


Why? You've got a solution, and it's about as fast as it can be. The only possible reason is that you want to do this for n numbers, NOT 3.

My suggestion: Try something out for n numbers and come back with your code if you have any problems.

Winston
 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teo kokos wrote:I want this also with for loop or while loop but how?


A better question is WHY?

When you program, you don't try and force a solution onto a problem. starting with "I HAVE TO USE A XYZ IN MY CODE" is just plain WRONG.

You start with the problem, figure out how to solve it using real word (English, Chinese, Russian, whatever...). Once you've done that, you start thinking about what java has that lets you do what you wrote down.

Your approach is like saying "I have to tighten the nut on this bolt, and I HAVE TO USE A HAMMER". sometimes, the tool you are trying to use is the wrong one.
 
teo kokos
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want it because our teacher has requested this to the last exams. This program to solve it like before once and once with using for or while loops. I would appreciate your help!
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teo kokos wrote:I want it because our teacher has requested this to the last exams. This program to solve it like before once and once with using for or while loops. I would appreciate your help!


You've already got a fair bit of help. Like I said above: try something out and come back with your code if you run into problems. Seems to me you have all the basic tools.

Winston

 
fred rosenberger
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teo kokos wrote:I want it because our teacher has requested this to the last exams. This program to solve it like before once and once with using for or while loops.


What, EXACTLY, did your teacher request? What were the precise words?

Because what you are asking for makes no sense. You generally use a for-loop when you know the exact number of iterations - like when you have an array and you want to look at each element.

You use a while loop when you don't know the number of iterations - like when you are waiting for a user to input a sentinel value.

You have three named variable. So, you don't have an array (or other collection type) to iterate through, and you don't need to wait for a special condition to be met. I personally don't see a good way to use a loop of any kind here.

That makes me wonder if you aren't giving us the actual assignment, but a paraphrasing, which leaves out certain details.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic