I don't know why my code is not calculating the average. It is compiling and running, but gives 0% as average. Here are the instructions for the assignment
Average and Grade (Methods Program):
Write a program that asks the user to enter 5 test scores. Your program should then display a letter
grade for each test score, the average test score and the overall letter grade
for the average score. You can NOT use a for loop or an array (we haven't even covered that) in this
program - the point is to learn how to define methods and call them several
times if necessary.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Homework8{
static double average;
static char grade;
public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in);
char letter1;
char letter2;
char letter3;
char letter4;
char letter5;
double score1;
double score2;
double score3;
double score4;
double score5;
double score;
System.out.println("Please enter the first score between 0 and 100: ");
score1 = keyboard.nextDouble();
System.out.println("Please enter the second score between 0 and 100: ");
score2 = keyboard.nextDouble();
System.out.println("Please enter the third score between 0 and 100: ");
score3 = keyboard.nextDouble();
System.out.println("Please enter the fourth score between 0 and 100: ");
score4 = keyboard.nextDouble();
System.out.println("Please enter the fifth score between 0 and 100: ");
score5 = keyboard.nextDouble();
//System.out.println("The average of your five tests was: " +
System.out.println(
"For TestA you scored: " + score1 + " giving you Grade: "
+ determineGrade(score1)
+ "\nFor TestB you scored: " + score2 + " giving you Grade: "
+ determineGrade(score2)
+ "\nFor TestC you scored: " + score3 + " giving you Grade: "
+ determineGrade(score3)
+ "\nFor TestD you scored: " + score4 + " giving you Grade: "
+ determineGrade(score4)
+ "\nFor TestF you scored: " + score5 + " giving you Grade: "
+ determineGrade(score5)
+ "\nBased on your average: " + average + " Your final Grade is: "
+ determineGrade((double)average));
}
public static double calcAverage(double score1, double score2, double score3, double score4, double score5) {
double average = (score1+score2+score3+score4+score5) / 5;
return average;
}
public static char determineGrade(double score)
{
if(score<=100 && score>=90)
{
return 'A';
}
else if(score>=80)
{
return 'B';
}
else if(score>=70)
{
return 'C';
}
else if(score>=60)
{
return 'D';
}
else
{
return 'F';
}
}
}
you define the method calcAverage but you didn't call it.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Homework8{
static double average;
static char grade;
public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in);
char letter1;
char letter2;
char letter3;
char letter4;
char letter5;
double score1;
double score2;
double score3;
double score4;
double score5;
double score;
System.out.println("Please enter the first score between 0 and 100: ");
score1 = keyboard.nextDouble();
System.out.println("Please enter the second score between 0 and 100: ");
score2 = keyboard.nextDouble();
System.out.println("Please enter the third score between 0 and 100: ");
score3 = keyboard.nextDouble();
System.out.println("Please enter the fourth score between 0 and 100: ");
score4 = keyboard.nextDouble();
System.out.println("Please enter the fifth score between 0 and 100: ");
score5 = keyboard.nextDouble();
double average = calcAverage(score1,score2,score3,score4,score5);
//System.out.println("The average of your five tests was: " +
System.out.println(
"For TestA you scored: " + score1 + " giving you Grade: "
+ determineGrade(score1)
+ "\nFor TestB you scored: " + score2 + " giving you Grade: "
+ determineGrade(score2)
+ "\nFor TestC you scored: " + score3 + " giving you Grade: "
+ determineGrade(score3)
+ "\nFor TestD you scored: " + score4 + " giving you Grade: "
+ determineGrade(score4)
+ "\nFor TestF you scored: " + score5 + " giving you Grade: "
+ determineGrade(score5)
+ "\nBased on your average: " + average + " Your final Grade is: "
+ determineGrade((double)average));
}
public static double calcAverage(double score1, double score2, double score3, double score4, double score5) {
double average = (score1+score2+score3+score4+score5) / 5.0;
return average;
}
public static char determineGrade(double score)
{
if(score<=100 && score>=90)
{
return 'A';
}
else if(score>=80)
{
return 'B';
}
else if(score>=70)
{
return 'C';
}
else if(score>=60)
{
return 'D';
}
else
{
return 'F';
}
}
}
Related
I want max and min values of salary to display but i get same values for max and min. Here's my code:
import java.util.*;
class Wage {
String employee_name, skill;
int hours;
double sum;
String[] sno = {"1", "2", "3", "4"};
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
Scanner s = new Scanner(System.in);
public void getEmployeeDetails() {
System.out.println("Welcome to Use General Wage Record System");
for (String count : sno) {
if (count.equalsIgnoreCase("1")) {
System.out.print("Enter Name of Employee 1: ");
employee_name = s.nextLine();
System.out.print("Enter the Skill Level (1,2,3) of Employee:");
Integer level_count = Integer.valueOf(s.nextLine());
if (level_count <= 3) {
System.out.print("Enter the Worked Hours for " + employee_name + ":");
hours = Integer.parseInt(s.nextLine());
if (level_count == 1) {
sum = hours * 15;
}
if (level_count == 2) {
sum = hours * 17;
}
if (level_count == 3) {
sum = hours * 21;
}
System.out.println("The wage of employee" + employee_name + "(Level" + String.valueOf(level_count) + ")" + "for" + hours + " " + "hours is" + " " + "$" + sum);
}
}
if (count.equalsIgnoreCase("2")) {
System.out.print("Enter Name of Employee 2:");
employee_name = s.nextLine();
System.out.print("Enter the Skill Level (1,2,3) of Employee:");
Integer level_count = Integer.valueOf(s.nextLine());
if (level_count <= 3) {
System.out.print("Enter the Worked Hours for " + employee_name + ":");
hours = Integer.parseInt(s.nextLine());
if (level_count == 1) {
sum = hours * 15;
}
if (level_count == 2) {
sum = hours * 17;
}
if (level_count == 3) {
sum = hours * 21;
}
System.out.println("The wage of employee " + employee_name + "(Level " + String.valueOf(level_count) + ")" + "for" + hours + " " + "hours is" + " " + "$" + sum);
}
}
if (count.equalsIgnoreCase("3")) {
System.out.print("Enter Name of Employee 3:");
employee_name = s.nextLine();
System.out.print("Enter the Skill Level (1,2,3) of Employee:");
Integer level_count = Integer.valueOf(s.nextLine());
if (level_count <= 3) {
System.out.print("Enter the Worked Hours for " + employee_name + ":");
hours = Integer.parseInt(s.nextLine());
if (level_count == 1) {
sum = hours * 15;
}
if (level_count == 2) {
sum = hours * 17;
}
if (level_count == 3) {
sum = hours * 21;
}
System.out.println("The wage of employee" + employee_name + "(Level" + String.valueOf(level_count) + ")" + "for" + hours + " " + "hours is" + " $" + sum);
}
}
if (count.equalsIgnoreCase("4")) {
System.out.print("Enter Name of Employee 4:");
employee_name = s.nextLine();
System.out.print("Enter the Skill Level (1,2,3) of Employee:");
Integer level_count = Integer.valueOf(s.nextLine());
if (level_count <= 3) {
System.out.print("Enter the Worked Hours for " + employee_name + ":");
hours = Integer.parseInt(s.nextLine());
if (level_count == 1) {
sum = hours * 15;
}
if (level_count == 2) {
sum = hours * 17;
}
if (level_count == 3) {
sum = hours * 21;
}
System.out.println("The wage of employee" + employee_name + "(Level" + String.valueOf(level_count) + ")" + "for" + hours + " " + "hours is" + "$ " + sum);
}
}
}
}
void average() {
System.out.println("\n\n");
System.out.println("stastical information for bar chart");
System.out.println("==================================");
if (sum > max) {
max = sum;
System.out.println("Maximum of wage" + max + ", " + employee_name);
}
if (sum < min) {
min= sum ;
System.out.println("Minimum of Wage" + min + ", " + employee_name);
}
}
}
public class Pay {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Wage wm = new Wage();
wm.getEmployeeDetails();
wm.average();
}
In the loop of the getEmployeeDetails() method, you don't store and update the min wage, the max wage and the employee names that have these wages.
Besides, you should be more specific in the naming. For example, maxWage and minWage are more meaningful than max and min.
So, you should have maxWage, maxWageEmployeeName, minWage and minWageEmployeeName fields in the field declaration of the Wage class.
At each employee inserted in the system, you should update these values by calling a dedicated method:
public void getEmployeeDetails() {
if (count.equalsIgnoreCase("1")) {
....
updateRanking(sum, employee_name);
}
}
public void updateRanking(double actualWage, String employee_name){
if (actualWage > maxWage) {
maxWage = actualWage;
maxWageEmployeeName = employee_name;
}
else if (actualWage < minWage) {
minWage = actualWage;
minWageEmployeeName = employee_name;
}
}
Besides, your average() method called at the end of your program should only display the result and not performing comparison since you should have updated information about max, min wage and who have these as updateRanking() is called at each insertion of employee wage :
void average() {
System.out.println("\n\n");
System.out.println("stastical information for bar chart");
System.out.println("==================================");
System.out.println("Maximum of wage" + maxWage + ", " + maxWageEmployeeName );
System.out.println("Minimum of Wage" + minWage + ", " + minWageEmployeeName );
}
my question is simple, how can i get my output to format two values with two decimal places. for whatever reason i cant output the "current balance" & "new balance" with two decimal places. separately they work fine but when together i get a conversion error. is this a rule i'm unaware of? i would like do do this with one action if possible. this is simply a cosmetic issue and all operations perform fine when i take out the formatting.
thanks,
public static void main(String[] args)
{
double min;
int accNum;
char accType;
double bal;
double newBal;
//user inputs
System.out.println("Please enter your account number: ");
accNum = console.nextInt();
System.out.println();
System.out.println("Enter the account type: s(savings) or c(checking)");
accType = console.next().charAt(0);
System.out.println();
//savings v checking
switch (accType)
{
case 's':
case 'S':
System.out.println("Enter the current balance: ");
bal = console.nextDouble();
System.out.println("Enter the minimum balance: ");
min = console.nextDouble();
System.out.println();
if (bal < min)
{
newBal = bal - S_MIN_FEE;
System.out.println("Insufficient Funds (-$10.00)");
}
else
newBal = bal + (bal * S_INT);
System.out.printf("Account #: " + accNum + "\n"
+ "Account Type: Savings" + "\n" + "Current Balance: "
+ "$%.2f%n", bal + "New Balance: $%.2f", newBal);
case 'c':
case 'C':
System.out.println("Enter the current balance: ");
bal = console.nextDouble();
System.out.println("Enter the minimum balance: ");
min = console.nextDouble();
System.out.println();
if (bal < min)
{
newBal = bal - C_MIN_FEE;
System.out.println("Insufficent Funds (-$25.00)");
}
else if (bal < C_BAL_MAX && bal >= min)
newBal = bal + (bal * C_INT);
else
newBal = bal + (bal * C_INT_MAX);
System.out.printf("Account #: " + accNum + "\n"
+ "Account Type: Checking" + "\n" + "Current Balance: "
+ "$%.2f%n", bal + "New Balance: $%.2f%n", newBal);
That is because you put the format first, and only first.
System.out.printf("%.2f %.2f%n", bal, newBal);
This is the same problem/mistake as posted an hour ago. java.util.IllegalFormatConversionException: f != java.lang.String Error
I cannot get the variable adjustedCharge to print out (the text is printed but not the value) I have tried to trace but still cannot get it to print properly. No errors are present either.
import java.util.Scanner;
public class Assignment {
public static void main(String[] args) {
//Declare scanner
Scanner input = new Scanner(System.in);
//Prompt for information
System.out.print("Customer's name:");
String name = input.nextLine();
System.out.print("Customer's address:");
String address = input.nextLine();
System.out.print("Customer's phone number:");
String phone = input.nextLine();
System.out.print("Customer's licence number:");
String licence = input.nextLine();
System.out.print("Customer's credit card number:");
String ccard = input.nextLine();
System.out.print("Credit card expiry date (MM/YYYY):");
String expiry = input.nextLine();
System.out.print("Hire length (in days):");
int hire = Integer.parseInt(input.nextLine());
System.out.print("Make/Model:");
String model = input.nextLine();
System.out.print("Registration of car:");
String rego = input.nextLine();
System.out.print("Hire rate (per day) Either S, M, L:");
char inputRate = input.nextLine().charAt(0);
System.out.print("Total days hired out:");
int totalDays = Integer.parseInt(input.nextLine());
//
double dtotalDays = totalDays;
double surcharge = dtotalDays - hire;
//Daily hire rate / Stage 2
double rate = 0;
if (inputRate == 'S' || inputRate == 's'){
rate = (char) 80.0;
}
if (inputRate == 'M' || inputRate == 'm'){
rate= 110.0;
}
if (inputRate == 'L' || inputRate == 'l'){
rate= 140.0;
}
//Calculate late fees
double penalty = rate * (surcharge * 2);
//Start Stage 3
double dCost=0;
double tCost=0;
StringBuilder dDetail = new StringBuilder("The damage Details are:" + "\n");
StringBuilder tDetail = new StringBuilder("The traffic fines are:" + "\n");
//Setup an exit statement
boolean quit = false;
while (!quit){
System.out.println("<<<Enter one of the following commands:>>>");
System.out.println("A - Damage Repair");
System.out.println("B - Traffic Infringement");
System.out.println("X - Exit Menu");
String schoiceEntry = input.next();
char choiceEntry = schoiceEntry.charAt(0);
//Integer.parseInt(input.nextLine());
double damageCost = 0;
switch (choiceEntry){
case 'A':
case 'a':
input.nextLine();
System.out.println("Enter a description of the damage:");
String damageDetail = input.nextLine();
System.out.println("Enter the damage cost:");
damageCost = input.nextInt();
System.out.print("The damage is: " + damageDetail + "\n");
System.out.print("The damage cost is: " + "$" + damageCost + "\n");
dDetail.append("-" + damageDetail + "\n");
dCost = dCost + damageCost;
System.out.println("----------------------------------");
break;
case 'B':
case 'b':
input.nextLine();
System.out.print("Enter a description of the traffic infringement:");
String trafficDetail = input.nextLine();
System.out.println("Enter the traffic infringement cost:");
double trafficCost = Integer.parseInt(input.nextLine());
tDetail.append("-" + trafficDetail + "\n");
tCost = tCost + trafficCost;
System.out.println("----------------------------------");
break;
case 'X':
case 'x':
quit = true;
System.out.print("***Menu entry has been terminated***" + "\n");
//System.out.printf("Total traffic cost is: %75s\n", "$" + tCost + "\n");
//System.out.printf("Total Damage cost is: %77s\n", "$" + dCost + "\n");
//System.out.printf(tDetail + "\n");
//System.out.print(dDetail + "\n");
break;
default:
System.out.print("Please enter either a valid menu selection (A, B, or X):" + "\n");
break;
}
}
double dhire = hire;
double charge = dhire*rate;
double adjustedCharge = charge + penalty;
//Print summary
System.out.println("---------------------------------------------"+
"------------------------------------------------------\n" +
"***CUSTOMER DETAILS:***\n");
System.out.printf("Name: %93s\n", name);
System.out.printf("Address: %90s\n", address);
System.out.printf("Phone Number: %85s\n", phone);
System.out.printf("Licence Number: %83s\n", licence);
System.out.printf("Credit Card Number: %79s\n", ccard);
System.out.printf("Credit Card Expiry: %79s\n", expiry);
System.out.println( "\n***CAR HIRE DETAILS:***\n");
System.out.printf("Make/Model: %87s\n", model);
System.out.printf("Registration Number: %78s\n", rego);
System.out.printf("Hire Length (days): %79s\n", model);
System.out.printf("Daily Hire Rate: %82s\n", rate);
System.out.printf("Basic Hire Charge: %80s\n\n", charge);
System.out.printf("Days hired: %87s\n", totalDays);
if (totalDays == hire){
System.out.printf("Late Return Surcharge: %76s\n", "$0.00");
}
if (totalDays > hire){
System.out.printf("Late Return Surcharge: %76s\n", + penalty);
}
System.out.printf("Adjusted Hire Charge: %77s\n", "\n", "$" + adjustedCharge + "\n");
System.out.print(dDetail + "\n");
System.out.printf("Total damage cost is: %78" + "s\n", "$" + dCost + "\n");
System.out.printf(tDetail + "\n");
System.out.printf("Total traffic fines incurred: %70s\n", "$" + tCost + "\n");
System.out.printf("Final hire charge: %79s\n", "$" + (adjustedCharge + dCost + tCost));
}
}
I just changed the way you printed out the adjustedCharge so you can still use printf
System.out.printf("Adjusted Hire Charge: %77s","$");
System.out.printf("%.1f",adjustedCharge);
System.out.printf("\n");
With printf you need to format the value you're printing and in this case, because you're trying to print a double, you'll need to format it with %f
I also noticed that my previous answer messed up the spacing you had for the rest of your output. So here's the fixed solution! Just change up the spacing to however you'd like
Or you can
use
Pass two arguments to printf
System.out.printf("Adjusted Hire Charge: %77s\n", "$" + adjustedCharge + "\n")
instead of
System.out.printf("Adjusted Hire Charge: %77s\n", "\n", "$" + adjustedCharge + "\n");
My program is supposed to show the calculation of the worker's total salary but somehow after all the input in include the program is end without showing any result.
It also to check if the total hours enter is over 40 and if does, it will calculate the overtime salary.
public class GajiTest {
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
int[] arrayList= new int[numberOfEmp];
for (int i = 0; i < arrayList.length; i++){
System.out.print("Enter Employee Name: ");
String empName= input.next();
System.out.print("Enter hourly rate: ");
int rate= input.nextInt();
System.out.print("Enter hours worked: ");
int hours=input.nextInt();
class Salary {
public double CalculateGaji(int hours,int rate){
if (hours >=40)
{
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else
{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+ "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
}
}}}
I suppose the problem add two lines try this :
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
int[] arrayList= new int[numberOfEmp];
for (int i = 0; i < arrayList.length; i++){
System.out.print("Enter Employee Name: ");
String empName= input.next();
System.out.print("Enter hourly rate: ");
int rate= input.nextInt();
System.out.print("Enter hours worked: ");
int hours=input.nextInt();
Salary sal=new Salary();
sal.CalculateGaji(hours,rate,empName);
}
}
}
class Salary {
public void CalculateGaji(int hours,int rate,String empName){
if (hours >=40)
{
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else
{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+ "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
}
Why it is not printing anything is because you are not calling your method CalculateGaji() of class Salary.
You need to call it by creating the object of the class Salary. (After the local class in the main method.)
Salary salary = new Salary();
salary.CalculateGaji(hours,rate);
Apart from it, there are a few things I would like to say,
You are creating an array of integers int[] arrayList= new int[numberOfEmp]; ArrayList is something else. What is an ArrayList? There should be no need to write the name of the variable to be arrayList. It might confuse you at some stage.
As per your code, you have created the class class Salary in your main method. This way it becomes the [Local Class][2]. If it is what you want then, it is find. Otherwise you could have written your class Salary out side main method and outside class class GajiTest.
But I don't see any need of creating any local class to achieve it. You can directly do it the following way,
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
int[] arrayList= new int[numberOfEmp];
for (int i = 0; i < arrayList.length; i++){
System.out.print("Enter Employee Name: ");
String empName= input.next();
System.out.print("Enter hourly rate: ");
int rate= input.nextInt();
System.out.print("Enter hours worked: ");
int hours=input.nextInt();
if (hours >=40)
{
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else
{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+ "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
From your code, I think what you are trying to do is create an array of employees and print out their names and salaries. There are multiple errors in your code that will not allow it to function if that is your intention.
Here is the code to create an array of employees and print out their names and salaries based on your CalculateGaji function:
public class calculateSalary {
static class employee{
private double rate;
private double hours;
private String empName;
public employee(){}
public void setRate(double value){
rate = value;
}
public void setHours(double value){
hours = value;
}
public void setName(String value){
empName = value;
}
public void CalculateGaji(){
if (hours >=40){
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName +"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName + "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
employee[] employeeArray= new employee[numberOfEmp];
for (int i = 0; i < employeeArray.length; i++){
employeeArray[i] = new employee();
System.out.println("Enter Employee Name: ");
employeeArray[i].setName(input.next());
System.out.println("Enter hourly rate: ");
employeeArray[i].setRate(input.nextDouble());
System.out.println("Enter hours worked: ");
employeeArray[i].setHours(input.nextDouble());
}
for(int i = 0; i < employeeArray.length; i++){
employeeArray[i].CalculateGaji();
}
}
}
Hope this helps.
Apologies for the silly question, I am currently struggling to learn java. I need this code to work so that it will repeat unless '0' is entered for the studentNumber, I'm unsure of how to get the "please enter student number" part to work when I have to declare the int for that before the if statement? I'm not sure if I've approached this completely wrong or what, but I need to be able to repeat the data entry unless "0" is entered as the studentNumber. Thanks for any help!
class Main {
public static void main( String args[] ) {
int studentNumber = BIO.getInt();
if(studentNumber > 0) {
System.out.print("#Please enter the student number : ");
System.out.print("#Please enter the coursework mark : ");
int courseWork = BIO.getInt();
System.out.print("#Please enter the exam mark : ");
int examMark = BIO.getInt();
double average = (double)(courseWork + examMark) / 2;
System.out.printf("sn = " + studentNumber
+ " ex = " + examMark + " cw = " + courseWork
+ " mark = " + average);
} else {
System.out.print("#End of data");
}
}
}
}
Use while()
while(studentNumber > 0){
studentNumber = BIO.getInt();
.........
........
}
See also
while in Java
Use while() instead of if, along with the following changes:
System.out.print("#Please enter the student number : ");
int studentNumber = BIO.getInt();
while(studentNumber > 0) {
System.out.print("#Please enter the coursework mark : ");
int courseWork = BIO.getInt();
System.out.print("#Please enter the exam mark : ");
int examMark = BIO.getInt();
double average = (double)(courseWork + examMark) / 2;
System.out.printf("sn = " + studentNumber
+ " ex = " + examMark + " cw = " + courseWork
+ " mark = " + average);
System.out.print("#Please enter the student number : ");
studentNumber = BIO.getInt();
}
System.out.print("#End of data");
This, as opposed to the other answers, will ensure that even in the first iteration, you perform the check (and promt the user for the student number).
Using Scanner to get the input from the user and process the input value
import java.util.Scanner;
public class ConditionCheck {
public static void main(String[] args) {
Scanner BIO = new Scanner(System.in);
System.out.print("#Please enter the student number : ");
int studentNumber = BIO.nextInt();
if(studentNumber > 0) {
System.out.print("#Please enter the coursework mark : ");
int courseWork = BIO.nextInt();
System.out.print("#Please enter the exam mark : ");
int examMark = BIO.nextInt();
double average = (double)(courseWork + examMark) / 2;
System.out.printf("sn = " + studentNumber
+ " ex = " + examMark + " cw = " + courseWork
+ " mark = " + average);
} else {
System.out.print("#End of data");
}
}
}
You should be using a while statement and do something as below:
class Main
{
public static void main( String args[] )
{
int studentNumber = 1;
While(studentNumber > 0)
{
studentNumber = BIO.getInt();
System.out.print("#Please enter the student number : ");
System.out.print("#Please enter the coursework mark : ");
int courseWork = BIO.getInt();
System.out.print("#Please enter the exam mark : ");
int examMark = BIO.getInt();
double average = (double)(courseWork + examMark) / 2;
System.out.printf("sn = " + studentNumber + " ex = " + examMark + " cw = " + courseWork + " mark = " + average);
}
else
{
System.out.print("#End of data");
}
}
}