【发布时间】:2015-10-09 13:56:58
【问题描述】:
我正在编写一个代码,它可以查找并打印用户指定值之间的每个数字,只要这些数字可以被 5 和 6 整除,但不能同时被 5 和 6 整除。代码的要求之一是每行只有 10 个输出,这就是我遇到的问题:
import java.util.Scanner;
public class DivisibleBy5And6
{
public static void main (String [] args)
{
Scanner scan = new Scanner (System.in);
final int PER_LINE = 10;
int value = 0;
int count = 0;
int max = 0;
String end = ";
do
{
System.out.print ("\nEnter a minimum number: ");
value = scan.nextInt();
System.out.print ("Enter a maximum number: ");
max = scan.nextInt();
System.out.print ("\n");
while (value <= max)
{
if ((value % 5) < 1 ^ (value % 6) < 1)
System.out.print (value + " ");
value++;
count++;
if (count % PER_LINE == 0)
System.out.println();
}
max = 0;
count = 0;
System.out.print ("\n");
System.out.print ("\nContinue? <y/n> ");
end = scan.next();
}while (!end.equalsIgnoreCase("n"));
}
}
我弄乱了我给出的关于如何限制输出的示例,我已经看到“count”if 语句有效并且我知道它为什么有效,但是关于“value”if 中的计算的一些内容声明使输出看起来不像它应该的那样。如果有人知道我错过了什么,我将不胜感激。
【问题讨论】:
-
创建一个大小为 10 的数组。填充它。打印它。重复。
-
附加提示:将计数器设置为零应该尽可能靠近使用它的位置和之前,而不是前后不远。