I've been given a project in which I have to find the highest and lowest possible product using 3 2-digit numbers without repeating any numbers.

For example, the algorithm for the highest would be:
7n * 8n * 9n where the n's would be 4, 5, or 6.
The algorithm for the lowest would be:
1n * 2n * 3n where the n's would be 4, 5, or 6.

So far, I know I will have to give an option of finding the lowest or highest, so I would ask the user to put in 0 to minimize and 1 to maximize. Now, I have thought of using an if statement for reading their input, but I need some kind of loop that will rearrange the 4's, 5's, or 6's. I thought of attaching the values of 4, 5, and 6 to the 90, 80, 70, or 10, 20, 30, but am unsure of how I would code a proper loop or simple if statement to find the best answer by trying each combination. In theory, I could make some really over-extensive amount of code to do it right, but it wouldn't follow curriculum.

The code I have so far is

public class Powers
{ 
    public static void main(String[] args);
    
        int input;
        int numOneTensLow = 10;
        int numTwoTensLow = 20;
        int numThreeTensLow = 30;
        int numOneOnesLow = 4;
        int numTwoOnesLow = 5;
        int numThreeOnesLow = 6;
        int numOneTensHigh = 90;
        int numTwoTensHigh = 80;
        int numThreeTensHigh = 70;
        int numOneOnesHigh = 6;
        int numTwoOnesHigh = 5;
        int numThreeOnesHigh = 4;
        int numOneLow;
        int numTwoLow;
        int numThreeLow;
        int numOneHigh;
        int numTwoHigh;
        int numThreeHigh;
        int lowproduct = (numOneLow * numTwoLow * numThreeLow);
        int highprodcut = (numOneHigh * numTwoHigh * numThreeHigh);
        
        ConsoleIO console = new ConsoleIO();
        System.out.println("This program will calculate three two-digit" +
        " numbers that create the lowest and highest possible products " +
        "using the numbers 1 through 9 once in each.");
        System.out.println("To calculate the highest input 1, to calculate" +
        " the lowest, input 0: ");
        input = console.readInt();
        
        if(int input = 0);
        {
            do
            {
                numOneTensLow + NumOneOnesLow;
                numTwoTensLow + NumTwoOnesLow;
                numThreeTensLow + NumThreeOnesLow;
                System.out.println((numOneTensLow + numOneOnesLow) * (NumTwoTensLow + NumTwoOnesLow)
                * (NumThreeTensLow + NumThreeOnesLow));
                }
            }

Recommended Answers

All 26 Replies

Base condition - use any java editor or IDE (NetBeans,Eclipse,BlueJ....)
What is this ? public static void main(String[] args); Post formally proper code.

quuba

I could help you with a little bit of help from your side first.Pls detail your problem and also comment your code wherever appropriate.

would love to help you imbestatjava, but i have no clue what you are trying to do

multiply by numbers by products of 7,8,9 and 1,2,3?
that part is understandable, but what is this?

90, 80, 70, or 10, 20, 30

i don't get what these values have to do with anything

The values 90, 80, 70 and 10, 20, 30 have a lot to do with the program because im trying to find the highest possible combination of 9n, 8n, and 7n so it'd be (90 + n) * (80 + n) * (70 + n) where every n must be either 4, 5, or 6. So far the only error I can actually see is in the line of code

public static void main(String[] args);

I think you need to do something like this :

highProduct1 = (numOneTensHigh + numOneOnesHigh) * 
					(numTwoTensHigh + numTwoOnesHigh) * 
					 (numThreeTensHigh + numThreeOnesHigh);
	
	highProduct2 = (numOneTensHigh + numTwoOnesHigh) * 
				    (numTwoTensHigh + numOneOnesHigh) * 
					 (numThreeTensHigh + numThreeOnesHigh);
	
	highProduct3 = (numOneTensHigh + numThreeOnesHigh) * 
					(numTwoTensHigh + numTwoOnesHigh) * 
					 (numThreeTensHigh + numOneOnesHigh);

	highProduct4 = (numOneTensHigh + numOneOnesHigh) * 
    				(numTwoTensHigh + numThreeOnesHigh) * 
    				 (numThreeTensHigh + numTwoOnesHigh);
	
	// This way try all combinations, if there are left any others ;-)
	// then get the max of them.
	
