Java while循环中的三个连续数字
编写一个程序,从用户处读取整数,直到用户输入-1,如果有三个连续的数字,则打印“连续”,否则打印“非连续”;也就是说,在您读取的数字列表中,有一个整数k,其数值为k、k+1和k+2Java while循环中的三个连续数字,java,Java,编写一个程序,从用户处读取整数,直到用户输入-1,如果有三个连续的数字,则打印“连续”,否则打印“非连续”;也就是说,在您读取的数字列表中,有一个整数k,其数值为k、k+1和k+2 public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x = scan.nextInt(); int y = x + 1; int z = x + 2; boole
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y = x + 1;
int z = x + 2;
boolean areConsecutive = false;
while (x != -1) {
if (x == y) {
x = scan.nextInt();
if (x == z)
areConsecutive = true;
}
x = scan.nextInt();
}
if (areConsecutive)
System.out.print("Consecutive");
else
System.out.print("None Consecutive");
}
有谁能告诉我这个代码有什么问题吗?我会这样做,我们必须检查三个数字是否连续,无论用户输入的顺序如何:
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int[] numbers = new int[3];
for( int i = 0; i < numbers.length; ++i)
numbers[i] = scan.nextInt();
Arrays.sort(numbers);
boolean areConsecutive = true;
for( int i = 0; i < numbers.length - 1; ++i)
if(numbers[i+1] - numbers[i] != 1)
areConsecutive = false;
if(areConsecutive)
System.out.print("Consecutive");
else
System.out.print("None Consecutive");
}
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
int[]数字=新的int[3];
对于(int i=0;i
这就是您要寻找的:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y = scan.nextInt();
int z = scan.nextInt();
boolean areConsecutive = false;
while ((x != -1)&&(y != -1)&&(z != -1)){
if ((z == y + 1)&&(y == x + 1)
areConsecutive = true;
if (areConsecutive)
System.out.print("Consecutive");
else
System.out.print("None Consecutive");
}
Scanner scan = new Scanner(System.in);
System.out.println("enter");
int x = scan.nextInt();
boolean areConsecutive = false;
while (x != -1) {
int y = x + 1;
System.out.println("enter");
x = scan.nextInt();
if (x == y) {
System.out.println("enter");
int z = x + 1;
x = scan.nextInt();
if (x == z)
areConsecutive = true;
}
}
if (areConsecutive)
System.out.println("Consecutive");
else
System.out.println("None Consecutive");
您很接近,但没有正确维护数字的历史记录 首先,为了澄清,该规范要求您输入用户提供的任意数量的任意数字,并简单地检查其中的任意三个是否连续。因此,下面的第一行将具有连续序列(从第三个数字开始的
1 2 3
位),但第二行不会:
9 9 1 2 3 9
3 1 4 1 5 9
实现这一点的一种方法是简单地保持最少的信息来检测连续序列。要做到这一点,您只需要保留最后输入的三个数字的副本。此类beast的伪代码(可轻松转换为任何过程语言)为:
# Get first number, ensure no chance of consecutive
# sequence until at least three are entered.
num3 = getint()
num2 = num3
num1 = num3
consecutive = false
# Loop until -1 entered.
while num3 != -1:
# Check for consecutive sequence.
if (num1 + 1 == num2) and (num2 + 1 == num3):
consecutive = true
# Shift numbers "left".
num1 = num2
num2 = num3
num3 = getint()
if consecutive:
print "Consecutive"
else
print "None Consecutive"
在检查y之前获取下一个整数,然后检查z。如果其中一个失败,请更新y和z并再次检查
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y = x + 1;
int z = x + 2;
boolean areConsecutive = false;
while (x != -1) {
x = scan.nextInt();
if (x == y) {
x = scan.nextInt();
if (x == z)
areConsecutive = true;
}
y = x + 1;
z = x + 2;
}
if (areConsecutive)
System.out.print("Consecutive");
else
System.out.print("None Consecutive");
}
在扫描新x之前,需要将y和z增加1。 这就是您正在寻找的:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y = scan.nextInt();
int z = scan.nextInt();
boolean areConsecutive = false;
while ((x != -1)&&(y != -1)&&(z != -1)){
if ((z == y + 1)&&(y == x + 1)
areConsecutive = true;
if (areConsecutive)
System.out.print("Consecutive");
else
System.out.print("None Consecutive");
}
Scanner scan = new Scanner(System.in);
System.out.println("enter");
int x = scan.nextInt();
boolean areConsecutive = false;
while (x != -1) {
int y = x + 1;
System.out.println("enter");
x = scan.nextInt();
if (x == y) {
System.out.println("enter");
int z = x + 1;
x = scan.nextInt();
if (x == z)
areConsecutive = true;
}
}
if (areConsecutive)
System.out.println("Consecutive");
else
System.out.println("None Consecutive");
您需要在每次迭代后更新
y
和z
值,但我每次都给x一个新值,所以y和z不会随着x引入新值而改变吗?例如,如果x一开始是4,然后我给它一个新的值5,y会从5(4+1)切换到6(5+1)吗?z也是一样。不是,它们是基本类型