首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从循环中每行输出/打印6个数字

从循环中每行输出/打印6个数字
EN

Stack Overflow用户
提问于 2016-09-24 05:40:14
回答 1查看 192关注 0票数 0

我正在尝试写一个非常基本的代码,它需要一个数字(通过手动编辑代码来完成(即不允许扫描仪),然后打印所有它的倍数直到某个最大值(也可以在代码中手动计算的东西)。我有工作的循环,值等代码-它只是我们必须包括2种方式打印它。一种方法很简单,每个数字都在一个新的行上。另一种方法要难得多--每行有6个数字,用几个空格隔开。我知道%(x/y/z/a/b/c)f将打印字符串/int/doubles/等。基于x/y/z/a/b/c的空格右对齐,但我不知道如何在6个数字后自动开始一个新行。

代码语言:javascript
复制
import java.util.*;
public class IncrementMax
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        int maxvalue = 200; // these top 2 values have to be adjusted to suit the program to your needs
        int incvalue = 5;
        int increment = incvalue; 
        int max = 200;

        System.out.println("I will print the multiples of " + increment + ", up to " + max + ". Do you want each number on a different line (y/n)?");
        String yesno = sc.next();

        if (yesno.equalsIgnoreCase("y"))
        {
            for(increment=incvalue; increment<(max+incvalue); increment=increment+incvalue)
                System.out.println(increment);
        }
        else if (yesno.equalsIgnoreCase("n"))
        {
            for (increment=incvalue; increment<(max+incvalue); increment=increment+incvalue)
                System.out.print(increment + ". ");
        }
        else
            System.out.print("");
    }
}

这是我到目前为止拥有的代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-24 05:47:34

这是%运算符的一个相对简单的用法:

代码语言:javascript
复制
for (increment = incvalue; increment < max + incvalue; increment += incvalue) {
    System.out.print(increment);
    if (increment % (incvalue * 6) == 0)
        System.out.println();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39669943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档