Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 13th, 2013, 12:27 PM
sstauraus sstauraus is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 sstauraus Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 13 m 44 sec
Reputation Power: 0
Homework - Beginner Problem: choosing a highest score

Hello anyone,

This is my first post and I would like to one day be able to help others with their programming needs. I am just beginning and have a quick and probably very easy problem.

The task is to create a scanner program that takes 3 quiz score inputs. For one of my methods I must write some code that chooses the HIGHEST score of the 3 no matter what the inputs are.

Right not the method looks like this:

public void highestquiz () {

if (quizScore1 > quizScore2) {
highestScore = quizScore1;
}
else if (quizScore1 > quizScore3){
highestScore = quizScore1;
}

if (quizScore2 > quizScore1) {
highestScore = quizScore2;
}
else if (quizScore2 > quizScore3){
highestScore = quizScore2;
}
if (quizScore3 > quizScore1) {
highestScore = quizScore3;
}
else if (quizScore3 > quizScore2) {
highestScore = quizScore3;
}

} //end highestscore

The program is compiling and running with this as my code, but it seems to choose a score at random to designate which was the highest. I figrue what I typed above was a bit long and tedious to those more experienced than I =)

Any help would be much appreciated.
Comments on this post
Jacques1 disagrees: Cross-posting everywhere on the internet is extremely frustrating for people trying to help you.
Show some respect.

Reply With Quote
  #2  
Old February 13th, 2013, 12:55 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2012
Location: Germany
Posts: 2,033 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 6 Days 20 h 59 m 13 sec
Reputation Power: 812
Hi,

please use [ CODE ] tags to make the code readable.

The problem is that you overwrite the intermediate results. Say you have the numbers 5, 2, 1. Since quizScore1 > quizScore2, you get highestScore = 5 in the first "if" statement. However, in the second statement you have quizScore2 > quizScore3, so highestScore is changed to 2 (and that's the final value).

You'd need to have a single "if-else-if-else-if..." statement.

But you can simplify a code a lot if you first assume quizScore1 to be the highest value and then check each value if it's greater:

Code:
highestScore = quizScore1;
if (quizScore2 > highestScore)
	highestScore = quizScore2;
if (quizScore3 > highestScore)
	highestScore = quizScore3;


And a more intelligent (and object-oriented) approach would consist of defining a "max" method and using that:

Code:
int max(int m, int n) {
	if (m >= n)
		return m;
	else
		return n;
}

... snip ...

highestScore = max(max(quizScore1, quizScore2), quizScore3);


Of course Java already has such a method built-in (Math.max), but I guess you're not supposed to use that?

Reply With Quote
  #3  
Old February 13th, 2013, 04:28 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 3,040 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 36 m 54 sec
Reputation Power: 346
Also posted at http://www.javaprogrammingforums.com/whats-wrong-my-code/23786-beginner-problem-choosing-highest-value-3-inputs.html

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Beginner Problem: choosing a highest score

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap