0

[WHAT IS WRONG] Inputting 4 float numbers, either (+ or - ); but only add the negative and then printing the sum, two decimal.

https://code.sololearn.com/cZmC4dcZ9xqP/?ref=app

7th Oct 2021, 10:24 AM
ruiririririri
ruiririririri - avatar
7 Respostas
+ 4
I advise you to start sololearn's Java course, that's no doubt, you're not aware of it Variable declaration is one of the first things to learn. float a=1,n,s=0; Here is missing the name of the variable that will be assigned the value 1. Better name it a, to fix your loop error
7th Oct 2021, 10:41 AM
Erlénio.RS
Erlénio.RS - avatar
+ 3
I don't know what you expecting through this program first mistake is in variable declaration u assigned 1 to float here variable is missing then inside loop u used if condition your condition will be failing for positive Integers
7th Oct 2021, 10:34 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
See this one import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); float a=1,n,s=0; n=in.nextFloat(); System.out.println(); while(a<=5) { if(n<0) s=s+n; if(n>0) s=s+n; a++; } System.out.println(" %.2f "+s); } } Note : first do always dry run this a good practice how your program flowing and try to solve your errors self read them properly and try to fix it one by one
7th Oct 2021, 10:39 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
Hi Erlénio.RS you are right. I just started learning coding not long ago. I just wanted to learn java for hobby and the problems that I asked here...I just found them on the internet. I am not properly taught, so I hope you understand. Thank you for the advice though. Appreciate it. Have a nice day
7th Oct 2021, 10:45 AM
ruiririririri
ruiririririri - avatar
7th Oct 2021, 11:20 AM
Stefanoo
Stefanoo - avatar
0
Hi Alena Sherine I’ll try to apply your advice. Thank you!
7th Oct 2021, 10:37 AM
ruiririririri
ruiririririri - avatar
0
Hi Stefanoo Thank you for your help. Have a nice day!
7th Oct 2021, 11:24 AM
ruiririririri
ruiririririri - avatar