在1到100之间生成1到300个数字并将每个数字放在字符串中时遇到问题 [英] Having issues with generating 1 to 300 numbers between 1 to 100 and placing each number in a String
问题描述
我正在尝试接受来自用户的1到300范围内的数字,然后生成输入的随机数量。然后,如果生成的数字在1到10的范围内,我将一个星号添加到一个字符串,然后我做同样的11到20等一直到100.虽然我不断收到错误说;预期in我的代码的结果部分。我想要实现的输出是:
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到10 ****************
1 to 10 ****************
11至20 ******
11 to 20 ******
21至30 ************** *
21 to 30 ***************
31至40 ********************
31 to 40 ********************
41至50
51至60 ***********
51 to 60 ***********
61至70 ****************
61 to 70 ****************
71至80 ****
71 to 80 ****
81至90 *******
81 to 90 *******
91至100 **
91 to 100 **
上面的星号可能不等于100,但我试图在输出中显示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 __.
我的代码如下。
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);
}
}
推荐答案
In你的String声明代码你使用了半冒号(;)而不是昏迷(,)来分离变量声明。改为以下行。还请更改变量名称。
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 =,结果=;
String 1to10 = "", 11to20 = "", 21to30= "", 31to40 = "", 41to50 = "", 51to50 = "", 61to70 = "", 71to80 = "",81to90 = "", 91to100 = "", results = "";
这篇关于在1到100之间生成1到300个数字并将每个数字放在字符串中时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!