Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[NEW]I need help with java programming caculating averages

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
muffinman177
Master Cheater
Reputation: 0

Joined: 09 Dec 2006
Posts: 377
Location: Drury Lane

PostPosted: Sat Oct 04, 2008 11:43 pm    Post subject: [NEW]I need help with java programming caculating averages Reply with quote

Code:
import java.util.Scanner;
public class TestAverage
{
   public static void Howmany()
   {
      
      Scanner kb = new Scanner(System.in);
      String one, two, three, four, five, test;
      int testone, testtwo, testthree, testfour, testfive, average;
      
      one = "one";
      two = "two";
      three = "three";
      four = "four";
      five = "five";
      
      System.out.println("How many tests do you have?..Please type using letters!");
      
      test = kb.nextLine();
      
      if (test.equals(one))
      {
         System.out.println("Please enter one test grade");
         testone = kb.nextInt();
         System.out.println("You're test average is: "+testone);
      }
      else if (test.equals(two))
      {
         System.out.println("Please enter two test grades");
         testone = kb.nextInt();
         testtwo = kb.nextInt();
         average = (testone+testtwo)/2;   
      
         System.out.println("You're test average is: "+average);
      }
      else if (test.equals(three))
      {
         System.out.println("Please enter three test grades");
         testone = kb.nextInt();
         testtwo = kb.nextInt();
         testthree = kb.nextInt();
         average = (testone+testtwo+testthree)/3;   
      
         System.out.println("You're test average is: "+average);   
      }
      else if (test.equals(four))
      {
         System.out.println("Please enter four test grades");
         testone = kb.nextInt();
         testtwo = kb.nextInt();
         testthree = kb.nextInt();
         testfour = kb.nextInt();
         average = (testone+testtwo+testthree+testfour)/4;   
      
         System.out.println("You're test average is: "+average);   
      }
      else if (test.equals(five))
      {
         System.out.println("Please enter five test grades");
         testone = kb.nextInt();
         testtwo = kb.nextInt();
         testthree = kb.nextInt();
         testfour = kb.nextInt();
         testfive = kb.nextInt();
         average = (testone+testtwo+testthree+testfour+testfive)/5;   
      
         System.out.println("You're test average is: "+average);   
      }
      else
      {
         System.out.println("Ok fine... only numbers one through five :)");
      }
      
      
   
   }
   public static void main(String[]args)
   {
      Howmany();
   }
}


ok..
this works.. but is there a more efficient way to make a program like this? as in.. without having to make a new else ststment for each number of tests

im in computer science I.. and were just beginning to learn how to use the Scanner utility and i kinda read ahead.. but i still wanna know how :]
Back to top
View user's profile Send private message
Snootae
Grandmaster Cheater
Reputation: 0

Joined: 16 Dec 2006
Posts: 969
Location: --->

PostPosted: Sun Oct 05, 2008 12:11 am    Post subject: Reply with quote

try this: (read down for explanation)
Code:
import java.util.Scanner;
public class TestAverage
{
   public static void Howmany()
   {
       
      Scanner kb = new Scanner(System.in);
      String one, two, three, four, five;
      int testamount, testone, testtwo, testthree, testfour, testfive, average;
     
      System.out.println("How many tests do you have?..Please type using numbers!");
       
      testamount = kb.nextLine();
       

 System.out.println("Please enter " + testamount + " test grade/s");


if (test >= 1)) {
testone = kb.nextInt(); }
if (test >= 2)) {
testtwo = kb.nextInt(); }
if (test >= 3)) {
testthree = kb.nextInt(); }
if (test >= 4)) {
testfour = kb.nextInt(); }
if (test >= 5)) {
testfive = kb.nextInt(); }

if (testamount = 1) {
average = (testone)/testamount;
}
if (testamount = 2) {
average = (testone, testtwo)/testamount;
}
if (testamount = 3) {
average = (testone, testtwo, testthree)/testamount;
}
if (testamount = 4) {
average = (testone, testtwo, testthree, testfour)/testamount;
}
if (testamount = 5) {
average = (testone, testtwo, testthree, testfour, testfive)/testamount;
}

  System.out.println("You're test average is: "+average);   
   
   }


   public static void main(String[]args)
   {
      Howmany();
   }
}

i dont know any java, but i should be able to help you

for starters, you should be ble to change this:
System.out.println("Please enter five test grades");
into something with a variable in it, ie:
Code:
System.out.println("Please enter " + test + " test grades");


not sure exactly how that will work in java, but the idea is the test variable holds the right string, say one two three, so you dont need an if for every one

an easier way to get the right amount of inputs would be have
Code:
 System.out.println("How many tests do you have?..Please type using [b]numbers[/b]!");
testint = kb.nextLine();


so your test will be a number (1 2 3 4 5)

after that, you can use something like:

if (test >= 1)) {
testone = kb.nextInt(); }
if (test >= 2)) {
testtwo = kb.nextInt(); }
if (test >= 3)) {
testthree = kb.nextInt(); }
if (test >= 4)) {
testfour = kb.nextInt(); }
if (test >= 5)) {
testfive = kb.nextInt(); }

