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

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [java] help!
« Reply #15 on: April 02, 2011, 06:30:42 am »
This is even more off-topic, but here's my version.

Code: [Select]
grades = []
n = lowest = float("inf")   # 'inf' stands for infinity - this actually works.

while n > 0:
    n = float(input("Grade: "))
    if n > 0:
        if n < lowest:
            lowest = n
        grades.append(n)

for i in range(grades.count(lowest)):
    grades.remove(lowest)
   
print("Average = {0}\nLowest =  {1}".format(sum(grades)/len(grades), lowest))

Scout's version was 13 lines long (without empty lines), mine is 11 lines long (without empty lines)  ;D.

Mine also removes any repeated low scores.  If that isn't desired behavior, then you could just remove the 'for' statement and execute 'grades.remove(lowest)' only once.

For the fun of it, 7 lines!

Code: [Select]
inputs = []
while True:
n = input()
if n<0:
break
inputs.append(n)
print "Average: %d\nLowest: %d" % (sum(inputs)/len(inputs),min(inputs))
* Scout wins.

:P

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: [java] help!
« Reply #16 on: April 02, 2011, 03:28:59 pm »
This is even more off-topic, but here's my version.

Code: [Select]
grades = []
n = lowest = float("inf")   # 'inf' stands for infinity - this actually works.

while n > 0:
    n = float(input("Grade: "))
    if n > 0:
        if n < lowest:
            lowest = n
        grades.append(n)

for i in range(grades.count(lowest)):
    grades.remove(lowest)
   
print("Average = {0}\nLowest =  {1}".format(sum(grades)/len(grades), lowest))

Scout's version was 13 lines long (without empty lines), mine is 11 lines long (without empty lines)  ;D.

Mine also removes any repeated low scores.  If that isn't desired behavior, then you could just remove the 'for' statement and execute 'grades.remove(lowest)' only once.

For the fun of it, 7 lines!

Code: [Select]
inputs = []
while True:
n = input()
if n<0:
break
inputs.append(n)
print "Average: %d\nLowest: %d" % (sum(inputs)/len(inputs),min(inputs))
* Scout wins.

:P

Wait, how does that drop the lowest score?
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [java] help!
« Reply #17 on: April 02, 2011, 03:35:51 pm »
min(array) returns the minimum value of an array.

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: [java] help!
« Reply #18 on: April 02, 2011, 04:10:41 pm »
It includes the lowest score in your average calculation, though, which is not what you want. Try this:

Code: (Python) [Select]
inputs = []
while True:
n = input()
if n<0:
break
inputs.append(n)
print "Average: %d\nLowest: %d" % ((sum(inputs) - min(inputs) * inputs.count(min(inputs))) / len(inputs), min(inputs))

And eventually we need to get back on topic :P

Pretty much nemo's, but edited:

Code: (Java) [Select]
int sum;
int numScores;
int lowest = 100;
while (true)
{
try
{
int score = Integer.parseInt(JOptionPane.showInputDialog("Enter test score (negative to exit): "));
}
catch (NumberFormatException exc)
{
break;
}

if (score < 0)
break;

numScores++;

if (score < lowest)
lowest = score;

sum += score;
}
System.out.println("Average: " + ((sum - lowest) / (numScores - 1)), "Lowest: " + lowest);
« Last Edit: April 02, 2011, 04:21:57 pm by Deep Thought »




Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: [java] help!
« Reply #19 on: April 03, 2011, 12:31:02 am »
Question:
What is supposed to happen if you enter multiple low scores?

Like, if the grades entered were
10, 90, 6, 5, 5, 12, -5

Would it calculate the average after dropping the negative and both lowest values
10, 90, 6, 12

Or would it calculate the average after dropping the negative and only one of the lowest values?
10, 90, 6, 12, 5

?
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: [java] help!
« Reply #20 on: April 03, 2011, 09:27:54 am »
well deep thought's code breaks after it reaches a negative number, so if you did 10,90,6,-5,12,5 it would calculate 10, 90, and 6 to get 10+90+6=106/3=35.333....

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: [java] help!
« Reply #21 on: April 03, 2011, 10:49:44 am »
well deep thought's code breaks after it reaches a negative number, so if you did 10,90,6,-5,12,5 it would calculate 10, 90, and 6 to get 10+90+6=106/3=35.333....

Well, it wouldn't even ask you for 12 and 5.




Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: [java] help!
« Reply #22 on: April 03, 2011, 06:22:41 pm »
well deep thought's code breaks after it reaches a negative number, so if you did 10,90,6,-5,12,5 it would calculate 10, 90, and 6 to get 10+90+6=106/3=35.333....

