简体   繁体   English

使用两个循环,但只有一个循环运行。 计算机科学导论(Java)

[英]Using two loops but only one loop runs through. Intro to computer science.(Java)

So I am trying to create a simple stock program.所以我正在尝试创建一个简单的股票程序。 My first loop works but, it won't pass to my second one and I can't figure out why.我的第一个循环有效,但是它不会传递给我的第二个循环,我不知道为什么。 I've tried breaks and if statements and I can't figure it out.我试过中断和 if 语句,但我无法弄清楚。

days = JOptionPane.showInputDialog("Enter the number of days in the stock period"); //Allows user to enter value
sharePoints = JOptionPane.showInputDialog("Enter the share points on the first day");   
int daynum = Integer.parseInt(days);
int share = Integer.parseInt(sharePoints);

int dayStart = 1;
double daynumMidPoint = (daynum/2);
System.out.println(share);
Math.round(daynumMidPoint);

for(int i = dayStart;i<daynumMidPoint;i++) {
    System.out.println(share=share+50);
}

for(int l = dayStart;l>=daynumMidPoint;l++){
    System.out.println(share=share-25);
}

Well if the first loop works, the second one will not since the first loop will only start if dayStart is under daynumMidPoint and the second one will only start dayStart is above or equal to daynumMidPoint .好吧,如果第一个循环有效,则第二个循环不会,因为第一个循环只会在dayStart低于daynumMidPoint启动,而第二个只会在dayStart大于或等于daynumMidPoint

If the first one succeeds then the second one definitely will not.如果第一个成功,那么第二个肯定不会。

Your math is wrong and the condition for the second loop is not fulfilled你的数学错了,第二个循环的条件没有满足

int dayStart = 1;
double daynumMidPoint = (daynum/2); // daynum = 10 ---> daynumMidPoint = 5
System.out.println(share);
Math.round(daynumMidPoint);

for(int i = dayStart;i<daynumMidPoint;i++) // i = 1, Condition: i < 5 = true
{
    System.out.println(share=share+50);
}
for(int l = dayStart;l>=daynumMidPoint;l++) // l = 1, Condition l >= 6 = false
{
    System.out.println(share=share-25);
}

So to fix this issue, you either need to adjust the condition for the second for loop.因此,要解决此问题,您需要调整第二个 for 循环的条件。 Or you need to set l to a different start value.或者您需要将 l 设置为不同的起始值。

Showing that this表明这

int dayStart = 1;

is set to one, this other loop wont work:设置为一,另一个循环将不起作用:

for(int l = dayStart;l>=daynumMidPoint;l++){
System.out.println(share=share-25);

since l is set to 1 and for the loop to run, you need l to be BIGGER or EQUAL than daynumMidPoint , and since this is set to double daynumMidPoint = (daynum/2);由于l设置为 1 并且要运行循环,因此您需要ldaynumMidPoint或相等,并且由于将其设置为double daynumMidPoint = (daynum/2); then it wont run since its bigger than l .那么它不会运行,因为它大于l

Seeing also that it seems you want Shares to increase if the current day is below the midpoint and decrease if the current day is above the midpoint, you can just put this(or something like this):还看到如果当天低于中点,您似乎希望股票增加,如果当天高于中点,则减少,您可以输入以下内容(或类似内容):

if(i<daynumMidPoint){
SOP(share=share+50);} 
else{
SOP(share=share-25);
}

inside this for-loop :在这个 for 循环中:

for(int i = dayStart;i<daynumMidPoint;i++) {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM