如何在循环结束前获得循环的第n个项?在java

时间:2016-11-29 12:05:48

标签: java loops

我有循环,我试图在循环小于40时打印第n个项,所有这些都不会在小于40时停止循环。我试图在总数小于40时打印它但是我正在考虑最后一个第n个值。

当循环下降到40以下时,我需要的是第n个值。我几乎完成了我的代码,但这个问题让我感到放松。

工作示例,这是输出应该是

  

请输入标记:82

     

请输入显示天数:3

     

方案1

     

(0)82.0(1)73.80(2)66.42(3)59.78

     

这项工作可能会在失败前最多6天

这是我的程序输出

  

请输入标记:82

     

请输入显示天数:3

     

方案1

     

(0)82.0(1)73.80(2)66.42(3)59.78

         int yourValue = -1;

// Loop
while (true)
{
    if (numOfDays >= i) System.out.print("(" + i + ") ");
    System.out.printf("%.02f",total);
    System.out.print("   ");
    total = total * 0.9;
    if (total <= 20) {
        if (numOfDays >= i) System.out.print("\nBecause mark drops below 20, mark stays as 20. final mark="+ finalMark);
        break;
    }
    if (total < 40 && yourValue == -1) yourValue = i;
    i++;
}
System.out.print("\nThis work can be up to " + yourValue + " days late before failing.");

3 个答案:

答案 0 :(得分:0)

我不确定我是否理解正确,但是输出预期行为的代码类似于:

    boolean isFinished = false;
    int lastValue = 0;
    for(i = 0;i <= numOfDays;i++){
        System.out.print("(" + i + ") " + total+ "   ");
        total = total -5;

        if (total<=20){
            System.out.print("\nBecause mark drops below 20, mark stays as 20. final mark="+ total);
            break;
        }

        if (!isFinished && total < 40){
            isFinished = true;
            lastValue = i;
        }

    } 

    if(isFinished) {
        System.out.print("\nThis work can be up to " + lastValue);
    }

答案 1 :(得分:0)

我不确定我是否正确理解您的问题,但请尝试以下代码:

Create table contient(
Num_Spect varchar(30) not null,
Code_Module varchar(30) not null,
constraints pk_contient primary key (Num_Spect,Code_Module),
constraints fk_spect  FOREIGN key (Num_Spect) REFERENCES specialite(Num_Spect),
constraints fk_module  FOREIGN key (Code_Module) REFERENCES module(Code_Module));

如果int yourValue = -1; // Loop for(i = 0;i <= numOfDays;i++){ System.out.print("(" + i + ") " + total+ " "); total = total -5; //yourValue will only be filled the first time total is below 40 if (total < 40 && yourValue == -1) yourValue = i; if (total<=20){ System.out.print("\nBecause mark drops below 20, mark stays as 20. final mark="+ finalMark); break; } } // End if (yourValue > -1) System.out.print("\nThis work can be up to " + yourValue + " days late before failing."); 属于total,则40的值会存储在i

如果yourValue低于total,则退出循环,您可以继续循环后面的代码


编辑:

你不能“超越循环获得价值”,但你很容易重构你的代码: (代码未经测试)

20


EDIT2:

请注意,您必须将int total2 = total; //Loop for(i = 0;i <= numOfDays;i++){ System.out.print("(" + i + ") " + total+ " "); total -= 5; if (total<=20){ System.out.print("\nBecause mark drops below 20, mark stays as 20. final mark="+ finalMark); break; } } System.out.print("\nThis work can be up to " + Math.floor((total2 – 40) / 5)+ " days late before failing."); 的值舍入为total,否则您必须使用integer double(例如,我希望total 1}}是total

double

只要int i = 0; int yourValue = -1; while (true) { if (numOfDays > i) System.out.print("(" + i + ") " + total+ " "); total = total * 0.9; if (total <= 20) { if (numOfDays > i) System.out.print("\nBecause mark drops below 20, mark stays as 20. final mark="+ finalMark); break; } if (total < 40 && yourValue == -1) yourValue = i; i++; } System.out.print("\nThis work can be up to " + yourValue + " days late before failing."); 大于20或total小于i,循环就会一直运行,但对于输出,我们会检查numOfDay是否仍然是低于i


EDIT3:

numOfDays


Edit4:

我想我找到了一个公式来换取这个double total2 = total; //Loop ... //End Loop int counter = -1; while(total2 >= 40) { counter++; total2 = total2 + 0.9; } System.out.print("\nThis work can be up to " + counter+ " days late before failing.");

试试以下内容:

Math.floor((total2 – 40) / 5)

没有时间在实践中检查这一点,但它应该有效。

答案 2 :(得分:0)

你的if语句出于for循环块,它假设是这样的:

// Loop
    for(int i = 0;i <= numOfDays;i++){
        System.out.print("(" + i + ") " + total+ "   ");
        total = total -5;

         if (total<=20){
         System.out.print("\nBecause mark drops below 20, mark stays as 20.
final mark="+ finalMark);
        return;
         }
        if (total < 40){
         System.out.print("\nThis work can be up to " + i);
        }
     }