java while循环中数字的除数

java while循环中数字的除数,java,while-loop,numbers,Java,While Loop,Numbers,我有一段Java代码,是我为赋值编写的,但是我使用了for循环,当时我们只允许使用while循环。我如何才能将其转换为while循环 代码的目的是获取由逗号分隔的输入数字,并告诉有多少连续的重复数字。i、 e.1,1是连续的,但2,1,2不是连续的 import java.util.Scanner; public class ConsecDouplicates { public static void main(String[] args) { Scanner scan = new S

我有一段Java代码,是我为赋值编写的,但是我使用了for循环,当时我们只允许使用while循环。我如何才能将其转换为while循环

代码的目的是获取由逗号分隔的输入数字,并告诉有多少连续的重复数字。i、 e.
1,1
是连续的,但
2,1,2
不是连续的

import java.util.Scanner;

public class ConsecDouplicates {

  public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


System.out.print("Enter numbers: ");
String str = scan.nextLine();


String[] numbers = str.split(",");

Integer last = null;
Integer current = null;
int total = 0;

for (String number: numbers) {

  current = Integer.parseInt(number);


  if (last != null) {
    if(last == current){
      System.out.println("Duplicates: " + current);
      total = total +1;
    }
  }
  last = current;

}
System.out.println("Total duplicates: " + total);
  }
} 

我假设您的其他逻辑良好,那么您可以简单地将的
转换为
,而
循环如下所述:

    int count = 0;

    while ( count < numbers.length) {

      current = Integer.parseInt(numbers[count]);


      if (last != null) {
        if(last == current){
          System.out.println("Duplicates: " + current);
          total = total +1;
        }
      }
      last = current;
      count++;

    }
int count=0;
while(计数<数字.长度){
current=Integer.parseInt(数字[count]);
if(last!=null){
如果(最后==当前){
System.out.println(“重复项:+当前项”);
总计=总计+1;
}
}
last=当前值;
计数++;
}

多亏它工作得很好,当涉及到专门使用while循环的逻辑时,我遇到了很多问题。