我正在尝试编写一个非常基本的代码,它需要一个数字(通过手动编辑代码(即不允许扫描程序)完成),然后打印所有数字的倍数一定的最大值(也可以在代码中手工计算)我的代码可以用于循环,值等等 - 这只是我们必须包含两种方法来打印它:一种方法很简单,每个数字都在一个新线,另一种方式更难 - 每行有6个数字,正确的意图,由几个空格分隔。我知道%(x/y/z/a/b/c)f
将打印字符串/整数/双打/等。右对齐基于x/y/z/a/b/c
的间距,但我不知道如何在6个数字后自动开始换行。从循环中输出/打印每行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循环,打印每次都打印出来,6号线在一个新行。提示:模数运算符 –
@MichaelMarkidis认为我已经正确输入了。 –