gpt4 book ai didi

java - 我如何找到循环中的第n项? java

转载 作者:行者123 更新时间:2023-12-01 09:12:09 24 4
gpt4 key购买 nike

这里是我编写的程序的快速浏览,以便为我的问题提供更好的示例。

循环代码

公共(public)无效方案1(int d){

     // first modification
if (mark<=20){
System.out.print("\nBecause mark under 20 mark stays as its original value. mark="+mark);
return;
}
int total = mark;
int finalMark=20;

System.out.print("Scheme 1"+"\n");

// Loop
for(int loopParameter = START_CONDITION;
loopParameter <= d;loopParameter++){
System.out.print("(" + loopParameter + ") " + total + " ");
total = total + constantDiffSch1;

// second modification

if (total < 40){
System.out.print("\nThis work can be up to " + loopParameter);
return;
}

// third modification
if (total<=20){
System.out.print("\nBecause mark drops below 20, mark stays as 20. final mark="+ finalMark);
return;
}
} // End
System.out.print("\n\n");

}

这是我的程序输出

Please input mark: 64

Please input number of days to display: 10

Scheme 1

(0) 64 (1) 59 (2) 54 (3) 49 (4) 44

This work can be up to 4 days late before failing.

这就是输出应该是的

Please input mark: 64

Please input number of days to display: 10

Scheme 1

(0) 64 (1) 59 (2) 54 (3) 49 (4) 44 (5) 39 (6) 34 (7) 29 (8) 24

This work can be up to 4 days late before failing.

我必须显示作业迟到了多少天并计算迟到罚金(标记-5)我还必须显示作业失败所需的天数(失败之前的天数可能大于用户输入的天数 (d) )。不及格分数低于 40。

第二个示例(输出)

Please input mark: 64

Please input number of days to display: 2

Scheme 1

(0) 64 (1) 59 (2) 54

This work can be up to 4 days late before failing.

我几乎已经完成了我的代码,但这个问题正在减慢我的速度。

附注我是java新手

这是我的完整程序

逾期处罚类

public class LatePenalties {

// attributes
private int mark;

private static final int constantDiffSch1 = -5;
private static final double constantDiffSch2 = 0.9;
private static final int START_CONDITION = 0;
// constructors
public LatePenalties(int m) {
mark = m;

}

// methods

public void scheme1(int d) {

// first modification
if (mark<=20){
System.out.print("\nBecause mark under 20 mark stays as its original value. mark="+mark);
return;
}
int total = mark;
int finalMark=20;

System.out.print("Scheme 1"+"\n");

// Loop
for(int loopParameter = START_CONDITION;
loopParameter <= d;loopParameter++){
System.out.print("(" + loopParameter + ") " + total + " ");
total = total + constantDiffSch1;

// second modification

if (total < 40){
System.out.print("\nThis work can be up to " + loopParameter);
return;
}

// third modification
if (total<=20){
System.out.print("\nBecause mark drops below 20, mark stays as 20. final mark="+ finalMark);
return;
}
} // End
System.out.print("\n\n");
}


public void scheme2(int d) {
double total = mark;

System.out.print("\n\nScheme 2"+"\n");

// Loop
for(int loopParameter = START_CONDITION;
loopParameter <= d;loopParameter++){
System.out.print( "(" + loopParameter + ") " );
System.out.printf("%.02f",total);
System.out.print(" ");

total = total * constantDiffSch2;
} // End
System.out.print("\n");
}
}

主类

import java.util.Scanner;

public class LatePenaltiesUser {

public static void main(String [] args) {
// local variables
Scanner input = new Scanner(System.in);
LatePenalties latePen;
int mark;
int days;

// input
do{
System.out.print("Please input mark (between 0 and 100) --> ");
mark = input.nextInt();
if (( mark < 0 ) | (mark > 100 )){System.out.print("\n" + "Input value outside the range!!!" + "\n");}
}while(( mark < 0 ) | (mark > 100 ));

do{
System.out.print("Please input number of days to display (between 0 and 20) --> ");
days = input.nextInt();
System.out.print("\n");
if (( days < 0 ) | (days > 20 )){System.out.print("Input value outside the range!!!"+ "\n");}
}while(( days < 0 ) | (days > 20 ));

// computation
latePen = new LatePenalties(mark);
latePen.scheme1(days);
latePen.scheme2(days);

}
}

我必须显示失败标记何时发生(小于 40),但我必须在 20 处或达到天数时停止循环,正如我在示例中显示的预期内容。

最佳答案

total 小于 40 时,您可以使用 break 退出循环。您可以将 scheme1 方法更新为下面

public void scheme1(int d) {

int total = mark;

System.out.print("Scheme 1" + "\n");
int days = 0;
// Loop
for (int loopParameter = START_CONDITION; loopParameter <= d; loopParameter++) {
System.out.print("(" + loopParameter + ") " + total + " ");
total = total + constantDiffSch1;

if(total < 40)
break;
days++;
} // End
if (total <= 40) {
System.out.print("\nThis work can be up to " + days +" days late before failing.");
}
System.out.print("\n\n");
}

Please input mark (between 0 and 100) --> 82 Please input number of days to display (between 0 and 20) --> 10

Scheme 1 (0) 82 (1) 77 (2) 72 (3) 67 (4) 62 (5) 57 (6) 52 (7) 47 (8) 42 This work can be up to 8 days late before failing.

关于java - 我如何找到循环中的第n项? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40852963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com