Having issues with generating 1 to 300 numbers between 1 to 100 and placing each number in a String

醉酒当歌 提交于 2019-12-24 15:25:08

问题


I'm trying to accept a number in the range 1 to 300 from the user, then generate the amount of random numbers that was inputted. Then if the numbers generated are in the range 1 to 10, I add an asterisk to a String, then I do the same for 11 to 20 etc. all the way up to 100. Although I keep getting errors saying "; expected" in the results portion of my code. The output I'm trying to achieve is:

1 to 10 ****************

11 to 20 ******

21 to 30 ***************

31 to 40 ********************

41 to 50

51 to 60 ***********

61 to 70 ****************

71 to 80 ****

81 to 90 *******

91 to 100 **

The asterisks above may not add up to 100, but I'm trying to get 100 asterisks displayed in the output, each one representing a random generated number in the range __ to __.

The code I have is below.

import java.util.*;
public class histogramAsterisks
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter a number between the range 1 to 300: ");
        int input = Integer.parseInt(in.nextLine());
        int aNumber;
        String 1to10 = "", 11to20 = "", 21to30= "", 31to40 = "", 41to50 = ""; 51to50 = ""; 61to70 = ""; 71to80 = ""; 81to90 = ""; 91to100 = "", results = "";
        if(input < 1 || input > 300 || input == null)
            results = "Please enter a valid input in the range 1 to 300.";
        else
        {
            for(int i = 0; i < input; i++)
            {
                aNumber = (int) (Math.random() * 100 + 1);
                if(aNumber <= 10)               1to10   += "*";
                else if(aNumber <= 20)          11to20  += "*";
                else if(aNumber <= 30)          21to30  += "*";
                else if(aNumber <= 40)          31to40  += "*";
                else if(aNumber <= 50)          41to50  += "*";
                else if(aNumber <= 60)          51to60  += "*";
                else if(aNumber <= 70)          61to70  += "*";
                else if(aNumber <= 80)          71to80  += "*";
                else if(aNumber <= 90)          81to90  += "*";
                else                            91to100 += "*";
            }
        }
        results  = "1 to 10\t\t\t" + 1to10;
        results += "\n11 to 20\t\t\t" + 11to20;
        results += "\n21 to 30\t\t\t" + 21to30;
        results += "\n31 to 40\t\t\t" + 31to40;
        results += "\n41 to 50\t\t\t" + 41to50;
        results += "\n51 to 60\t\t\t" + 51to60;
        results += "\n61 to 70\t\t\t" + 61to70;
        results += "\n71 to 80\t\t\t" + 71to80;
        results += "\n81 to 90\t\t\t" + 81to90;
        results += "\n91 to 100\t\t\t" + 91to100;
        System.out.println(results);
    }
}

回答1:


In your code for String declaration you have used semi colon (;) instead of coma (,) for separation of variable declaration. Change as below line. And also please change variable names.

String 1to10 = "", 11to20 = "", 21to30= "", 31to40 = "", 41to50 = "", 51to50 = "", 61to70 = "", 71to80 = "",81to90 = "", 91to100 = "", results = "";




回答2:


As described at https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html a variable name must:

A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_".

You are trying to create a bunch of variables starting with numbers, for instance 1to10, which are not valid.


You could probably solve this easier by using arrays instead. So, create an array with the "buckets" of results you are expecting, and place the result in that instead. I have edited your code and inserted it below to demonstrate. At the moment you get a number instead of asterisks, but you can easily change that once you understand the code.

import java.util.*;
public class histogramAsterisks
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter a number between the range 1 to 300: ");
        int input = Integer.parseInt(in.nextLine());
        double aNumber;
        int[] results = new int[10];
        for ( int i = 0; i < 10; ++i) { results[i] = 0; }
        if (input < 1 || input > 300)
        {
            System.out.println("Please enter a valid input in the range 1 to 300.");
            return;
        }
        for(int i = 0; i < input; i++)
        {
           aNumber = (Math.random() * 100 + 1);
           int bucket = (int)(aNumber / 10);
           ++results[bucket];
        }

        for (int i = 0 ; i < 10; ++i)
        {
            System.out.println(i + "1 to " + i + "0\t\t\t" + results[i]);
        }
    }
}

With an input of 10, this prints:

Please enter a number between the range 1 to 300: 10
01 to 00          2
11 to 10          0
21 to 20          1
31 to 30          2
41 to 40          2
51 to 50          0
61 to 60          2
71 to 70          0
81 to 80          0
91 to 90          1



回答3:


The "; expected" is caused by the line where you declare the "XtoY" strings:

String 1to10 = "", 11to20 = "", 21to30= "", 31to40 = "", 41to50 = ""; 51to50 = ""; 61to70 = ""; 71to80 = ""; 81to90 = ""; 91to100 = "", results = "";

Two problems ocurr:

  • As stated in other answers, a variable's name may not begin with a digit(0-9). You can begin the name with an underscore, though discouraged, or change it to something else.
  • Always use a comma to separate multiple variables declarations. In the line above, some of the variables have been separated by a semi-colon(;)

Your program has a lot of room to improvement. I sugest you also take a look in the following links:

Scanner.nextInt() - Method for reading integers from a stream.

StringBuilder - A class better suited for multiple string concatenations.

Hope it helps ;)



来源:https://stackoverflow.com/questions/26908311/having-issues-with-generating-1-to-300-numbers-between-1-to-100-and-placing-each

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!