0
[WHAT IS WRONG] Inputting 4 float numbers, either (+ or - ); but only add the negative and then printing the sum, two decimal.
7 Réponses
+ 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
+ 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
+ 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
+ 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
+ 1
Here is the fixed version.
https://code.sololearn.com/cx4cU2R3G3y5/?ref=app
0
Hi Alena Sherine I’ll try to apply your advice. Thank you!
0
Hi Stefanoo Thank you for your help. Have a nice day!