【问题标题】:How do I print a set amount of characters corresponding to the value of a variable?如何打印与变量值相对应的一定数量的字符?
【发布时间】:2013-12-17 08:47:06
【问题描述】:

我正在尝试制作一个系统,其中有人输入 0-100 之间的数字,然后程序会将数字分配到设定的边界,基本上是一个成绩存储系统。 我将向您展示我的代码,然后详细说明。

public static void main(String[] args) {
//Boundary0/30/40/70 indicates which band the counter is for. e.g. 0-29, 30-39 etc
int Boundary0 = 0;
int Boundary30 = 0;
int Boundary40 = 0;
int Boundary70 = 0;
int Grade;
int count;
count = 0;
Scanner in = new Scanner(System.in);
//Read in first number
System.out.print("Enter an Integer");
 Grade = in.nextInt();



 while (Grade < 100){    
   //To count number of students
   count++;
   //To allocate each grade to corresponding tier
   if(Grade >= 0 && Grade <= 29){
          Boundary0++;
   }
   if(Grade >= 30 && Grade <= 39){
          Boundary30++;
   }
   if(Grade >= 40 && Grade <= 69){
          Boundary40++;
   }
   if(Grade >= 70 && Grade <= 100){
          Boundary70++;
   }
   Grade = in.nextInt();      
 }

 //To print each boundary seperately with the number of marks in each tier and overall total

    System.out.println("0-29:" + " " + Boundary0 );       
    System.out.println("30-39:" + " " + Boundary30);               
    System.out.println("40-69:" + " "+ Boundary40);        
    System.out.println("70-100:" + " " +Boundary70);        
    System.out.println("Amount of Students: " + count);
}
}

当用户输入一个数字时,程序会将相应的边界变量加1

然后当用户输入大于 100 的数字时,程序停止并打印

层和每一层旁边,告诉用户每一层有多少值。

那么我想做的是,在代码末尾,sout 命令所在的位置

我不想从字面上说每个部分有多少个数字,我想代表

以 *s 为例

0-29: *****
30-39: ****
40-69: ********
70-100: *****

对不起,如果我没有完全清楚,我认为这可能只是对自己缺乏了解......

谢谢

【问题讨论】:

  • * 是什么意思?
  • 那么如果30-39有2个数字,值是多少?
  • 从字面上看,属于该类别的每个值只有一个 *,所以假设为一个层输入了 5 个值,我想要该类别的 5 颗星
  • 如果 30-30 中有两个数字 Boundary30 等于 2,我希望在 30-39 之前打印两颗星:最后
  • 问题是什么?你写的代码有什么问题?

标签: java loops counting


【解决方案1】:

创建一个可重用的方法,为您打印* -

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

然后这样称呼它-

System.out.println("0-29:" + " " + printStars(Boundary0));
System.out.println("30-39:" + " " + printStars(Boundary30));               
System.out.println("40-69:" + " "+ printStars(Boundary40));        
System.out.println("70-100:" + " " +printStars(Boundary70));

【讨论】:

  • 我会把它放在哪里?抱歉,我刚刚开始编码
  • 只需将该方法放在您的主要方法旁边。
  • 太棒了:D 我已经使用可重复使用的方法让它工作了......非常有帮助,感谢其他所有人向我展示示例以帮助我理解。
【解决方案2】:

只需添加额外的 for 循环:

System.out.print("0-29: ");
for(int i = 0; i < Boundary0; i++){
    System.out.print("*");
}
System.out.println("");

【讨论】:

  • 非常感谢!很有帮助
  • 没问题 :) 还要检查 Mohammad Adil 的答案,它向您展示了如何重用这个循环。
【解决方案3】:

我认为您已经通过代码实现了您想要的。 你只需要打印那些:

在您的代码末尾,添加以下部分:

System.out.print("0-29: ");
for(int i=0;i<Boundary0;i++)
{
 System.out.print("*");
}
System.out.println("");

System.out.print("30-39: ");
for(int i=0;i<Boundary30;i++)
{
 System.out.print("*");
}
System.out.println("");

System.out.print("40-69: ");
for(int i=0;i<Boundary40;i++)
{
 System.out.print("*");
}
System.out.println("");

System.out.print("70-100: ");
for(int i=0;i<Boundary70;i++)
{
 System.out.print("*");
}
System.out.println("");

这肯定会解决目的,并与您编写的代码同步。

但我个人的建议是使用HashMap&lt;String,Integer&gt;; 来实现它。

希望对你有帮助

编辑!!!

这似乎很罗嗦。尝试在 Mohd 的答案中打印“*”的可重复使用功能。阿迪尔。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    相关资源
    最近更新 更多