Results 1 to 5 of 5
Thread: working out wrong average
- 12-11-2007, 09:08 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 3
- Rep Power
- 0
working out wrong average
hello, i want to work out an average of different books' ratings , but my code is working out the wrong average, also i want to group the ratings that are for the same book (if they are entered more than once).
--------------------------------------------------------
import java.util.Scanner;
public class bookReview4
{
public void readBookTitles(String [] bookTitles)
{
}
public void readRatings(String [] bookTitles)
{ int count=0;
double sum=0;
int arrayposition;
int book=0;
// double average;
int rating=0;
//String [] bookTitles;
int numberofbooks=count+1;
double [] ratingSum= new double [numberofbooks];
Scanner keyboard = new Scanner(System.in);
while(rating<bookTitles.length)
{
System.out.println("please put rating for "+bookTitles[rating]);
ratingSum[count]= keyboard.nextInt();
//sum+=ratingSum[count];
// =rating;
rating++;
//count++;
}
for (rating= 0; rating<bookTitles.length; rating++)
{
sum += ratingSum[count];
}
double average=sum/3;
//average =
System.out.println("average is "+ average);
// for(int i=0 ;bookTitle[rating]=bookTitle[i]; i++)
// {
//
// System.out.println("average for"+bookTitle[i]+ "is ");
// }
//Give user ratings
if (average >=0 && average<1)
{
System.out.println("No rating ");
System.out.println();
}
else if (average >=1 && average<2)
{
System.out.println("Waste Paper ");
System.out.println();
}
else if (average >=2 && average<3)
{
System.out.println("Dissappointing ");
System.out.println();
}
else if (average >=3 && average<4)
{
System.out.println("A good read ");
System.out.println();
}
else if (average >=4 && average<5)
{
System.out.println("Excellent book ");
System.out.println();
}
else if (average >=5)
{
System.out.println("A must read! ");
System.out.println();
}
}
}
- 12-11-2007, 10:09 AM #2
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
On a quick glance:
Why do you have the commented out? Dont you want to iterate over the array of books?Java Code://count++;
Also when you have a problem, it helps to say exactly what your problem is. Saying its "wrong" does no one any good.
- 12-11-2007, 02:03 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 3
- Rep Power
- 0
i try and do this and the program doesnt run properely and stops a the second entry,i want to do it so that it works out the average for all the books combined AND if for e.g. book2 has been entered twice work out an average for each book (book2 +book2/2) that has been entered, and print out what the highest rated book is, which in this case is book2, its not working out the average as a floating point or some reason, do you see what i mean and what i am trying to do?
- 12-11-2007, 09:58 PM #4
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
No, i have no idea what "it doesnt run properly" means. Does it give you an error? Is the answer wrong? If it is wrong, what is the answer, and what SHOULD the answer be? Is that you whole code? Also it helps when you use the [ code] [/ code] tags.
- 12-12-2007, 08:35 AM #5
Member
- Join Date
- Dec 2007
- Posts
- 3
- Rep Power
- 0
yes it is my whole code, it does run there isn't any syntax errors, but its only working out an average for the last entry i input, i want it to work out an average for all the books, so for e.g. if i input book1, book1, book2, book3, i want it to recognise that book1 has been entered twice BUT it is the same book, and work out an average and give it the ratings.... i know its something to do with in.nextInt(); i'm not sure exactly what this does ive been told its appropriate but i dont know what to do with it or where in the code it will go., thanks for your help, much appreciated by the way
Similar Threads
-
Calculate Average
By sthack99 in forum New To JavaReplies: 4Last Post: 06-13-2008, 11:09 AM -
sum,product,average program
By chitwood in forum New To JavaReplies: 7Last Post: 01-24-2008, 05:18 AM -
I need help with my java servlet homework(average)
By jellyfish888 in forum Java ServletReplies: 2Last Post: 12-23-2007, 09:57 PM -
get the average and maximum score
By Eric in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 04:15 AM -
Calculate average age for women and men?
By Legoland in forum New To JavaReplies: 3Last Post: 04-18-2007, 10:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks