![](/img/trans.png)
[英]Printing a character from an int value with a loop with a desired number of numbers per line of output
[英]Outputting/printing 6 numbers per line from a loop
我正在嘗試編寫一個非常基本的代碼,該代碼需要一個數字(通過手動編輯代碼來完成(即不允許使用掃描儀),然后將所有倍數打印到一定的最大值(也可以在代碼中手動估算)。我有用於循環,值等的代碼-只是我們必須包括兩種打印方式,一種方式很簡單,每個數字都換行,另一種方式更難-每行都有6號,右意,由幾個空格分開。我知道%(x/y/z/a/b/c)f
將打印字符串/整數/雙打/等。右對齊與間隔基於所述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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.