简体   繁体   English

连续输入(输入/扫描器)两个相同的数字以中断“ while” Java循环

[英]Entering (input/scanner) two same numbers consecutively to break a loop “while” Java

I am a new-bee in java, I have a problem that i cant figure out to compare previous entered number(int) with next one continuously and I need to write a program that repeatedly reads numbers from the user's keyboard. 我是Java的新手,我有一个问题,我想不通连续比较下一个输入的数字(int)和下一个数字,我需要编写一个程序来重复从用户键盘读取数字。 The program stops looping when the user types the same number twice in a row. 当用户连续两次键入相同的数字时,程序将停止循环。

Thanks in advance for your kind guidance. 预先感谢您的友好指导。

Here's a sample run of the program: 5 13 21 5 4 5 5 Done! 这是该程序的示例运行:5 13 21 5 4 5 5完成!

Following was my unsuccessful effort :) 以下是我未成功的努力:)

Scanner input = new Scanner(System.in); 扫描仪输入=新的Scanner(System.in); System.out.println("Enter Numbers"); System.out.println(“输入数字”);

    int x = 0;
    int y = 0;
    x = input.nextInt();
    y = input.nextInt();
    while (x != y) {

        x = input.nextInt();
        y = input.nextInt();

    }

    System.out.println("Done!!!!!!!");
    input.close();

You can use loop to read number from console and stop if previous nubmer equals to current one. 您可以使用循环从控制台读取数字,如果先前的数字等于当前数字则停止。 As marker of first number you can use eg null value of Integer prv (as alternative, you can use boolean isFirstLine flag for first line or res.isEmpty() ). 作为第一个数字的标记,您可以使用例如Integer prv null值(作为替代,可以对第一行使用boolean isFirstLine标志或res.isEmpty() )。

public static List<Integer> receiveNumbers() {
    List<Integer> res = new LinkedList<>();
    Integer prv = null;

    try (Scanner scan = new Scanner(System.in)) {
        while (true) {
            int num = scan.nextInt();

            if (prv != null && prv == num)
                break;

            res.add(num);
            prv = num;
        }
    }

    return res;
}

import java.util.Scanner; 导入java.util.Scanner;

public class OngoingPractice { 公共课程OngoingPractice {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    int previous, current;

    previous = keyboard.nextInt();
    current = keyboard.nextInt();

    while (current != previous) {
        previous = current;
        current = keyboard.nextInt();
    }

    System.out.println("Done!");

Remember the last entered data in one variable and check it with the current data. 记住在一个变量中最后输入的数据,并与当前数据进行核对。 If both matches, break the loop. 如果两者都匹配,请打破循环。

        Scanner scanner = new Scanner(System.in);            
        String previousNumber="";
        while (scan.hasNextInt()) {
          int number = scan.nextInt(); 
          if(!previousNumber.equals("") && number==Integer.parseInt(previousNumber)) { 
            break;
          }else {
            System.out.println(number);
          }            
          previousNumber=number+"";
        }

Just use an infinite while loop and break if the new int is equal to previous one. 只需使用无限的while循环,然后在新的int等于先前的int中断即可。 As a suggestion you should show how you tried. 作为建议,您应该显示如何尝试。

Scanner sc = new Scanner(System.in);
    int i;
    Integer j = null;
    boolean flag = false;
    while(true) {
        i = sc.nextInt();
        if(j==null) {j=i; flag = true;}
        if(j==i&&!flag) {
            System.out.println("Done");
            break;}
        j=i;
        flag = false;
    }

Edited : if first one is -1, it won't work as in the comment. 编辑:如果第一个为-1,它将无法像注释中那样工作。 so I modified some. 所以我修改了一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM