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 8739403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:55:55+00:00 2026-06-13T10:55:55+00:00

public static void main (String [] args) { // declare variables, capture input String

  • 0
public static void main (String [] args)
{
    // declare variables, capture input

    String input, name = JOptionPane.showInputDialog("Please " +
                          "enter your first and last name.");
    double testScore1, testScore2, testScore3, average;

    // capture input, cast, and validate input

    input = JOptionPane.showInputDialog("What is the score " +
                                        "of your first test?");
    testScore1 = Double.parseDouble(input);

    while (testScore1 < 1 || testScore1 > 100)
    {
        input = JOptionPane.showInputDialog("This test score is not " +
                "between 1 and 100. \nPlease enter a test score in " +
                "this range:");

        testScore1 = Double.parseDouble(input); 
    }

    input = JOptionPane.showInputDialog("What is the score " +
                                        "of your second test?");
    testScore2 = Double.parseDouble(input);

    while (testScore2 < 1 || testScore2 > 100)
    {
        input = JOptionPane.showInputDialog("This test score is not " +
                "between 1 and 100. \nPlease enter a test score in " +
                "this range:");

        testScore2 = Double.parseDouble(input); 
    }

    input = JOptionPane.showInputDialog("What is the score " +
                                        "of your third test?");
    testScore3 = Double.parseDouble(input);

    while (testScore3 < 1 || testScore3 > 100)
    {
        input = JOptionPane.showInputDialog("This test score is not " +
                "between 1 and 100. \nPlease enter a test score in " +
                "this range:");

        testScore3 = Double.parseDouble(input);
    }

    // calculate average and display output

    average = (testScore1 + testScore2 + testScore3)/3; 

    JOptionPane.showMessageDialog(null, name + ", your average score is: " + average);

}

First off, I’m a beginner programmer. My terminology and jargon are quite lacking, so bear with me.

I’m writing a program to capture 3 test scores then validate them using a while loop (must be within the 1-100 range). The test scores are then averaged and the output displays the average. Pretty simple stuff.

I’m wanting to find a way, if possible, to capture the number of test scores, then from there, capture each actual score. For example, the program asks “How many tests are being computed for average?”, then take that number and have it be the same amount of times the program prompts, “Please enter test score (1):” or something along those lines. So for further clarity, if the user typed 4 for number of tests, then the prompt for inputting the score would show up 4 times.

I feel the above code is redundant by using a while loop for each score and at that, limited because the program is only meant for 3 scores. Any help is much appreciated and feel free to critique anything else in the code.

  • 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-13T10:55:56+00:00Added an answer on June 13, 2026 at 10:55 am

    Yes you can.

    What you need is a nested loop. In pseudo code:

    while(condition)
    {
       int numberOfInput = getInput() ; //get the input from the user
    
       for(int i =0 ; i < numberOfInput; i++) //iterate for the amount of prompts required
         prompt() ; //get input
    
    }
    
    
    
    function prompt
          while (testScore1 < 1 || testScore1 > 100)
          {
            input = JOptionPane.showInputDialog("This test score is not " +
                    "between 1 and 100. \nPlease enter a test score in " +
                    "this range:");
    
            testScore1 = Double.parseDouble(input); 
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static void main(String[] args) { int i = 0; while(i==0){ System.out.println(Possibilities: R, T,
public static void main(String[] args) { // TODO Auto-generated method stub BufferedReader br1 =
public static void main(String[] args) throws IOException, URISyntaxException,TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); String
public class Main { public static void main(String[] args){ Class2 class2Object = new Class2();
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class Foo { public static void main(String[] args) { float f; System.out.println(f); }
public class b { public static void main(String[] args) { byte b = 1;
public class Sample { public static void main(String[] args) { int year = 2012;
public class Loopname{ public static void main (String [] args){ String files []= new
public class I { public static void main(String args[]) { int i=0,j=1,k=2; i=i++ -

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.