Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何打印与变量值相对应的一定数量的字符?_Java_Loops_Counting - Fatal编程技术网

Java 如何打印与变量值相对应的一定数量的字符?

Java 如何打印与变量值相对应的一定数量的字符?,java,loops,counting,Java,Loops,Counting,我试图建立一个系统,有人输入一个介于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 Bou

我试图建立一个系统,有人输入一个介于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);
}
}
对不起,如果我不是很清楚,我想那可能是因为我不了解自己


谢谢

只需为循环添加其他内容:

System.out.print("0-29: ");
for(int i = 0; i < Boundary0; i++){
    System.out.print("*");
}
System.out.println("");
System.out.print(“0-29:”);
对于(int i=0;i
只需为循环添加额外的:

System.out.print("0-29: ");
for(int i = 0; i < Boundary0; i++){
    System.out.print("*");
}
System.out.println("");
System.out.print(“0-29:”);
对于(int i=0;i
创建一个可重复使用的方法,为您打印
*
-

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

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

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

我认为您已经通过代码实现了所需的功能。 您只需打印以下内容:

在代码末尾,添加以下内容:

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("");
System.out.print(“0-29:”);

对于(inti=0;i,我认为您已经通过代码实现了所需的功能。 您只需打印以下内容:

在代码末尾,添加以下内容:

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("");
System.out.print(“0-29:”);

对于(int i=0;i什么是a
*
?那么如果30-39中有两个数字,那么该值是多少?对于属于该类别的每个值,字面上只有a*,因此假设为一个层输入了5个值,我希望该类别有5颗星如果30-30边界中有两个数字30等于2,我希望两颗星按30-39:at打印结尾问题是什么?你写的代码有什么问题?a
*
是什么意思?那么如果30-39中有2个数字,那么值会是什么?对于属于该类别的每个值,字面上只是a*,因此假设为一个级别输入了5个值,我希望该类别有5颗星如果30-3中有两个数字0 Boundary30将等于2,我希望在30-39之前打印两颗星:在末尾问题是什么?您对所编写的代码有什么问题?非常感谢!非常有用,没有问题:)但还要检查Mohammad Adil的答案,该答案向您展示了如何重用此循环。非常感谢!非常有用,没有问题:)但也要检查Mohammad Adil的答案,它向你展示了如何重用这个循环。我应该把它放在哪里?对不起,我刚刚开始编写代码,只是把这个方法放在你的主方法旁边。真棒:D我已经用可重用的方法工作了…非常好的帮助,感谢所有其他人给我展示示例来帮助我理解。你会在哪里我把它放在哪里了?对不起,我刚刚开始编写,把这个方法放在你的主方法旁边。真棒:D我已经用可重用的方法工作了…非常好的帮助,谢谢大家给我展示了一些例子来帮助我理解。