在我的 while 循环代码中,当输入数字为负数时,我想再次提示用户输入数字,但这样做时出现错误

In my code in while loop when the input number is negative I want to prompt the user again to enter the number, but I got error doing so

import java.util.Scanner;

public class SumOfNumber
{
    public static void main(String[] args)
    {
        // Declare variable
        int inpNum;
        int total;

        // Prompt the user to input a number;
        Scanner keyboard = new Scanner(System.in);

        // Prompt user for positive nonzero integer.
        System.out.println("Enter a positive nonzero integer");

        // Convert the input into int.
        inpNum = keyboard.nextInt();

        // Assign value to total.
        total = 0;

想要在输入的数字小于零时提示用户

    // Method to find sum of integers.
    while( inpNum < 0 )
    {
        System.out.println("Negative integer entered.\n"
                        + "Please enter a positive integer");

当我执行程序时,出现错误:找不到

的符号

inpNum = keyboard.nextInt();

        inpNum = Keyboard.nextInt();
    }

        while( inpNum >= 1 )
        {
            total += inpNum;
            inpNum--;
        }

        System.out.println("Sum of the positive integers is " + total);

}

}

改变

inpNum = Keyboard.nextInt(); //to
inpNum = keyboard.nextInt();

您的问题出在行 inpNum = Keyboard.nextInt(); 中,您将扫描仪声明为 keyboard 并将其用作 Keyboard 大写。