Help with project 20 (second one) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help with project 20 (second one)

Why I have to create this loop dividing the "amount" with INTEGERS instead of double? There is a loss of information as the compiler says....

4th Mar 2022, 6:25 AM
Миша Момотюк
Миша Момотюк - avatar
5 Answers
+ 1
Can you add a link to your code? it would help others to better understand the problem if they can view the code ... https://www.sololearn.com/post/75089/?ref=app
4th Mar 2022, 6:36 AM
Ipang
+ 1
Миша Момотюк That is because you have specified the input to be a double, not an int. input -> 7.0 is ok input -> 7 is error
4th Mar 2022, 8:50 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Миша Момотюк You need to change all your input parameters to meet the int criteria. Also, create a container to catch your double result. import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double result = 0; int amount = scanner.nextInt(); for (int i = 1; i <= 3; i++){ result += amount * 0.9; } System.out.println(result); } } I put in += after result because the loop indicated that you wanted the result variable to grow, not just continue to be re-assigned with the same value each iteration
4th Mar 2022, 9:30 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double amount = scanner.nextDouble(); for (int i = 1; i <= 3; i++){ amount = amount*0.9; } System.out.println(amount); } } //It works perfectly with double but not int
4th Mar 2022, 6:55 AM
Миша Момотюк
Миша Момотюк - avatar
0
That's how that normally works. But the task requires me to make this work with int, not double(7.0 is error, 7 is ok). If i change it to int, the compiler will say "there is a data loss!"
4th Mar 2022, 9:17 AM
Миша Момотюк
Миша Момотюк - avatar