Page 3 of 4 FirstFirst 1234 LastLast
Results 41 to 60 of 62

Thread: Math game

  1. #41
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Quote Originally Posted by Nanomech View Post
    but I'm getting errors at compile.
    That provides zero information. You need to post the code, the errors it generates and indicate which line is causing the error.

    Java Code:
    public String getWeather(int temp) {
        // assumes temp is in Celsius
        if(temp <= 0) {
            return "It is freezing";
        } else if(temp > 0 && temp <= 10) {
            return "It is quite cold";
        } else if(temp > 10 && temp <= 20) {
            return "It is cool";
        } else if(temp > 20 && temp <= 30) {
            return "It is pleasant";
        } else if(temp > 30 && temp <= 40) {
            return "It is nice and warm";
        } else {
            return "It is really hot";
        }
    }
    
    public void print() {
        String weather = getWeather(25);
        System.out.println(weather);
    }

  2. #42
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    Here's my code that I have. I took JosAH's advice and returned the value of the expression. I want to be able to test the answer with an if statement in the game3() for loop, so that everytime it returns from the method, it brings the result back with it, and an if statement is performed to test wether or not it was correct or wrong. Everytime I take away the if statement in the game3() for loop, it compiles and runs fine, everytime it is added, I get this error:

    Java Code:
    error: Class names, 'MyProgram', are only accepted if annotation processing is explicitly requested
    1 error
    Java Code:
    public static void game3(int tracker, int numberOfTries) {
    	 
    	     int trace = 0;
    	 
    	     System.out.println("");
    	     System.out.println("In the last round it took you " + numberOfTries + " attempts.");
    	     System.out.println("This will count towards your overall score.");
    	     System.out.println("");
    	     System.out.println("The next round is a series of sums.");
    	     System.out.println("The aim of this game is to provide me with the correct answer.");
    		 System.out.println("--------------------------------------------------------------------------------");
    		 System.out.println("Round 3 Commencing...");
    		 System.out.println("");
    
    		 for (int y=1;y<=10;y++) {
    		     askQuestion();
                 
    			 if (ans == true) {
    
                     System.out.println("Correct!");
    				 trace++;
                 }			
                 else {
    
                     System.out.println("Wrong!");
                 }			 
    			 
    		 }
    		 
    		 
    	 
    	 }
    	 
    	 public static boolean askQuestion() {
    	 
    	     String operators = "+-*/";
             Random r = new Random();
    		 boolean answer = false;
    		 int result = 0;
             int left = r.nextInt(30); // generate a number in the range [1, 30)
             int right = r.nextInt(30); // generate a number in the range [1, 30)
             char op = operators.charAt(r.nextInt(operators.length()));
    		 
    		 if (op == '/') {
    		     while (right == 0) {
    			 
    			     right= r.nextInt(30);
    			 }
    		 }
    		 
    		 
    		 System.out.println("");
    		 System.out.print(""+left+" "+op+" "+right+" = ");
    
             switch (op) {
                 case '+': result = left + right; break;
                 case '-': result = left - right; break;
                 case '/': result = left / right; break;
                 case '*': result = left * right; break;
    	     }
    		 
             Scanner s = new Scanner(System.in);
    		 int input = s.nextInt();
    		 
    		 boolean ans = input == result;
    		 return ans;
    		 
    	 }
    Thanks for your example of returning a value though, I just need it explained in an easy way.

    Regards,

    NM.
    [A!B]Java

  3. #43
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nanomech View Post
    Java Code:
    		 for (int y=1;y<=10;y++) {
    		     askQuestion();
                 
    			 if (ans == true) {
    
                     System.out.println("Correct!");
    				 trace++;
                 }			
                 else {
    
                     System.out.println("Wrong!");
                 }
    Your askQuestion() method returns a boolean value but you're completely ignoring it and you assume the existence of variable 'ans' which is local to your askQuestion() method. Why don't you simply do this instead?

    Java Code:
    if (askQuestion()) { // question correctly answere?
       ...
    }
    else { // nope, the answer was wrong
       ...
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #44
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    Ah this is working now, but not quite 100%. See, what is happening now is I am getting 20 questions asked, but it only puts 'Correct' or 'Wrong' on every alternative question. So the first question prints correct or wrong, the 2nd doesn't, the 3rd does, 4th doesn't etc etc. Time to debug! This should be fun
    [A!B]Java

  5. #45
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nanomech View Post
    Ah this is working now, but not quite 100%. See, what is happening now is I am getting 20 questions asked, but it only puts 'Correct' or 'Wrong' on every alternative question. So the first question prints correct or wrong, the 2nd doesn't, the 3rd does, 4th doesn't etc etc. Time to debug! This should be fun
    Show us your code from your first method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #46
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    Ok, so this is where the value of the expression is being returned to.

    Java Code:
    public static void game3(int tracker, int numberOfTries) {
    	 
    	     int trace = 0;
    	 
    	     System.out.println("");
    	     System.out.println("In the last round it took you " + numberOfTries + " attempts.");
    	     System.out.println("This will count towards your overall score.");
    	     System.out.println("");
    	     System.out.println("The next round is a series of sums.");
    	     System.out.println("The aim of this game is to provide me with the correct answer.");
    		 System.out.println("--------------------------------------------------------------------------------");
    		 System.out.println("Round 3 Commencing...");
    		 System.out.println("");
    
    		 for (int y=1;y<=10;y++) {
    		     askQuestion();
                 
    			 if (askQuestion()) {
    
                     System.out.println("Correct!");
    				 trace++;
                 }			
                 else {
    
                     System.out.println("Wrong!");
                 }			 
    			 
    		 }
    [A!B]Java

  7. #47
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nanomech View Post
    Ok, so this is where the value of the expression is being returned to.

    Java Code:
    		 for (int y=1;y<=10;y++) {
    		     askQuestion();
                 
    			 if (askQuestion()) {
    
                     System.out.println("Correct!");
    				 trace++;
                 }			
                 else {
    
                     System.out.println("Wrong!");
                 }
    Do you realize that you're calling method askQuestion() twice? Remove the first call.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #48
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    That's fantastic, I now have to desired result, thank you very much indeed for all your help and also to the others who contributed.

    Regards,

    LC.
    [A!B]Java

  9. #49
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nanomech View Post
    That's fantastic, I now have to desired result, thank you very much indeed for all your help and also to the others who contributed.
    You're welcome of course but please realize that the last mistake you made has nothing to do with object oriented or Java programming; i.e. if you call a method/function/procedure twice in Algol/60, Pascal, PL/I or whatever language, it will execute twice, no matter what you think; all a computer can do is obediently do what you asked for; the trick is to know what you ask for. There is no magic anywhere.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #50
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    I see, I thought that the first one would have done the calculations, then the one within the if statement just checked the result. I'll bear that in mind. Thank you.

    So am I right in saying that if you want to return something from another method, you have to use the method name within your code that you are going to use to do your calculations etc etc.
    [A!B]Java

  11. #51
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nanomech View Post
    I see, I thought that the first one would have done the calculations, then the one within the if statement just checked the result. I'll bear that in mind. Thank you.

    So am I right in saying that if you want to return something from another method, you have to use the method name within your code that you are going to use to do your calculations etc etc.
    Java doesn't work that way (and very few other languages work that way); if you call a method/function/procedure with its parameters, that method executes and optionally returns a value. If it is a void method, no value is returned when the method is done. The caller of that method can ignore the return value or use it. So in this fragment:

    Java Code:
    if (askQuestion( ... )) ...
    The askQuestion( ... ) method is called and its return value is tested in the if-statement. It's just like mathematics, i.e. x+f(42) adds two values, x and whatever f(42) returns (after doing its calculations). Got it?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #52
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    ok so purely for an example,

    Java Code:
    public class Example {
    
         public static void main(String[] args) {
    	 
    	 holder();
    	 
    	 
    	 
    	 
    	 }
         public static int holder() {
    	 
    	     int goThere = 1;
    		 
    		 return goThere;
    	 
    	 }
    }
    Let's say I want to return the value of goThere and print that value out in the main method. How could I go about this?
    Now I know that the goThere variable is local to only the 'holder' method but how do I get to use that variable in the main method?

    Sorry, I know to you guys it's something that is so simple, but I'm finding it difficult getting my head around. Once I understand it for the first time, I have it for life then.

    Apologies,

    LC.
    Last edited by Nanomech; 08-29-2011 at 04:04 PM.
    [A!B]Java

  13. #53
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nanomech View Post
    ok so purely for an example,

    Java Code:
    public class Example {
    
         public static void main(String[] args) {
    	 
    	 holder();
    	 
    	 
    	 
    	 
    	 }
         public static int holder() {
    	 
    	     int goThere = 1;
    		 
    		 return goThere;
    	 
    	 }
    }
    Let's say I want to return the value of goThere and print that value out in the main method. How could I go about this?
    Now I know that the goThere variable is local to only the 'holder' method but how do I get to use that variable in the main method?

    Sorry, I know to you guys it's something that is so simple, but I'm finding it difficult getting my head around. Once I understand it for the first time, I have it for life then.

    Apologies,

    LC.
    You can't use the variable goThere in your main method because it doesn't exist there, i.e. it is local to that other method; you sure can use the return value of that method in any way you like because it is 'given' or returned to the calling method:

    Java Code:
    // in your main method:
    System.out.println("holder: "+holder());
    The code snippet prints holder: 1, i.e. whatever that method returned.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #54
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    Ah I see, so you're using the method name again to get the value from which that method returned, not the variable. So, if I was to do this:

    Java Code:
    public class Example {
    
         public static void main(String[] args) {
    	 
    	 holder();
    	 
    	 
    	 
    	 
    	 }
         public static int holder() {
    	 
    	     int goThere = 10 * 5;
    		 
    		 return goThere;
    	 
    	 }
    }
    This would return 50 to the calling method? From there, I use the method name where the value came from in this instance 'holder()' to retrieve the value which that method returned?

    Regards,

    NM.
    [A!B]Java

  15. #55
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Yes, that method returns the value 50 to the calling method but that calling method ignores the returned value. Where should it be saved? Calling the method holder() again would make it return 50 again, but why call it again if you could've saved the return value the first time it was called?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #56
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    Ok so this method returns 50 to the main method, but because I haven't used it, the main method is ignoring it. What possible ways are there to manipulate a value that's returned?

    for example, if I were to do this:

    Java Code:
    public class Example {
    
         public static void main(String[] args) {
    	 
    	 System.out.println(holder() * 2);
    	 
    	 
    	 
    	 
    	 }
         public static int holder() {
    	 
    	     int goThere = 10 * 5;
    		 
    		 return goThere;
    	 
    	 }
    }
    This would print 100 out?

    Just for clarity, what this program is doing, is it starts at the main method, get's to the println command then, goes into the holder method returns 50, then does the calculation of 50 * 2 before printing out the answer?
    [A!B]Java

  17. #57
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nanomech View Post
    Ok so this method returns 50 to the main method, but because I haven't used it, the main method is ignoring it. What possible ways are there to manipulate a value that's returned?

    for example, if I were to do this:

    Java Code:
    public class Example {
    
         public static void main(String[] args) {
    	 
    	 System.out.println(holder() * 2);
    	 
    	 
    	 
    	 
    	 }
         public static int holder() {
    	 
    	     int goThere = 10 * 5;
    		 
    		 return goThere;
    	 
    	 }
    }
    This would print 100 out?

    Just for clarity, what this program is doing, is it starts at the main method, get's to the println command then, goes into the holder method returns 50, then does the calculation of 50 * 2 before printing out the answer?
    Yep, it prints 100 and your analysis is correct. If you want to save that return value you should store it in a (local) variable:

    Java Code:
    int save= holder();
    System.out.println(save * 2);
    This code snippet also prints out 100 (assuming this is a snippet from your main( ... ) method). Have you studied the Java tutorials? (Google is your friend here)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #58
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default

    I have looked at so many but again the terminology used there is just too hard to grasp. I've managed to find this one: Java Programming Made Easy - Learn to Program Today! which 'simplifies' examples and explains them in a much more delicate way.

    So, for - another - example :P, if I was to type in the main method:- holder(int takeIt);, I can now use this variable in the holder() method. Why can't you return a variable FROM a method, but you can pass a variable INTO a method?

    Thanks so much, I feel I am beginning to understand how to pass values/variables. Maybe my issue is trying to understand absolutely everything rather than taking it for what it is. I like to know 'why this and why that'. Your help has been priceless.

    Regards,

    NM.
    [A!B]Java

  19. #59
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nanomech View Post
    I have looked at so many but again the terminology used there is just too hard to grasp. I've managed to find this one: Java Programming Made Easy - Learn to Program Today! which 'simplifies' examples and explains them in a much more delicate way.

    So, for - another - example :P, if I was to type in the main method:- holder(int takeIt);, I can now use this variable in the holder() method. Why can't you return a variable FROM a method, but you can pass a variable INTO a method?

    Thanks so much, I feel I am beginning to understand how to pass values/variables. Maybe my issue is trying to understand absolutely everything rather than taking it for what it is. I like to know 'why this and why that'. Your help has been priceless.
    Have you tried Oracle's/Sun's Tutorials? Most people study them and they are a must read. You can't pass variables to and from methods; you can pass their values around. The distinction may seem tricky now and then, but keep in mind that Java uses the 'call by value' parameter passing mechanism. For primitive type variables their value is undersandable: ints, doubles, floats, booleans etc. For class type variables their 'reference' is passed around; a reference can be understood as a pointer to the actual thing. The tutorials explain it all.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #60
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Math game

    Ah the value is passed.....I suppose I should have noticed this from passing my tracking variable into my game2 method. I used it to print out the value of the tracker but because I was using the tracker variable, I assumed the variable was passed and not the value. Thank you for clearing that up.

    On a side note - The forums have not been loading for me now for about 1-2 weeks. Has anyone else had this problem? Glad to be back.

    Thank you for all the advice JosAH, it has been really helpful. :thumbsup:
    [A!B]Java

Page 3 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. math and GUI
    By urbanleg in forum AWT / Swing
    Replies: 3
    Last Post: 08-06-2011, 04:05 PM
  2. Replies: 1
    Last Post: 06-02-2011, 09:29 AM
  3. Math average.
    By Willywonka in forum New To Java
    Replies: 3
    Last Post: 04-16-2011, 07:38 PM
  4. Math.cos()
    By ravi1 in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 02:52 PM
  5. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •