Reputation: 243
I am a beginner with Java and losing the plot. Could someone help me, I am simply trying to get this code to work. Everything runs fine except the calculations at the end. I have copied and pasted code and output.
import java.util.Scanner;
public class newfile {
public static void main(String[]args) { // starting point of the program
Scanner input = new Scanner(System.in);
int oneA, oneB, oneC;
double finA=0, finB=0, finC=0,average=0;
System.out.println("Hello, Welcome to the 'Calculate Your Grades' program!\nPlease input your predicted grade for Assignment 1A in a percentage format:");
oneA=input.nextInt();
System.out.println("Thank you, can you please input your predictive grade for Assignment 1B?");
oneB=input.nextInt();
System.out.println("Finally, can you please input your predictive grade for Assignment 1C?");
oneC=input.nextInt();
average=(oneA+oneB+oneC)/3;
System.out.println("Excellent, Thank you. The program has calculated your average grade to be "+average+"%");
finA=(oneA/100)*25;
finB=(oneB/100)*25;
finC=(oneC/100)*50;
System.out.println("This module states:\n Assignment 1A is weighted at 25%. You achieved "+finA+"%\n Assignment 1B is weighted at 25%. You achieved "+finB+"%\n Assignment 1C is weighted at 50%. You achieved "+finC+"%");
}
}
Output:
--------------------Configuration: newfile - JDK version 1.7.0_45 <Default> - <Default>--------------------
Hello, Welcome to the 'Calculate Your Grades' program!
Please input your predicted grade for Assignment 1A in a percentage format:
60
Thank you, can you please input your predictive grade for Assignment 1B?
60
Finally, can you please input your predictive grade for Assignment 1C?
60
Excellent, Thank you. The program has calculated your average grade to be 60.0%
This module states:
Assignment 1A is weighted at 25%. You achieved 0.0%
Assignment 1B is weighted at 25%. You achieved 0.0%
Assignment 1C is weighted at 50%. You achieved 0.0%
Process completed.
The build report is clear.
Upvotes: 0
Views: 115
Reputation: 2772
This is your fixed code.
import java.util.Scanner;
public class newfile {
public static void main(String[] args) { // starting point of the program
Scanner input = new Scanner(System.in);
int oneA, oneB, oneC;
double finA = 0, finB = 0, finC = 0, average = 0;
System.out.println("Hello, Welcome to the 'Calculate Your Grades' program!\nPlease input your predicted grade for Assignment 1A in a percentage format:");
oneA = input.nextInt();
System.out.println("Thank you, can you please input your predictive grade for Assignment 1B?");
oneB = input.nextInt();
System.out.println("Finally, can you please input your predictive grade for Assignment 1C?");
oneC = input.nextInt();
average = (oneA + oneB + oneC) / 3;
System.out.println("Excellent, Thank you. The program has calculated your average grade to be " + average + "%");
finA = (oneA / 100.0) * 25;
finB = (oneB / 100.0) * 25;
finC = (oneC / 100.0) * 50;
System.out.println("This module states:\n Assignment 1A is weighted at 25%. You achieved " + finA + "%\n Assignment 1B is weighted at 25%. You achieved " + finB
+ "%\n Assignment 1C is weighted at 50%. You achieved " + finC + "%");
}
}
Upvotes: 0
Reputation: 4470
When dividing two integers in java the default behaviour is to return the quotient and truncate the remainder (or decimal) part. So 3/2 == 1
instead of 1.5. When you do a division between an integer and a double (or float), the integer is first converted to a double (or float) and then floating point division is done, returning a double (or float).
In your code the conversion to double is done as part of the assignment to the variables because all of the values in the arithmetic expression are integers. This means that the actual expressions (oneA/100)*25
is performed as integer arithmetic. The division has a quotient of 0, since oneA < 100
, which means the multiplication is just 0*25
.
As ZouZou says you can change the division to be floating point by changing the 100 to 100.0, which means the compiler will treat them as doubles instead of ints, making the division convert oneA to a double first.
Upvotes: 2
Reputation: 93842
average=(oneA+oneB+oneC)/3;
Same for here :
finA=(oneA/100)*25;
finB=(oneB/100)*25;
finC=(oneC/100)*50;
Dividing integers in a computer program requires special care. Some programming languages, treat integer division (i.e by giving the integer quotient as the answer). So the answer is an integer.
You're performing integer division. Hence :
60/100 => 0
You can avoid that by making one of argument as double :
finA=(oneA/100.0)*25;
finB=(oneB/100.0)*25;
finC=(oneC/100.0)*50;
Upvotes: 7