so if test is greater than or equal to two, it will ask for two numbers, and so on

this code will probably not directly work, as i dont know java, but this is a rough idea, and will make your code a fifth of the size

_________________
Back to top
View user's profile Send private message
DoomsDay
Grandmaster Cheater
Reputation: 0

Joined: 06 Jan 2007
Posts: 768
Location: %HomePath%

PostPosted: Sun Oct 05, 2008 1:17 am    Post subject: Reply with quote

Code:
class AvgCalc
{
   public static void main(String [] args)
   {
      Scanner in = new Scanner(System.in);
      System.out.print("Please enter a list of grades, terminate with -1: ");
      int gc = 0;
      int sum = 0;
      for (int t = 0; t!= -1; t = in.nextInt())
      {
         sum +=t;
         gc++;
      }
      System.out.println("The average is " + 1.0 * sum / gc);
   }
}
Back to top
View user's profile Send private message
muffinman177
Master Cheater
Reputation: 0

Joined: 09 Dec 2006
Posts: 377
Location: Drury Lane

PostPosted: Sun Oct 05, 2008 8:12 pm    Post subject: Reply with quote

DoomsDay wrote:
Code:
class AvgCalc
{
   public static void main(String [] args)
   {
      Scanner in = new Scanner(System.in);
      System.out.print("Please enter a list of grades, terminate with -1: ");
      int gc = 0;
      int sum = 0;
      for (int t = 0; t!= -1; t = in.nextInt())
      {
         sum +=t;
         gc++;
      }
      System.out.println("The average is " + 1.0 * sum / gc);
   }
}


calculations are wrong.. and could you explain how to use "for"
Back to top
View user's profile Send private message
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Sun Oct 05, 2008 8:23 pm    Post subject: Reply with quote

for (Initialize variables; Condition-to-loop; What-to-do-at-the end-of-every-loop)
{
}

_________________
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Mon Oct 06, 2008 12:35 am    Post subject: Reply with quote

Just to expand on what lurc said:

The for loop allows you to execute code a number of times:

Code:
for(int i = 0; i < 10; i++)
{
    // Code here
}


The "code here" line would be executed 10 times. What the first line means is "initialize a variable called i and set it to 1, then loop through this code while i < 10. at the end of each loop, add one to i". For the first time the loop goes round, i would be zero. The second time i would be 1, until the last loop where i would be 9. At this point, the i++ is executed and i becomes 10, which means i < 10 is no longer true and the loop exits.

So, if I wanted to write "Looping is fun!" twenty times to the screen, I could do this:

Code:
for(int i = 0; i < 20; i++)
{
    System.out.println("Looping is fun!");
}


And there we have it. You can use any logical expression instead of i < 20, for example i > 12 or i <= n. You can also loop downwards by using i-- instead of i++.

Here's a simple little application to print out the times tables from 0x0 to 10x10:
Code:
for(int a = 0; a <= 10; a++)
{
    for(int b = 0; b <= 10; b++)
    {
        System.out.println(a + " x " + b + " = " + a * b);
    }
}


Hope I've not messed anything up there, I've not coded Java in years!
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Mon Oct 06, 2008 12:46 am    Post subject: Reply with quote

Code:
c=0;
while(c<5){
dosomethinghere;
c++;}

==
Code:
for(c=0;c<5;c++){
dosomethinghere;}


So you could just replace
[code[if (test >= 1)) {
testone = kb.nextInt(); }
if (test >= 2)) {
testtwo = kb.nextInt(); }
if (test >= 3)) {
testthree = kb.nextInt(); }
if (test >= 4)) {
testfour = kb.nextInt(); }
if (test >= 5)) {
testfive = kb.nextInt(); } [/code]
with
Code:
for(numentered=0, total = 0;numentered <test;numentered++)) {
total += kb.nextInt(); }
avg = total/test;

_________________
Back to top
View user's profile Send private message
DoomsDay
Grandmaster Cheater
Reputation: 0

Joined: 06 Jan 2007
Posts: 768
Location: %HomePath%

PostPosted: Mon Oct 06, 2008 2:43 am    Post subject: Reply with quote

muffinman177 wrote:
DoomsDay wrote:
Code:
.
.
.


calculations are wrong.. and could you explain how to use "for"
My bad, make that (1.0 * sum) / (gc-1)
Back to top
View user's profile Send private message
nog_lorp
Grandmaster Cheater
Reputation: 0

Joined: 26 Feb 2006
Posts: 743

PostPosted: Thu Oct 09, 2008 2:56 am    Post subject: Reply with quote

Was gonna say, way to convert the truncated int into a float noob Very Happy.

Calculations were wrong because the 'int' type cannot handle decimals and such, so it round them down to the nearest whole number. You could do it without being a lil' smartass by casting it to float. (float) a / (float) b.

For loops accept three expressions separated by semicolons.

for (initialize loop variables; loop condition; increment loop variables)

Is the traditional form, but it isn't super strict, you could do
for(int i; i<100; System.out.print("MOO"));
just to be a dumbass.

Also, doomsy, java initializes variables to 0 for you Very Happy

TILDAnog_lorp

_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites