Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add information from if-else statements to an array Java

I’m new to Java, currently, I’m learning arrays and loops.
I have an interesting task from my homework.

Write a program that will multiply each price passed:

By 2 if the price of the goods is less than 1000.
By 1.5 if the price of the goods is greater than or equal to 1000.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The task:

Write the method public void multiple prices (float [] prices).
It takes an array of prices, and multiplies each price by 2 if the price is less than 1000, and by 1.5 if the price is greater than or equal to 1000.

Examples of tests:

Calling multiplyPrices() with data [100, 1500] should change the transmitted array to [200, 2250].

And here I’m stuck =(

I wrote a code that takes an array, converts it to float, takes its value, and checks it with the if-else statement, the only problem is that I don’t know how to put it back.

My classmate says that it looks as if I calculate the correct values and immediately display them in the console without changing the data of the price array.
The program at the end displays the value of the array, and that’s why I get such a result.
It is necessary not to print x1 and x3 to the console, and I need to save x1 and x3 in an array of prices under the index i. Then, when checking the task, the "correct" should be displayed.

But how can this be done?
Unfortunately with if-else, the data could be extracted neither back, nor forward.

Can you give some advice?

Thank you in advance!

import java.util.Arrays;

public class QuadraticEquationSolver {
    
    public void multiplyPrices(float[] prices) {
        
        for (int i = 0; i < prices.length; i++) {           
            float season = prices[i];
            
                if (season >= 1000) {   
                    float x1 = season * 1.5f;
                    float x2 [] = new float[] {x1};
                    System.out.print(Arrays.toString(x2));
                    
                } else if (season < 1000 ) {
                    float x3 = season * 2f;
                    float x4 [] = new float[] {x3};
                    System.out.print(Arrays.toString(x4));
            }   
        }
    }

    
    public static void main(String[] args) {
        QuadraticEquationSolver shop = new QuadraticEquationSolver();

        //Should be [200, 2250]
        float[] prices = new float[] {100f, 1500f};
        shop.multiplyPrices(prices);
        System.out.println(Arrays.toString(prices));
    }
}

>Solution :

Try the below code:

import java.util.Arrays;

public class QuadraticEquationSolver {
    public void multiplyPrices(float[] prices) {
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < 1000)
                prices[i] = prices[i] * 2;
            else
                prices[i] = (float) (prices[i] * 1.5);
        }
    }

    public static void main(String[] args) {
        QuadraticEquationSolver shop = new QuadraticEquationSolver();

        //Should be [200, 2250]
        float[] prices = new float[] {100f, 1500f};
        shop.multiplyPrices(prices);
        System.out.println(Arrays.toString(prices));
    }
}

Read more on java arrays here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading