如何限制'for'循环添加的内容? [英] How do I restrict what a 'for' loop adds?

查看:94
本文介绍了如何限制'for'循环添加的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个创建直方图的java程序。只要有人输入某组数字中的整数(即1-10; 11-20等),就会打印星号。我做到了。没问题。现在另一个项目要我修改我最初做的事情,并在5次输入一个数字时加上星号添加一次。它不应该打印任何剩余的。 (即有人输入1-10范围内的6个数字......星号应该只打印一次。)我遇到了很多麻烦有了这个...这是原始代码。 (我是编码的新手,所以这个解决方案可能不是最有效的。)我尝试使用mod(%),这几乎可以工作,但不完全。



I'm working on a java program that creates a histogram. An asterisk is printed whenever someone inputs an integer in a certain set of numbers (i.e. 1-10; 11-20, etc.). I made that. No problem. Now another project wants me to revise what I originally did and make an asterisk add once when a number is inputted from a range 5 time. It should not print any of the left overs. (i.e. Someone inputs 6 numbers from a range of 1-10...An asterisk should be printed only once.) I'm having a lot of trouble with this...Here's the original code. (I'm new to coding so this solution might not be the most efficient.) I tried using mod (%), which almost worked, but not quite.

import java.util.*;
public class sixFour
{
  public static void main(String[] args)
  {
{
  Scanner scan = new Scanner (System.in);
  int count1 = -1;
  int count2 = -1;
  int count3 = -1;
  int count4 = -1;
  int count5 = -1;
  int count6 = -1;
  int count7 = -1;
  int count8 = -1;
  int count9 = -1;
  int count10 = -1;
  
  int[] array = new int [10];
  for (int count = 0; count < array.length; count++)
  {
    System.out.println("Enter an int from 1-100.");
    int input = scan.nextInt();
    if (input > 0 && input <= 100)
      
    {
      array[count] = input;     
      if (array[count] >=1 && array[count] <= 10)
      { 
       count1 ++;
      }
      else if (array[count] >= 11 && array[count] <= 20)
      {
        count2 ++;
      }
      else if (array[count] >= 21 && array[count] <= 30)
      {
        count3++;
      }
      else if (array[count] >= 31 && array[count] <= 40)
      {
        count4 ++;
      }
      else if (array[count] >= 41 && array[count] <= 50)
      {
        count5++;
      }
      else if (array[count] >= 51 && array[count] <= 60)
      {
        count6 ++;
      }
      else if (array[count] >= 61 && array[count] <= 70)
      {
        count7++;
      }
      else if (array[count] >= 71 && array[count] <= 80)
      {
        count8++;
      }
       else if (array[count] >= 81 && array[count] <= 90)
      {
        count9++;
      }
       else if (array[count] >= 91 && array[count] <= 100)
      {
        count10++;
       }
    }
    else
    {
      System.out.println("NO.");
    }
  }
  System.out.print("1-10:  ");
    for (int final1 = 0; final1 <= count1; final1 ++)
    {
      System.out.print("*");
    }
  
  System.out.println();
  
  System.out.print("11-20: ");
  for (int final2 = 0; final2 <= count2; final2 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("21-30: ");
  for (int final3 = 0; final3 <= count3; final3 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("31-40: ");
  for (int final4 = 0; final4 <= count4; final4 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("41-50: ");
  for (int final5 = 0; final5 <= count5; final5 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("51-60: ");
  for (int final6 =  0; final6 <= count6; final6 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("61-70: ");
  for (int final7 = 0; final7 <= count7; final7 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
 
  System.out.print("71-80: ");
  for (int final8 = 0; final8 <= count8; final8 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("81-90: ");
  for (int final9 = 0; final9 <= count9; final9 ++)
  {
    System.out.print("*");
  }
  
  System.out.println();
  
  System.out.print("91-100: ");
   for (int final10 = 0; final10 <= count10; final10 ++)
  {
    System.out.print("*");
  }
  
  }
}
}

推荐答案

你把mod(%)放在哪里了?



我认为把它们放在你的for循环中就可以了:



where did you put the mod(%)?

I would think putting them in your for-loops would do it:

for (int final1 = 0; final1 < (count1%5); final1 ++)
{
  System.out.print("*");
}





取决于你是否想要低于或高于5的任何*,你需要<或< =



depending on if you want a "*" for anything below or above 5, you would need either < or <=


这篇关于如何限制'for'循环添加的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