	if (max < highProduct1)
		max = highProduct1;
	
	if (max < highProduct2)
		max = highProduct2;
	
	if (max < highProduct3)
		max = highProduct3;
	
	if (max < highProduct4)
		max = highProduct4;
	
	// In the end print out the highest product
	System.out.println("Highest product is : " + max);

You can do the same way for the Lowest product.

read again #2
quuba

Your theme posted here is interesting. Min/max, combinatoric algorithms.
Line public static void main(String[] args); change to public static void main(String[] args){ Add }} at end of program.
Download from http://www.netbeans.org/ NetBans IDE 6.5, select Java SE 37 MB.
and Java SE Development Kit (JDK) 6 Update 11 from http://java.sun.com/javase/downloads/index.jsp ,

or

JDK 6 Update 11 with NetBeans 6.5 from http://java.sun.com/javase/downloads/index.jsp .
It's free.
Install and work.
Advanced editor show to You all wrong points in code before compiling.

I'm supposed to use BlueJ and its supposed to be a three-way multiplication, so I was hoping there could be some kind of loop I could use for this. An example for the maximization would be:
(90 + 6) * (80 + 5) * (70 +4)
And minimization would look like:
(10 + 5) * (20 + 6) * (30 + 4)
And somehow in a loop I would have to try each combination, so if I were to do each individually it would look like a bunch of lines of this:
14 * 25 * 36
14 * 26 * 35
15 * 26 * 34
15 * 24 * 36
16 * 25 * 34
16 * 24 * 35

...and that's just minimization.

Update: Now I get an error of ".class expected" for:

if(int input = 0);

Update: Now I get an error of ".class expected" for:

if(int input = 0);

that's to be expected, remove the ;

Update: Now I get an error of ".class expected" for:

if(int input = 0);

that's to be expected, remove the ;

Wrong answer, the if statement is wrong!
You want to check the statement not assign value...

if(input == 0){
    // Then something
}

Wrong answer, the if statement is wrong!
You want to check the statement not assign value...

if(input == 0){
    // Then something
}

hmmmm ... must be even more tired than I thought to miss that :/
still, the ; shouldn't have been there

