如何在java中将数字转换为符号?例如,不是2到**,或3到***等。

如何在java中将数字转换为符号?例如,不是2到**,或3到***等。,java,arrays,Java,Arrays,可能重复: 如何将数字转换为符号?我的作业的最后一部分是这样的:柱状图应该根据该值的滚动次数显示2-12之间的条形图。如下所示: 目前我的输出如下: public static void main(String[] args) { // TODO code application logic here System.out.print("Please enter how many times you want to roll two dice?"); Scann

可能重复:

如何将数字转换为符号?我的作业的最后一部分是这样的:柱状图应该根据该值的滚动次数显示2-12之间的条形图。如下所示: 目前我的输出如下:

    public static void main(String[] args) {
    // TODO code application logic here
    System.out.print("Please enter how many times you want to roll two dice?");
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int [] rolls = new int[n];

    Random r1 = new Random();
    Random r2 = new Random();

    int dice1;
    int dice2;

    int two = 0;
    int three = 0;
    int four = 0;
    int five = 0;
     int six = 0;
    int seven = 0;
    int eight = 0;
    int nine = 0;
     int ten = 0;
    int eleven = 0;
    int twelve = 0;

    for (int roll=0; roll < rolls.length; roll++)
    {
         dice2 = r2.nextInt(6)+1;
         dice1 = r1.nextInt(6)+1;
         System.out.println(roll + " The first dice rolled a " + dice1 + " the second dice rolled a " + dice2);

         int sum;
         sum = dice1 + dice2;

         if (sum == 2)
             two++;
         if (sum == 3)
             three++;
         if (sum == 4)
             four++;
         if (sum == 5)
             five++;
         if (sum == 6)
             six++;
         if (sum == 7)
             seven++;
         if (sum == 8)
             eight++;
         if (sum == 9)
             nine++;
         if (sum == 10)
             ten++;
         if (sum == 11)
             eleven++;
         if (sum == 12)
             twelve++;

    }
    System.out.println("Histogram of rolls:" );  
    System.out.println("2 occurred " + two + " times");
    System.out.println("3 occurred " + three + " times");
    System.out.println("4 occurred " + four + " times");
    System.out.println("5 occurred " + five + " times");
    System.out.println("6 occurred " + six + " times");
    System.out.println("7 occurred " + seven + " times");
    System.out.println("8 occurred " + eight + " times");
    System.out.println("9 occurred " + nine + " times");
    System.out.println("10 occurred " + ten + " times");
    System.out.println("11 occurred " + eleven + " times");
    System.out.println("12 occurred " + twelve + " times");




}
publicstaticvoidmain(字符串[]args){
//此处的TODO代码应用程序逻辑
System.out.print(“请输入您想要掷两个骰子的次数?”);
扫描仪sc=新的扫描仪(System.in);
int n=sc.nextInt();
int[]滚动=新的int[n];
Random r1=新的Random();
随机r2=新随机();
int-1;
int-2;
int 2=0;
int三=0;
int-four=0;
int五=0;
int-six=0;
int七=0;
int八=0;
int九=0;
int-ten=0;
int-11=0;
int十二=0;
对于(int roll=0;roll

}

创建一个名为
toStars

public String toStars(int number) {
    StringBuilder temp = new StringBuilder();
    for(int i=0;i<number;i++) {
        temp.append("*");
    }
    return temp.toString();
}
int n=10;
对于(int i=0;i

该代码将打印“*”10次

我想指出的是,通过对所有计数使用一个数组,而不是对每个计数使用一组变量,可以大大简化代码。很明显,你们确实知道数组……我知道@StephenC,但当我变成数组时,我不知道如何从2数到12。事情总是说1发生了0次。也许你可以给我举例说明。谢谢我鼓励使用
StringBuilder
而不是字符串连接,但是这个想法是正确的;)@我本来打算用它,但记不起确切的语法。现在就开始做。这不是一个大问题,在这种情况下不会有太大的区别,但鼓励良好的实践总是很好的,+1;)我试过了,但我不明白星星旁边的那个(整数)是什么。正如你所看到的,我是第一学期编程的noob。谢谢。@Somekittenst不是
toStars
,它是一个函数。
int
告诉编译器
toStars
应该采用一个整数参数。
System.out.println("2 : " + toStars(two));
int n = 10;

for(int i = 0; i < n; i++)
{
   System.out.print("*");
}