I know similar questions have been asked and answered, but my code still isn't working and I wanted to ask you for help.
In my Java code, I want to create a method which turns all negative integers of an array into positive ones.
In the main
method, I then want to create an array, fill it with integers and call the method created above on it.
The compilers don't have a problem with my code, but the output is still filled with negative numbers. What do I do wrong?
Below my code (words in German
, sorry for those who don't understand):
public class BetragAnwendung {
public int[] bildeBetrag(int[] werte) {
for (int i : werte) {
Math.abs(i);
}
return werte;
}
public static void main(String[] args) {
BetragAnwendung betragAnwendung = new BetragAnwendung();
int[] array = { 1, -2, -42 };
int[] positiveArray = betragAnwendung.bildeBetrag(array);
for (int i = 0; i < array.length; i++) {
System.out.println(positiveArray[i]);
}
}
}
The output is:
1
-2
-42
Thanks in Advance!