In my opinion this is a better way to do that, in looping you would have to customise the loop in a way that it runs only for set of values that you need for e.g. no repitition of the digits etc this way you would have to take care of a lot of conditions which would make the code complex. Hence I say that the method shown above would be a more cleaner convenient option.
Also since the repetition is not allowed you actually have lesser combination of digits to evaluate so that you can write out all the combinations by hand (which I guess won't be more than 4-6)

Okay I'm kind of stuck on this if-else statement. I chose to just give the correct ones directly, since looping would be too difficult to fabricate. Right now I get an error "else without if" for:

if(input == 0){
        {
            numOneLow = numOneTensLow + numOneOnesLow;
            numTwoLow = numTwoTensLow + numTwoOnesLow;
            numThreeLow = numThreeTensLow + numThreeOnesLow;
            System.out.println(lowproduct);
        }
        else(input == 1);
        
                numOneTensHigh + numOneOnesHigh;
                numTwoTensHigh + numTwoOnesHigh;
                numThreeTensHigh + numThreeOnesHigh;
                System.out.println(highproduct);             
            }
if(input == 0){
        {

remove one of the {'s

and also: re-check what you're doing here:

else(input == 1);

Thanks for the help, and someone told me that I should use the (input == #); over what I had to eliminate an error. This was to read the input from the user to determine if they want to find the highest or lowest product. I'm going to revise this program completely this weekend so that it tests every possibility completely since I have no clue how I should fabricate it within a loop. If there were a way to switch the integers n1, n2, n3 with the initial tens value for either, I could use a loop; however, I'm new to java (more of a VB guy) and am unaware as to how to do such a thing. If I were to code it, it would end up being very unorthodox as most of my programs do.

(input == #);

yes and no.
yes: you do need to do

if(input == valueToCheck)

if it are integers, since you want to compare the value to equality and they're not Objects.
no: you do not want to put the ';' straight after the if. what happens if what is between the brackets is true, is what is behind the if statement, until the first ';' if there are no '{ }'
so, if you would check it like that, and put the ';', you would automatically run the next lines, if the statement was true or not, since the next lines are not dependent on the if.

if(input == 0){
        {
            numOneLow = numOneTensLow + numOneOnesLow;
            numTwoLow = numTwoTensLow + numTwoOnesLow;
            numThreeLow = numThreeTensLow + numThreeOnesLow;
            System.out.println(lowproduct);
        }
        else(input == 1);
        
                numOneTensHigh + numOneOnesHigh;
                numTwoTensHigh + numTwoOnesHigh;
                numThreeTensHigh + numThreeOnesHigh;
                System.out.println(highproduct);             
            }

I don't think this approach would always work.
E.g. 96*85*74 = 603840 whereas 76*85*94 = 607240 you will have to try out all possible combinations of them and then check for the maximum among them as shown by me in post #6

if(input == 0){
        {
            ...
        }
        else(input == 1);
        
               ...            
            }

I don't think this approach would always work.

especially not if you copy the errors we pointed out a couple posts above ;) :)

thats for him to remove, I just copied the code he has pasted a couple posts above in the thread. Its certainly not my job.

I'm going to reformat this in a new manner:

Going to go to the high if as i had before, try all combos, then print highest result.

Then I'll go to the low and do the same with the least. I will repost the reformatted code soon.

Thats what I have been suggesting you since a long while ago.

No need to be haughty; I'm just trying to follow curriculum to the best of my ability. Now I'm going to have to devise a makeshift program over an array or loop. Here's what I have changed, trying every combination possible and printing the actual already-known highest and lowest based on output.

public class Powers
{ 
    public static void main(String[] args){
 
        ConsoleIO console = new ConsoleIO();    
        int input;
        int numOneTensLow = 10;
        int numTwoTensLow = 20;
        int numThreeTensLow = 30;
        int numOneOnesLow = 4;
        int numTwoOnesLow = 5;
        int numThreeOnesLow = 6;
        int numOneTensHigh = 90;
        int numTwoTensHigh = 80;
        int numThreeTensHigh = 70;
        int numOneOnesHigh = 6;
        int numTwoOnesHigh = 5;
        int numThreeOnesHigh = 4;
        int numOneLowOne = numOneTensLow + numOneOnesLow;
        int numTwoLowOne = numTwoTensLow + numTwoOnesLow;
        int numThreeLowOne = numThreeTensLow + numThreeOnesLow;
        int numOneHighOne = numOneTensHigh + numOneOnesHigh;
        int numTwoHighOne = numTwoTensHigh + numTwoOnesHigh;
        int numThreeHighOne = numThreeTensHigh + numThreeOnesHigh;
        int numOneLowTwo = numOneTensLow + numOneOnesLow;
        int numTwoLowTwo = numTwoTensLow + numThreeOnesLow;
        int numThreeLowTwo = numThreeTensLow + numTwoOnesLow;
        int numOneHighTwo = numOneTensHigh + numOneOnesHigh;
        int numTwoHighTwo = numTwoTensHigh + numThreeOnesHigh;
        int numThreeHighTwo = numThreeTensHigh + numTwoOnesHigh;
        int numOneLowThree = numOneTensLow + numTwoOnesLow;
        int numTwoLowThree = numTwoTensLow + numOneOnesLow;
        int numThreeLowThree = numThreeTensLow + numThreeOnesLow;
        int numOneHighThree = numOneTensHigh + numTwoOnesHigh;
        int numTwoHighThree = numTwoTensHigh + numOneOnesHigh;
        int numThreeHighThree = numThreeTensHigh + numThreeOnesHigh;
        int numOneLowFour = numOneTensLow + numTwoOnesLow;
        int numTwoLowFour = numTwoTensLow + numThreeOnesLow;
        int numThreeLowFour = numThreeTensLow + numOneOnesLow;
        int numOneHighFour = numOneTensHigh + numTwoOnesHigh;
        int numTwoHighFour = numTwoTensHigh + numThreeOnesHigh;
        int numThreeHighFour = numThreeTensHigh + numOneOnesHigh;
        int numOneLowFive = numOneTensLow + numThreeOnesLow;
        int numTwoLowFive = numTwoTensLow + numOneOnesLow;
        int numThreeLowFive = numThreeTensLow + numTwoOnesLow;
        int numOneHighFive = numOneTensHigh + numThreeOnesHigh;
        int numTwoHighFive = numTwoTensHigh + numOneOnesHigh;
        int numThreeHighFive = numThreeTensHigh + numTwoOnesHigh;
        int numOneLowSix = numOneTensLow + numThreeOnesLow;
        int numTwoLowSix = numTwoTensLow + numTwoOnesLow;
        int numThreeLowSix = numThreeTensLow + numOneOnesLow;
        int numOneHighSix = numOneTensHigh + numThreeOnesHigh;
        int numTwoHighSix = numTwoTensHigh + numTwoOnesHigh;
        int numThreeHighSix = numThreeTensHigh + numOneOnesHigh;
        int lowproductone = (numOneLowOne * numTwoLowOne * numThreeLowOne);
        int highproductone = (numOneHighOne * numTwoHighOne * numThreeHighOne);
        int lowproducttwo = (numOneLowTwo * numTwoLowTwo * numThreeLowTwo);
        int highproducttwo = (numOneHighTwo * numTwoHighTwo * numThreeHighTwo);
        int lowproductthree = (numOneLowThree * numTwoLowThree * numThreeLowThree);
        int highproductthree = (numOneHighThree * numTwoHighThree * numThreeHighThree);
        int lowproductfour = (numOneLowThree * numTwoLowThree * numThreeLowThree);
        int highproductfour = (numOneHighFour * numTwoHighFour * numThreeHighFour);
        int lowproductfive = (numOneLowFive * numTwoLowFive * numThreeLowFive);
        int highproductfive = (numOneHighFive * numTwoHighFive * numThreeHighFive);
        int lowproductsix = (numOneLowSix * numTwoLowSix * numThreeLowSix);
        int highproductsix = (numOneHighSix * numTwoHighSix * numThreeHighSix);

        System.out.println("This program will calculate three two-digit" +
        " numbers that create the lowest and highest possible products " +
        "using the numbers 1 through 9 once in each.");
        System.out.println("To calculate the highest input 1, to calculate" +
        " the lowest, input 0: ");
        input = console.readInt();
        

        
        if(input == 0){
        {
            System.out.println(lowproductone);
            System.out.println(lowproducttwo);
            System.out.println(lowproductthree);
            System.out.println(lowproductfour);
            System.out.println(lowproductfive);
            System.out.println(lowproductsix);
        }
        else(input == 1);
                System.out.println(highproductone);
                System.out.println(highproducttwo);
                System.out.println(highproductthree);
                System.out.println(highproductfour);
                System.out.println(highproductfive);
                System.out.println(highproductsix);
            }
   
        }}}}

Right now I'm getting an error for having an "else without if", and was hoping someone could help me out. Thanks for the help. I still need to add the printing of the statement of what the highest/lowest combos actually are, but I'll add it once I get the if statement to work.

I hope that you realize that this is wrong:

if(input == 0){
        {

        }
        else(input == 1);
  
            }

You should have this:

if (input==0) {

} else {

}

OR:

if (input==0) {

} else if (input==1) {

} else {

}

No need to be haughty

I think you need to show more courtesy to the fellow who's trying to help for long. You are showing me the same solution here that I had hinted to you ages earlier (take a closer look at my previous posts and try to understand them, cause I know you sure haven't) and that's what I meant from my previous post. I do not know which curriculum you are trying to follow by not taking the hints and not reaching the solution at the earliest.

Snobbish remarks with assistance doesn't justify your self-righteousness, veruckt24. I know you're making a conscious decision to help me with this and I am very appreciative, but to become upset over the fact that I didn't take your advice right away on an open forum when I have so many projects moving through me is just plain annoying to me. Thank you for the proper formatting on the if-else statement though. I am sorry I can not determine between a 'good' or 'bad' post, but when changing code you must realize that taking every forum and members thereof in advice can suffice to a mutlitude of mishaps if I were to take everyone's suggestions or changes.

commented: Guess whos the snob here. -1

I am not even asking you to take my advice and I simply don't need to, I have given it and now it's upto you whether to take it or not. It's not that I am to benefit or something from that. What I just had remarked about was that the solution you told me you were implementing had been hinted by me quiet a while ago already.
To that you replied, that I was being haughty which is certainly not going to liked by anyone as far as I know.

And if it's annoying I wasn't the one requesting for help in the first place. This is complete ingrateful nature which I assume from your bad rep has been already noted by many.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.