My journey to Java programming :D

+1 Kevin Roy · July 7, 2015

Hi guys. I need your help on this java exercise I'm doing (I just started learning java like a couple of days ago). What i wanted it to be is the user enters a score, then the program ask if the user wants to add another score, (1 for Yes, and 0 for No). if the answer is 1, it should prompt again to enter another score, if the user input is 0, it sums up all the inputs and displays the average. I believe I'm missing something on the if statement (right on the word "return". Can someone help me on this one? thanks a lot!

import java.util.Scanner;

class kenny {
private static Scanner num;
private static Scanner ans;

public static void main(String[] args){

num = new Scanner(System.in);
ans = new Scanner(System.in);

int ansf;
int total = 0;
int score;
int average;
int counter = 0;


System.out.println("Enter Score here:");
score = num.nextInt();
total = total + score;
counter++;

System.out.println("Add another score? 0 = No / 1 = Yes");
ansf = ans.nextInt();

if(ansf == 1){
return;
} else{
average = total / counter;
System.out.println("Your average score is " + average);

}

}
}

Post a Reply

Replies

Oldest  Newest  Rating
0 jinu mm · July 7, 2015
nice question but i ddnot know by http://yseo.in
0 ashiq sultan · July 8, 2015
hi buddy,
your code is correct but you should not use a If else statement to achieve what you desire rather use a do while loop
like this

do{

System.out.println("Enter Score here:"); 
score = num.nextInt(); 
total = total + score; 
counter++; 
System.out.println("Add another score? 0 = No / 1 = Yes"); 
ansf = ans.nextInt(); 

}while(ansf==1);

//What this does is it loops through the above code until the ansf==1

average = total / counter; 
System.out.println("Your average score is " + average); 

delete your codes below your variable initializations and paste this, it works
  • 1

General Chat

107,272 followers
About

Chat about anything non-computer related including, music, movies, tuna sandwiches, and more!

Links
Moderators
Bucky Roberts Administrator