do while循环内的system.out.print()无效

问题描述 投票:0回答:1

system.out.print中的字符串(问题)表示填写数字。问题必须继续,直到我填写0。问题是do-while循环内的system.out.print无法正常工作

我的代码:

package com.company;

import java.util.Scanner;

public class WhileLoopNumbers {
public static void main(String[] args) {
    Scanner invoer = new Scanner(System.in);

    final int STOP_TEKEN = 0;
    int nummer = invoer.nextInt();

    do {
        System.out.print("Geef een getal: ");
    }
    while (nummer == STOP_TEKEN);
}
}
java
1个回答
0
投票

[尝试以可读的方式提供您的代码,以使我们能够提供有效的答案(英语)。当输入为0时,循环应该结束,但是现在您的代码使您要求输入数字直到输入除零以外的任何内容

要更正代码,请将您的while表达式更改为!=0。同样,您需要在循环内而不是循环外要求用户提供一个新数字。

package com.company;

import java.util.Scanner;

public class WhileLoopNumbers {

    public static void main(String[] args) {
        Scanner invoer = new Scanner(System.in);

        final int STOP_TEKEN = 0;
        do {
            int nummer = invoer.nextInt();
            System.out.print("Geef een getal: ");
        }
        while (nummer != STOP_TEKEN);
    }
}

0
投票
package com.company;

import java.util.Scanner;

public class WhileLoopNumbers {

    public static void main(String[] args) {
        Scanner invoer = new Scanner(System.in);

        final int STOP_TEKEN = 0;
        int nummer = invoer.nextInt();

        do {
            System.out.print("Geef een getal: ");
        }
        while (nummer != STOP_TEKEN);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.