Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9082167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:29:40+00:00 2026-06-16T20:29:40+00:00

I am trying to get average of 2 random number. the average should 30

  • 0

I am trying to get average of 2 random number. the average should 30 and the first number should be less than the second number. However, I’m stuck in the loop function.

public static void main(String[] args) {
    Scanner user_input = new Scanner(System.in);
    String respond;
    System.out.println("enter OK");
    respond = user_input.next();
    randomAverage();

}

public static void randomAverage(){
    Random random = new Random();
    int average = 30;
    int a = random.nextInt(100); //random range
    int b = random.nextInt(100);
    System.out.println("random a " +a);
    System.out.println("random b "+b);
    while(a>b){
        a = random.nextInt(100); //random range
        b = random.nextInt(100);
        System.out.println("random a " +a);
        System.out.println("random b "+b);
    }
    int c = (a+b)/2;
    while (c>average || c<average){
        a = random.nextInt(100); //random range
        b = random.nextInt(100);
        System.out.println("random a " +a);
        System.out.println("random b "+b);
    }
}

I tried the above function, however I got redundant data

anyone can help me

I’m just started to learn this language

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:29:43+00:00Added an answer on June 16, 2026 at 8:29 pm

    You are never updating the value of c.

    Your second loop should look something like:

    int c = (a+b)/2;
    while (c != average) {              // Simpler conditional
        a = random.nextInt(100);
        b = random.nextInt(100);
        c = (a+b)/2;                    // Must update this.
        System.out.println("random a " +a);
        System.out.println("random b "+b);
    }
    

    Note as radai mentioned,

    Since (a+b)/2 = 30, if you know a you can solve for b. No sense burning CPU cycles when simply b = 60 - a.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the rounded number of the average load in the
I'm trying to get the average number of days between when a user closes
I am stuck trying to get an overall average elapsed time between 2 dates
I'm trying to get the average number of interactions on my metrics, but this
I am trying to get the average number of months difference between two dates:
I'm trying to get an average of sums using nested aggregate functions and grouping.
I am trying to get the average of the rating (found in the review
I'm trying to get the average of a ratio in SQL. My table looks
I'm trying to get the MODE average for a set of zipcodes (zip code
So i am trying get 2 div-containers which both should contain centered text (Both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.