Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java help with question

  • 17-11-2012 3:19pm
    #1
    Registered Users, Registered Users 2 Posts: 255 ✭✭


    Hi, I need help with a question.

    The question asks me to ask the user for a number eg 10, then it wants me to display 10 random number in the range of 1 to 100, then it wants me to displau an answer like this.

    1 to 10 ***(3)
    11 to 20 ****(4)

    I can do everything except display the "*" the number of time and number appears within the range.


Comments

  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    String stars;

    for(int i = 0; i < numStars; i++)
    {
    stars += "*";
    }


  • Registered Users, Registered Users 2 Posts: 255 ✭✭SellingJuan


    String stars;

    for(int i = 0; i < numStars; i++)
    {
    stars += "*";
    }
    Sorry still dont have it.. take a look at this please.




    {
    public static void main(String [] args)
    {
    int num=0,input=0, aNumber=0,counter=0, count=0,i;
    String result = " ",disp=" ";;
    int [] array = new int[300];
    String stars ="";
    input = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter a number between 1 and 300"));

    if(input < 1 && input >300)
    result = "Error incorrect entry";

    else
    for(i = 0; i < input; i++)
    {
    aNumber = (int) ((Math.random()*100)+1);
    array = aNumber;
    //result += aNumber + ", ";
    if (aNumber >=1 && aNumber <= 10)
    //count++ ;
    //for(i =1 ; i <=10; i++)

    //disp += "*";

    //result = "1to 10" + disp ;



    for( i = 0; i < aNumber; i++)
    {
    stars += "*";
    }


    }







    JOptionPane.showMessageDialog(null,stars);
    }
    }


  • Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭BrokenArrows


    [B]if (aNumber >=1 && aNumber <= 10)[/B]
        for([B] i = 0; i < aNumber; i++)[/B]
        {
            stars += "*";
        } 
    

    You are not outputting any *'s where the random number generated is greater than 10

    I dont think you need the 1st IF statement.

    Also use the CODE tags when posting code. It keeps the formatting and makes it easier to read.

    EDIT: Also you are reusing the variable i which you are using for a different loop earlier on in the code. Change this to be a different variable.


  • Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭BrokenArrows


    There are other problems in the code but im also not quite understanding what you are trying to accomplish.

    It maybe that you have not explained your issue but it may also be the fact that ive had quite a few cans!!


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    A very simple implementation...
    import java.util.*;
    
    class SellingJuan
    {
        
        public static void main(String [] args)
        {
            Scanner scan = new Scanner(System.in);
            
            int userNum = 0;
            
            Random rand = new Random(System.currentTimeMillis());
            
            final int MAX_RANGE = 100;
            
            String range1 = "", range2 = "", range3 = "", range4 = "", range5 = "", 
                range6 = "", range7 = "", range8 = "", range9 = "", range10 = "";
            
            do
            {
                System.out.println("Please enter a number between 1 and 100");
                userNum = Integer.parseInt(scan.nextLine());
                
                if(userNum < 1 || userNum > MAX_RANGE)
                {
                    System.out.println("Error: Number must be between 1 and 100");
                }
            }while(userNum < 1 || userNum > MAX_RANGE);
            
            int[] randomNumArray = new int[userNum];
            
            for(int i = 0; i < userNum; i++)
            {
                randomNumArray[i] = rand.nextInt(MAX_RANGE) + 1;
                System.out.println(randomNumArray[i]);
            }
            
            for(int i = 0; i < randomNumArray.length; i++)
            {
                if(randomNumArray[i] <= 10)
                {
                    range1 += "*";
                }
                
                if(randomNumArray[i] >= 11 && randomNumArray[i] <= 20)
                {
                    range2 += "*";
                }
                
                if(randomNumArray[i] >= 21 && randomNumArray[i] <= 30)
                {
                    range3 += "*";
                }
                
                if(randomNumArray[i] >= 31 && randomNumArray[i] <= 40)
                {
                    range4 += "*";
                }
                
                if(randomNumArray[i] >= 41 && randomNumArray[i] <= 50)
                {
                    range5 += "*";
                }
                
                if(randomNumArray[i] >= 51 && randomNumArray[i] <= 60)
                {
                    range6 += "*";
                }
                
                if(randomNumArray[i] >= 61 && randomNumArray[i] <= 70)
                {
                    range7 += "*";
                }
                
                if(randomNumArray[i] >= 71 && randomNumArray[i] <= 80)
                {
                    range8 += "*";
                }
                
                if(randomNumArray[i] >= 81 && randomNumArray[i] <= 90)
                {
                    range9 += "*";
                }
                
                if(randomNumArray[i] >= 91)
                {
                    range10 += "*";
                }
            }
            
            System.out.println("1 to 10 " + range1 + " (" + range1.length() + ")");
            System.out.println("11 to 20 " + range2 + " (" + range2.length() + ")");
            System.out.println("21 to 30 " + range3 + " (" + range3.length() + ")");
            System.out.println("31 to 40 " + range4 + " (" + range4.length() + ")");
            System.out.println("41 to 50 " + range5 + " (" + range5.length() + ")");
            System.out.println("51 to 60 " + range6 + " (" + range6.length() + ")");
            System.out.println("61 to 70 " + range7 + " (" + range7.length() + ")");
            System.out.println("71 to 80 " + range8 + " (" + range8.length() + ")");
            System.out.println("81 to 90 " + range9 + " (" + range9.length() + ")");
            System.out.println("91 to 100 " + range10 + " (" + range10.length() + ")");
            
        }
        
    }
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 27,370 ✭✭✭✭GreeBo


    Can you explain the problem again, maybe with a couple of examples of input and output and describe what the issue you are having is?


  • Registered Users, Registered Users 2 Posts: 255 ✭✭SellingJuan


    import java.lang.Math;
    import javax.swing.JOptionPane;
    public class CrazyStar
    {
    public static void main (String [] args)
    {
    int number, randomNum, numberOfStars;
    String result="";
    int [] numbers = new int [10];
    number = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter a number between 1 and 300:"));
    if ((number < 1) || (number >300))
    {
    result = "I said between 1 and 300 stupid!";
    }
    else
    {
    for (int counter = 0 ; counter < number; counter++)
    {
    randomNum = (int) ((Math.random () * 100) +1);
    numbers[((randomNum - 1 ) /10)]++;
    }
    for (int counter = 0 ; counter < numbers.length; counter++)
    {
    result += ((counter *10)+1) + " to " + ((counter +1)*10) +"\t\t";
    numberOfStars = numbers[counter];
    for (int index = 0 ; index < numberOfStars; index++)
    {
    result += "*";
    }
    result += "(" + numberOfStars + ")\n";
    }
    }
    JOptionPane.showMessageDialog(null,result);
    }
    }


    That is it solved.

    Im still having a few problems with the very last part, like if I had to do another similar question im not sure Id be able for it.


  • Registered Users, Registered Users 2 Posts: 27,370 ✭✭✭✭GreeBo


    I'm still not sure I have any idea what problem you are having.

    My input:
    1) clearly explain what issue you are having
    2) Use the CODE tags to make your code legible
    3) Do you know what happens if I enter "yellow" when prompted by your app?
    4) Your code has no comments
    5) Why do you add 1 to your random number and then take one away after
    6) Why do you multiply your random number by 100 and then divide it by 10?
    7) What is the intent behind "numbers[((randomNum - 1) / 10)]++;"
    Is the purpose of this code to signify how many times the random number falls into one of 10 buckets and then indicate it with a *?


Advertisement