Well, it wouldn't even ask you for 12 and 5.
Well, I mean if you attempted to input that.  Details.... details....

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: [java] help!
« Reply #23 on: April 03, 2011, 08:29:50 pm »
well deep thought's code breaks after it reaches a negative number, so if you did 10,90,6,-5,12,5 it would calculate 10, 90, and 6 to get 10+90+6=106/3=35.333....


if you did 10,90,6,-5 shouldn't 6 be dropped, and you end up with 100/2 = 50?


Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: [java] help!
« Reply #24 on: April 03, 2011, 10:21:35 pm »
well deep thought's code breaks after it reaches a negative number, so if you did 10,90,6,-5,12,5 it would calculate 10, 90, and 6 to get 10+90+6=106/3=35.333....


if you did 10,90,6,-5 shouldn't 6 be dropped, and you end up with 100/2 = 50?
Nope.

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: [java] help!
« Reply #25 on: April 03, 2011, 10:32:25 pm »
I am assigned to make 'A program that will read integer test scores from the keyboard until a negative value is typed in. The program will drop the lowest score from the total and print the average of the remaining scores.'

I agree with nemo.
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: [java] help!
« Reply #26 on: April 03, 2011, 10:53:58 pm »
I am assigned to make 'A program that will read integer test scores from the keyboard until a negative value is typed in. The program will drop the lowest score from the total and print the average of the remaining scores.'

I agree with nemo.
Ah, I totally missed where Deep Thought did that in his code. (sum-lowest) does that.
* Dunce graphmastur  goes and sits in a corner

Offline apcalc

  • The Game
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1393
  • Rating: +120/-2
  • VGhlIEdhbWUh (Base 64 :))
    • View Profile
Re: [java] help!
« Reply #27 on: April 03, 2011, 10:59:41 pm »
* apcalc hits himself for not reading no arrays!

Well, anyways, here is a second shot at this, as I had to go back and do it!  (Yes, I cheated and used a pre-written console input class :P).

Code: [Select]
public class Main {
    public static void main(String[] args) {
       int lowScore=Integer.MAX_VALUE;
       int sum=0;
       int numValues=0;
       int currGrade;
       while(true) {
           System.out.print("Enter Grade:  ");
           currGrade=TextIO.getInt();
           if (currGrade<0) break;
           if(currGrade<lowScore) {
               if(lowScore!=Integer.MAX_VALUE) sum+=lowScore;
               lowScore=currGrade;
               numValues++;
           }
           else {sum+=currGrade;numValues++;}
       }
       System.out.println("Average:  "+(double)sum/(numValues-1));
    }
}


Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: [java] help!
« Reply #28 on: April 04, 2011, 09:45:27 am »
apcalc, I like how you use TextIO Class, I love it too, much better than the scanner.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: [java] help!
« Reply #29 on: April 10, 2011, 03:37:44 pm »
ok, well Thanks for your help so far. I appreciate it. I have one question though.. Why doesnt this work:

Code: [Select]
import javax.swing.JOptionPane;

public class while2 {

    public static void main(String[] args) {

        int temp = 0;
        int low = 0;
        int acc = 0;
        double total = 0;
        int score = 0;
        int lowest = 0;

             String scoretemp = JOptionPane.showInputDialog("Enter your test score (enter a negative number to exit):");
             score = Integer.parseInt(scoretemp);  //5
             low = score;  //low is 5
             total += score;  //total is 5
             acc += 1; //1

        while (true) {
        scoretemp = JOptionPane.showInputDialog("Enter your test score (enter a negative number to exit):");
        score = Integer.parseInt(scoretemp);  //score is now a negative number
        if (score < 0) {  //nope
                break;
        }
        else{
                        if (score < low) {  //if 1 < 2
                               total += low;  // total is total + low (5+4+3+2+1) = 15
                                low = lowest;  //lowest is now 2
                        }
                        else{
                               total += score;
                        }
        }
        acc += 1; //6
        low = score; //low is now 1
    }
     System.out.println(lowest); //lowest is 2
     System.out.println(total);  //total is 15
     total = total - lowest; //total is now 13
     System.out.println(total);
     acc = acc - 1; //5
   System.out.println(acc);
total =(double) total / acc;  //13 / 5 = 2.6
System.out.println(total);


// System.out.println("your average of the test scores minus the lowest score is: " + total);
}
}

I ran this in my head and commented it so that i can keep track of everything that goes on in the program. I did this based on these numbers *in order*: 5,4,3,2,1,<negative number to exit>
here are my results:

0
19.0
19.0
4
4.75

edit: the problem is is that the lowest score for some reason isn't being set in the if block. Thus it's at its default value EVEN THOUGH it Should work
edit 2: for some reason the order on the scores actually makes a difference on what the outcome is  :-\
edit 3: Sorry but im not actually able to use your programs because they are more advanced then what I have learned. I have only learned JOptionPane, while loops, for loops, scanner class, string manipulation, and math functions. D:
« Last Edit: April 10, 2011, 04:00:25 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.