简体   繁体   English

如何将一个循环中的增量更改为一个以上?

[英]How to change the increment in a loop to more than one?

For one of my programming projects, I was told to make one of these charts: 对于我的一个编程项目,有人告诉我制作以下图表之一:

1 - 10  | *****
11 - 20 | **
21 - 30 | *******************
31 - 40 |
41 - 50 | ***
51 - 60 | ********
61 - 70 | **
71 - 80 | *****
81 - 90 | *******
91 - 100| *********

This is an array that when the user inputs numbers in a particular range it adds a * for each value entered in that range. 这是一个数组,当用户在特定范围内输入数字时,它将为该范围内输入的每个值添加一个* I have already created a program that creates one of these charts. 我已经创建了创建这些图表之一的程序。 My next programming project tells me to only add an * for every five values entered in a particular range of numbers. 我的下一个编程项目告诉我,在特定数字范围内输入的每五个值仅添加一个* If less than 5 values are entered then no * 's will be printed. 如果输入的值小于5,则不会打印* I don't know how I should approach this problem. 我不知道该如何解决这个问题。

Here is the code for the chart: 这是图表的代码:

import java.util.Scanner;

public class SixFive
{
    public static void main(String[] args)
    {
        int[] counts = new int[10];

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter integers between 1-100");

        while(scan.hasNextInt()) 
        {
            int num = scan.nextInt();
            if (num < 1 || num > 100) 
                break;
            counts[(num - 1) / 10]++;
        }

        scan.close();

        for (int i = 0; i < 10; i++) 
        {
            System.out.format("%2d -%3d ", i*10+1, i*10+10);
            for (int j = 0; j < counts[i]; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

In your inner loop, replace the condition j < counts[i] by j < counts[i] / 5 like this: 在您的内部循环中,将条件j < counts[i]替换为j < counts[i] / 5如下所示:

for (int i = 0; i < 10; i++) {
    System.out.format("%2d -%3d | ", i * 10 + 1, i * 10 + 10);
    for (int j = 0; j < counts[i] / 5; j++)
        System.out.print("*");
    System.out.println();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有一个以上的帖子增量? - Is there a post increment by more than one? 如何在一个循环中分配多个同一字符串? - How do I assign more than one of the same String in a loop? 当传感器事件发生变化时,如何多次增加变量? - How can I increment the variable more than one time when sensor event changed? 如何在循环中的一个字符串中存储多个字符串变量输入(JAVA) - how to store more than string variable input in one string from loop (JAVA) 如何使用循环来允许用户选择多个项目? - How do I use a loop to allow the user to select more than one item? 如果同一XML节点有多个层次,如何遍历JAXB类 - How to Loop through JAXB classes, if there are more than one level of same XML node 如何在循环中输入多于1个整数 - How to enter more than 1 integer in a loop 我将如何更改此代码以允许在客户端和服务器之间发送多个用户输入消息 - How would i change this code to allow more than one user input message to be sent between the client and server 为什么我不能使用多个Java递增或递减运算符? - Why do I can't use more than one Java increment or decrement operators? 为mysql中的多个列提供auto_increment的默认值 - Give default value for auto_increment for more than one column in mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM