检查正数的输入

时间:2015-10-14 03:20:00

标签: java loops error-checking

因此,对于我的编程作业,用户必须输入成绩数,但我们必须检查输入并返回正确的错误。该数字必须是一个正数,程序需要在错误之间有所不同,并给出正确的响应并将其循环回去,直到输入正确的数字。我得到了错误检查部分,但我无法让程序继续下一部分。任何帮助将不胜感激

#include <stdio.h>
int main() 
{
int tot=0, probc=0, a=1, b=1, c=1, d=1, e=1, cnt=16;

while(e<=cnt)
{
   while(d<=cnt)
    {
        while(c<=cnt)
        {
            while(b<=cnt)
            {
                while(a<=cnt)
                {
                    if(a==b){++probc;}
                    else if(a==c){++probc;}
                    else if(a==d){++probc;}
                    else if(a==e){++probc;}
                    else if(b==c){++probc;}
                    else if(b==d){++probc;}
                    else if(b==e){++probc;}
                    else if(c==d){++probc;}
                    else if(c==e){++probc;}
                    else if(d==e){++probc;}
                    ++a;++tot;
                }
                a=1;++b;
            }
            b=1; ++c;
        }
        c=1; ++d;
    }
    d=1;++e;
}
printf("%d / %d", probc,tot);
return 0;
}
output:
524416 / 1048576

1 个答案:

答案 0 :(得分:0)

试试这个,

//use a boolean to tell the loop to continue or not
boolean cont = true;

do {


    System.out.println("Enter number of grades");

    if (input.hasNextInt()){

        numGrade = input.nextInt();

        //I assume 0 is a valid response (not a negative int)
        if (numGrade <= 0){
            System.out.println("Your number of grades needs to positive! Try again");
            continue;
        }
        else {
            cont = false;
            System.out.println("Your input is valid! Value entered is " + numGrade);
        }
    }
    else {
        System.out.println("You did not enter a number! Try again");
                input.next();
                continue;
    }
}
while (cont);