簡體   English   中英

無法將一種方法調用為另一種方法

[英]Trouble calling one method into another

我有一個程序,處理使用不同方法的不同循環。 該程序作為一個整體,除了調用另一個方法之外,還可以正常工作。

我在調用另一個方法時遇到麻煩。

我想做的是:

  1. 讓該方法調用另一個方法並運行該方法,該方法從用戶那里獲取一個變量(1-100)。
  2. 如果輸入的輸入無效,則遞增變量。
  3. 連續輸入3個輸入時,顯示消息並退出菜單。

被調用的方法本身就可以很好地工作,當我調用它時它也可以工作。

我不能工作的是在3次無效輸入之后的顯示消息。 目前,它需要大約7個無效輸入,然后顯示消息?

起作用的方法:

public static boolean processGrade(int percentMark)
{
    Menu m = new Menu();
    clrscr();
    System.out.println("Please enter your mark e.g. 90. (input -1 to exit): "); 
    percentMark = Genio.getInteger();
    if
    (percentMark >=70 && percentMark <=100)
    {
        clrscr();
        System.out.println("Your Grade is A - Excellent!\n\n");
        pressKey();
        clrscr();
    }
    else if(percentMark >= 60 && percentMark <70)
    {
        clrscr();
        System.out.println("Your Grade is B - Good!\n\n");
        pressKey();
        clrscr();
    }
    else if (percentMark >=50 && percentMark <60)
    {
        clrscr();
        System.out.println("Your Grade is C - Ok!\n\n");
        pressKey();
        clrscr();
    }
    else if(percentMark >=40 && percentMark <50)
    {
        clrscr();
        System.out.println(" Your Grade is D - Must Do Better!\n\n");
        pressKey();
        clrscr();
    }
    else if (percentMark <40 && percentMark >= 0 )
    {
        clrscr();
        System.out.println(" Your Grade is E - Must Do Better!\n\n");
        pressKey();
        clrscr();
    }
    else if (percentMark < -1 || percentMark >100)
    {
        clrscr();
        System.out.println("ERROR: Value MUST be in the range of 0 - 100!");
        pressKey();
        clrscr();
        return false;
    }
    else if (percentMark == -1)
    {
        //clrscr();
        System.out.println("You entered -1, you will now return to the menu!");
        pressKey();
        return false;
    }
    return true;
}

我無法使用的方法會調用上述消息:

public static void processGradeV2(int percentMark)
{
    int invalid = 0;
    outerloop:
    do {   
        clrscr();
        boolean result = processGrade(percentMark);
        processGrade(percentMark);// Call processGrade method

        if(result == false)
        {
            invalid++;
        }
        if(invalid == 3)
        {
            clrscr();
            System.out.println("Sorry, you have entered an invalid integer 3 times in a row! The program will return to the menu screen.");
            pressKey();
            break outerloop;
            //return;
        } 
        if(percentMark == -1)
        {
            clrscr();
            System.out.println("You entered -1, you will now return to the menu!");
            pressKey();
            clrscr();
            break outerloop;
            //processUserChoices();
        }
    }
    while(invalid <3);
 }
processGrade(percentMark);// Call processGrade method

會在這里給您帶來問題,請將其刪除。

您已經分配了processGrade(percentMark); 結果到result變量。

我將刪除boolean result = processGrade(percentMark) ,而是將您的if語句更改為:

 if(!processGrade(percentMark)){
     invalid++;
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM