Results 1 to 18 of 18
  1. #1
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Please help!!! Sending a value to a method for a quiz.

    Hello,

    I just start programing 3 weeks ago and boy am I having a blast. It's just that I hit a wall and I hit it hard. I already feel the despair setting in, so I am coming to Java gods for help. Please deities of Java help me understand my errors in my thinking before I seriously not be able to function in life. Okay, I'm being dramatic but I want to understand why it's not working. For my problem, I am creating a program that for my nursing school project. Actually, just for me. You enter a medication in the program gives you a quick quiz. I created a switch statement, if the user get's the answer correct I would like to award the user 10 points. I try and pass the value to a new method and it appears that my switch statement or my method returns a value of 0, when the the program moves to another part of the switch statement. Here is my code:
    Java Code:
       switch(input5){
                         // this is where the user inputs the answer
                    case "Vomiting":
                    // I want score to hold the value of 10 and pass it to a method. Rinse and repeat 
              score = num + 10;
                 score= getMethod(score,0);    
                         // passing the value of score not the variable (i hope)
                   JOptionPane.showMessageDialog (null,"you're correct")  ;
                            break;
                        case "Dizziness":
                          JOptionPane.showMessageDialog (null,"you're correct")  ;
                       getMethod(0,10);    
                            break;
                        case "Confusion":
                           JOptionPane.showMessageDialog (null,"you're correct");
                          
                          System.out.println(getMethod(score,0));
                                    
                            break;
                        case "Vision":
                           JOptionPane.showMessageDialog (null,"you're correct");
                          
                           break;
                        default :
                           JOptionPane.showMessageDialog (null,"you're Wrong");
                            break;
                     }
                        }
                    }
               
                        
    
                    else {
                        JOptionPane.showMessageDialog(null,"Please select again");
                    }
               
                       break;
    
    
    static  int getMethod(int total, int total2)
              // my method 
        {
         int result = total;
          
          System.out.println(result+total2);
          return result;
            
            }
        }
    Last edited by Fubarable; 06-16-2012 at 06:19 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Please help!!! Sending a value to a method for a quiz.

    Hello and welcome to the forum.

    I've added [code] [/code] tags to your code to help make it more readable, but your indentation is all over the place, making your code still very difficult to read. Please fix this so that we can read and understand your code and be able to help you.

  3. #3
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    Sorry, god, I am newbie, I will fix it right now.

  4. #4
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    Can you link me a guide or instructions on how to indent properly. THanks.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Please help!!! Sending a value to a method for a quiz.

    You can Google many such style guides including Java Programming Style Guide: Indentation.

    Myself, I like to use 3 spaces for each new level, but regardless, the key is to be consistent. The lines on the same "level" should all start at the same place so it's easy to see what block a line of code belongs to.

  6. #6
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    Try declaring "result" and "total" as global variables..

  7. #7
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    Here is my code
    Java Code:
    switch(input5){
    // this is where the user inputs the answer
       case "Vomiting":
    // I want score to hold the value of 10 and pass it to a method. Rinse and repeat 
    score = num + 10;
    score= getMethod(score,0);   
    // passing the value of score not the variable (i hope)
    JOptionPane.showMessageDialog (null,"you're correct");
       break;
       case "Dizziness":
    JOptionPane.showMessageDialog (null,"you're correct")  ;
    getMethod(0,10);    
        break;
       case "Confusion":
    JOptionPane.showMessageDialog (null,"you're correct");
    System.out.println(getMethod(score,0));
             break;
      case "Vision":
    JOptionPane.showMessageDialog (null,"you're correct");
            break;
      default :
     JOptionPane.showMessageDialog (null,"you're Wrong");
         break;
                     }
    static  int getMethod(int total, int total2)
              // my method 
      {
       int result = total;
       System.out.println(result+total2);
       return result;
      }

  8. #8
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    Odd, i tried to enter the code tag and had a major fail. I must of misread the link.

  9. #9
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    Quote Originally Posted by Tha Boss View Post
    Try declaring "result" and "total" as global variables..
    Okay, I'm trying now.

  10. #10
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    I am straight up having trouble. I just want the method to add the value, but it just keep storing 10 and won't add them. Where am I going wrong.
    Java Code:
    case "Vomiting":
       test.score = num +10;
       score = getMethod(score,0);  
    break;
    case "Dizziness"
       test.result =num +10;
       result = getMethod(0,score);
       JOptionPane.showMessageDialog (null,"you're correct")  ;
       getMethod(0,10);    
        break;
    static  int getMethod(int total, int total2)
      // my method 
      {
       total =test.score;
       total2 = test.result;
       int answer = total +total2;
       System.out.println(answer);
       return result;
      }
        }
    I don't see where I going wrong with this.

  11. #11
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    In your method, you're adding "total1" and "total2" and storing to the variable "answer" but you return "result" to your program...

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Please help!!! Sending a value to a method for a quiz.

    The method immediately destroys the values passed to it by assigning them new values on line 14 & 15.
    Either use the values of the variables as they are passed to the method or don't pass any.

    When you call the method on line 9 you don't save the value that it returns.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    Thank you my problem is fixed.... I just needed an extra pair of eyes.

  14. #14
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    One last problem, it seems like somewhere a value is stored in on of my variables. It looks like it's a value of 1.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Please help!!! Sending a value to a method for a quiz.

    Can you be a lot more specific.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    Thank you all I made a lot of progress on my program.

  17. #17
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    Quote Originally Posted by Norm View Post
    Can you be a lot more specific.
    When the user types in Vomiting, the program yields 11 instead of 10. I am assuming that the value of one of the variables contains a value. I thought all variables automatically are initialized to 0. If so, then why am I getting 11 instead of 10.

  18. #18
    Louis345 is offline Member
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Please help!!! Sending a value to a method for a quiz.

    Alright, and my last question and honestly I am done being a pest. The numbers finally add, which is a great achievement for me. Been at this for 1 full day. Now when the user presses enter twice it adds an extra 10. For example, the user enters in Vomiting and he yields 10 points, and second the user enters in Dizziness and he is awarded 10 points. The program correctly gives him a score of 20, but once the user clicks on on java window he is awarded another 10 points giving him a total score of 30. I don't know why when the user clicks okay he get's a free 10 points. Only if test were that easy right!!! Again, thanks a million.

Similar Threads

  1. Sending a file to a web page using POST method
    By FlyNn in forum Advanced Java
    Replies: 7
    Last Post: 01-31-2014, 02:28 PM
  2. Really need help for the first Quiz...
    By zhoucao244 in forum New To Java
    Replies: 2
    Last Post: 01-27-2012, 04:18 AM
  3. Help with quiz program
    By askinne2 in forum New To Java
    Replies: 1
    Last Post: 04-16-2010, 10:49 PM
  4. Help with quiz
    By khanzaman in forum AWT / Swing
    Replies: 10
    Last Post: 01-19-2009, 07:45 AM
  5. Help pls with a quiz
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 06:09 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
  •