Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Need some assistance with the method

  1. #1
    Junior Member
    Join Date
    Apr 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need some assistance with the method

    Create a Java program that uses methods. In the main method, the program must prompt the user to enter an integer number from 1 to 20. Then, the program must call a method named “guess” that receives the integer number and generates a random number (integer) from 1 to 20. The method (guess) must print “Correct!!” if the random number is equal to the number received from the main method. Otherwise, the method must print “Incorrect.”

    Use the following header:
    public static void guess (int x)
    {
    }

    Requirements Specification

    The program must satisfy the following requirements:
    • It must ask the user to enter an integer between 1-20.
    • Call a method named “guess” that generates a random number between 1-20.
    • The method “guess” must compare the two numbers and print either “Correct” or “Incorrect”.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need some assistance with the method

    Do you have any java programming questions about your assignment?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need some assistance with the method

    import java.util.Scanner;

    public class Test{

    public static void main(String []args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter an integer between 1-20:");

    int num = input.nextInt();

    int z = num;

    int a = (int) (1 + Math.random() * 20);

    guess(z,a);


    }

    public static void guess(int x, int d) {

    if(x==d) {

    System.out.print("Correct");
    }

    else {
    System.out.print("Incorrect");
    }
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need some assistance with the method

    Do you have any questions about your assignment?
    Please post them.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Method question

    [Question]
    Create a Java program that uses methods. In the main method, the program must prompt the user to enter an integer number from 1 to 20. Then, the program must call a method named “guess” that receives the integer number and generates a random number (integer) from 1 to 20. The method (guess) must print “Correct!!” if the random number is equal to the number received from the main method. Otherwise, the method must print “Incorrect.”
    Use the following header:
    public static void guess (int x)
    {
    }

    Requirements Specification

    The program must satisfy the following requirements:
    • It must ask the user to enter an integer between 1-20.
    • Call a method named “guess” that generates a random number between 1-20.
    • The method “guess” must compare the two numbers and print either “Correct” or “Incorrect”.

    [Code that i tried]
    import java.util.Scanner;

    public class Test{

    public static void main(String []args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter an integer between 1-20:");

    int num = input.nextInt();

    int z = num;

    int a = (int) (1 + Math.random() * 20);

    guess(z,a);


    }

    public static void guess(int x, int d) {

    if(x==d) {

    System.out.print("Correct");
    }

    else {
    System.out.print("Incorrect");
    }
    }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need some assistance with the method

    Do you have a question? Your assignment is not a question.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    http://www.java-forums.org/misc.php?do=bbcode#code
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need any assistance
    By Chin24 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 16th, 2018, 01:21 PM
  2. Failed to compile, Main Method Not found, assistance please.
    By rickyjoepr@gmail.com in forum Object Oriented Programming
    Replies: 3
    Last Post: September 1st, 2014, 07:17 AM
  3. In need of assistance!
    By Nexcit in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 11th, 2014, 05:24 AM
  4. [SOLVED] for loop assistance
    By Kseidel in forum Loops & Control Statements
    Replies: 3
    Last Post: September 17th, 2012, 08:10 PM
  5. Need some assistance please
    By JavaPhish in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2012, 10:00 AM