我正在尝试写一个非常基本的代码,它需要一个数字(通过手动编辑代码来完成(即不允许扫描仪),然后打印所有它的倍数直到某个最大值(也可以在代码中手动计算的东西)。我有工作的循环,值等代码-它只是我们必须包括2种方式打印它。一种方法很简单,每个数字都在一个新的行上。另一种方法要难得多--每行有6个数字,用几个空格隔开。我知道%(x/y/z/a/b/c)f将打印字符串/int/doubles/等。基于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("");
}
}这是我到目前为止拥有的代码。
发布于 2016-09-24 05:47:34
这是%运算符的一个相对简单的用法:
for (increment = incvalue; increment < max + incvalue; increment += incvalue) {
System.out.print(increment);
if (increment % (incvalue * 6) == 0)
System.out.println();
}https://stackoverflow.com/questions/39669943
复制相似问题