Author Topic: [solved][java] help!  (Read 14986 times)

0 Members and 1 Guest are viewing this topic.

Offline Madskillz

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 488
  • Rating: +32/-2
    • View Profile
Re: [java] help!
« Reply #30 on: April 10, 2011, 05:11:34 pm »
For one thing I would simplify all the variables you really only need 3 for this project. This should simplify your code and help you follow whats going on easier. I think you are getting some variables confused.
Sum, lowest and number of entries.

In your code, it looks like lowest is never actually given a value, except for when you initialize it. That's your 0 as your first println. You have low = lowest, switch it to lowest = score;

Nemo really had it spot on in his thinking. By setting the lowest value to one greater than the highest possible answer, this automatically makes the first entry entered the first lowest number. Then you simply have to check the incoming score against the lowest and if it is lower than the lowest value set the lowest value to this new value. Which is along the same lines as I think you were thinking, but his saves you some code.  I would just keep adding the scores to the total and then subtract the lowest when it comes to printing the results. But we all do stuff differently.

Edit: I modified your code, it's quick and dirty but it should fix your problems. Your score variable could be kept, I just took it out.
Code: [Select]
public class count {
   public static void main(String[] args) {

       int lowest = 0;
       int total = 0;
       int numEntries = 0;

       String tempScore = JOptionPane.showInputDialog("Enter your test score (negative to exit):");
       lowest = Integer.parseInt(tempScore);
       total += Integer.parseInt(tempScore);
       numEntries++;

       while (true) {
       tempScore = JOptionPane.showInputDialog("Enter your test score (enter a negative number to exit):");
       if (Integer.parseInt(tempScore) < 0) {  
               break;
       }
       else{
                       if (Integer.parseInt(tempScore) < lowest) {
                               lowest = Integer.parseInt(tempScore);
                       total  += Integer.parseInt(tempScore);
                       }
                       else {
                       total  += Integer.parseInt(tempScore);
                       }
       }
       numEntries++;
   }
       total = total-lowest;
    System.out.println("Lowest Score:" + lowest);
    System.out.println("Number of Total Entries: " + numEntries);
    System.out.println("Total:" + total);  
    System.out.println("Average:" + (double)total/(numEntries-1));


// System.out.println("your average of the test scores minus the lowest score is: " + total);
}
}
« Last Edit: April 10, 2011, 05:33:49 pm by Madskillz »

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java] help!
« Reply #31 on: April 10, 2011, 06:18:20 pm »
wtf that's how my code was structured! Literally.

pseudo code:

Code: [Select]
enter test number
lowest = test number
total += test number

loop {
if score is negative {
  break
}
else
{
if score < low {
  lowest = score
  total += score
}
else
{
  total += score
}

number of entries ++

//all math here and rest of program blah blah blah

edit: how come mine didnt work but your's did. Also more importantly.. how did the lowest score never get set to? That's what I want to know because it should have
« Last Edit: April 10, 2011, 06:19:24 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline Madskillz

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 488
  • Rating: +32/-2
    • View Profile
Re: [java] help!
« Reply #32 on: April 10, 2011, 08:16:03 pm »
You had low = lowest in the second if statement in the while loop. You actually wanted lowest = score. I think with having both a low and lowest variable you were confusing yourself.  ;) That's why I suggested keeping things simple and minimalistic. But yeah, I didn't right my own code, all that was you. I just modified it to fix your issues.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java] help!
« Reply #33 on: April 10, 2011, 09:55:47 pm »
:D thanks! yeah what I was doing is comparing the current score with the last score (low) and if it was lower than the last one then its the lowest... although now that im typing this lol im not sure what good that would do me since i still have to compare (lowest) to something.. if thats even how i did it! lol your right i did confuse myself

edit: why did you parseInt so many times, why not just once?
« Last Edit: April 10, 2011, 09:56:26 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline Madskillz

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 488
  • Rating: +32/-2
    • View Profile
Re: [java] help!
« Reply #34 on: April 10, 2011, 10:10:21 pm »
I was just freeing up variables, no reason in particular. That's why I said you could keep the score variable that you had.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [java] help!
« Reply #35 on: April 11, 2011, 05:25:12 am »
:D thanks! yeah what I was doing is comparing the current score with the last score (low) and if it was lower than the last one then its the lowest... although now that im typing this lol im not sure what good that would do me since i still have to compare (lowest) to something.. if thats even how i did it! lol your right i did confuse myself

edit: why did you parseInt so many times, why not just once?

Did you teacher accept any of those? :)

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java] help!
« Reply #36 on: April 11, 2011, 06:57:20 am »
well, im just gonna use madskills since it looks more along the lines of my work because any of the others are actually too advanced and my teacher will know something is up.. so yeah :(

I would but I might get in trouble for doing so. I hope you can understand
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [solved][java] help!
« Reply #37 on: April 11, 2011, 07:02:27 am »
well, im just gonna use madskills since it looks more along the lines of my work because any of the others are actually too advanced and my teacher will know something is up.. so yeah :(

I would but I might get in trouble for doing so. I hope you can understand

Oh good luck then :)