Outputting/printing 循环中每行 6 个数字
Outputting/printing 6 numbers per line from a loop
我正在尝试编写一个非常基本的代码,它需要一个数字(通过手动编辑代码完成(即不允许扫描仪),然后打印所有它的倍数,直到某个最大值(也可以手动估算)在代码中)。我有用于循环,值等的代码 - 只是我们必须包括两种打印它的方法。一种方法很简单,每个数字都在一个新行上。另一种方法很多更难 - 每行有 6 个数字,正确的,由几个空格分隔。我知道 %(x/y/z/a/b/c)f
将打印 strings/ints/doubles/etc。根据 x/y/z/a/b/c
以间距右对齐,但我不知道不知道如何在 6 个数字后自动换行。
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("");
}
}
这是我目前的代码。
这是%
运算符的一个相对简单的使用:
for (increment = incvalue; increment < max + incvalue; increment += incvalue) {
System.out.print(increment);
if (increment % (incvalue * 6) == 0)
System.out.println();
}
我正在尝试编写一个非常基本的代码,它需要一个数字(通过手动编辑代码完成(即不允许扫描仪),然后打印所有它的倍数,直到某个最大值(也可以手动估算)在代码中)。我有用于循环,值等的代码 - 只是我们必须包括两种打印它的方法。一种方法很简单,每个数字都在一个新行上。另一种方法很多更难 - 每行有 6 个数字,正确的,由几个空格分隔。我知道 %(x/y/z/a/b/c)f
将打印 strings/ints/doubles/etc。根据 x/y/z/a/b/c
以间距右对齐,但我不知道不知道如何在 6 个数字后自动换行。
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("");
}
}
这是我目前的代码。
这是%
运算符的一个相对简单的使用:
for (increment = incvalue; increment < max + incvalue; increment += incvalue) {
System.out.print(increment);
if (increment % (incvalue * 6) == 0)
System.out.println();
}