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

  • Home
  • SEARCH
  • 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 8693061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:27:27+00:00 2026-06-13T00:27:27+00:00

So I have to create three random numbers using the Math.random(); method then use

  • 0

So I have to create three random numbers using the Math.random(); method then use a for loop to output each number by itself and then using a concatenate to make them into a string. It’s for my assignment. I’ve done everything else. I could do it a different way but it requires a for loop. I can’t use the Random class. I have not learned it in my course, therefore it can’t be applied.

public class Lottery
{
    public static void main(String[] args)
    {
        //declare and initialized variables and objects
        Scanner input = new Scanner(System.in);



        //Identify the repeated steps and use a for loop structure


        for(int i=0; i < 3; i++) 
        {
            double lotto = Math.random(); 
            int lotteryNumberDigit = (int)(lotto*10);

           String lotteryNumberString = Integer.toString(lotteryNumberDigit);

        }



            String firstNumber = lotteryNumberString.substring(0,0);
            String secondNumber = lotteryNumberString.substring(1,1);
            String thirdNumber = lotteryNumberString.substring(2,2);

            String firstTwoWinner = firstNumber + secondNumber;
            String lastTwoWinner = secondNumber + thirdNumber;
            String allNumbersWinner = firstNumber + secondNumber + thirdNumber;

            System.out.println("Please enter your three numbers (e.g. 123): ");

            String userInput = input.next();

             if(userInput.substring(0,2).equals(firstTwoWinner))

        {
            System.out.println("Winner: " + allNumbersWinner );

            System.out.println("Congratulations, the front pair matched.");
        }

        else if (userInput.substring(1,3).equals(lastTwoWinner))
        {
             System.out.println("Winner: " + allNumbersWinner );

             System.out.println("Congratulations, the end pair matched.");
        }

        else if (userInput.equals(allNumbersWinner))
        {
             System.out.println("Winner: " + allNumbersWinner );

             System.out.println("Congratulations, both pairs matched.");
        }

        else
        {
             System.out.println("Winner: " + allNumbersWinner );

             System.out.println("Sorry, no matches. You only had one chance out of 100 to win anyway.");
        }



    }
}

I had to take out the arrays because my teacher said it was not expectable until the next chapter.

  • 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-13T00:27:30+00:00Added an answer on June 13, 2026 at 12:27 am

    You can simply write below to print the 3 digit number.

        Random random = new Random();
        for(int i=0; i < 3; i++){
            int randomNumber = random.nextInt(10);
            System.out.print(randomNumber);
        }
            System.out.println();
    

    Note: By using print method, you will print the three digits next to each other.

    Alternatively you can do below:

        String newConcatNumString = "";
        Random random = new Random();
        for(int i=0; i < 3; i++){
           newConcatNumString  = newConcatNumString +random.nextInt(10);
        }
        System.out.println(newConcatNumString);
    

    If you want to get the number from string, you can simply do:

        int concatenatedValue = Integer.parseInt(newConcatNumString);
    

    EDIT:
    If you don’t want to use Random class, do the below(updating first example only. Same can be applied into second as well):

        for(int i=0; i < 3; i++){
            int lotteryNumberDigit = (int)(Math.random()*10);
            System.out.print(lotteryNumberDigit);
        }
        System.out.println();
    

    Edit: for you next problem, define an int[] in the beginning and store the values in the array for later use of comparison. Updated code snippet as below:

        int[] generatedNumbers = new int[3];
        for(int i=0; i < 3; i++){
            int lotteryNumberDigit = (int)(Math.random()*10);
                        generatedNumbers[i] = lotteryNumberDigit;
            System.out.print(lotteryNumberDigit);
        }
        System.out.println();
    

    After the above loop, generatedNumbers will contain all three digits as separate entry in the array. Your comparison code can be as below:

      System.out.println("Please enter your three numbers, one digit at a time): ");
      for(int i=0; i<3; i++){
        int userInput = input.next();
        if(userInput == generatedNumbers[i]){
            System.out.println("Your digit "+i+" is correct");
        }else{
            System.out.println("Your digit "+i+" is incorrect");
        }
      }
    

    EDIT:
    Please do the comparison as below:

        if (userInput.equals(allNumbersWinner)) {
             System.out.println("Winner: " + allNumbersWinner );
             System.out.println("Congratulations, both pairs matched.");
        }else if(userInput.substring(0,2).equals(firstTwoWinner )) {
            System.out.println("Winner: " + allNumbersWinner);
            System.out.println("Congratulations, the front pair matched.");
        }else if (userInput.substring(1,3).equals(lastTwoWinner)) {
             System.out.println("Winner: " + allNumbersWinner );
             System.out.println("Congratulations, the end pair matched.");
        }else {
             System.out.println("Winner: " + allNumbersWinner );
             System.out.println("Sorry, no matches. You only had one chance out of 100 to win anyway.");
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created the following method so as to create unique random numbers .
I have this object I'm loading with THREE.objLoader and then create a mesh with
Is there a difference between generating multiple numbers using a single random number generator
I have three problems: I want to create resizable shapes with box bounding... I
I have three models that are coming together to create one view model and
I have three classes that all have a static function called 'create'. I would
I have the following three arrays and need to create a new two-dimensional array
I have three tables. The designs were like student table create table student (studID
I want to create tree structure using web service. I have used bottom up
I need to create a function that will generate 2 random numbers between x

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.