所以我有这段代码,它基本上是试图找到数组中的最大数字和最小数字(这里是票价)。但是我发现自己写了两个 for 循环,我想知道是否有更有效的方法来写这个?
/** Setting cheapestCost to the index of the cheapest transport obj*/
for(int i = 0; i < 15; i++) {
if(allTransports[cheapestCost].getTicketPrice() > allTransports[i].getTicketPrice()) {
cheapestCost = i;
}
}
/** Setting greatestCost to the index of the most expensive transport obj*/
for(int i = 0; i < 15; i++) {
if(allTransports[greatestCost].getTicketPrice() < allTransports[i].getTicketPrice()) {
greatestCost = i;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我的页面上有一个登录框,我想将其放在页面中间,所以我使用了:
.login {
width: 280px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
Run Code Online (Sandbox Code Playgroud)
它奏效了。但是,我不确定为什么。根据我的理解,顶部和左侧 50% 将框的左上角放在中间,所以框本身的中心还不在中心,但是在变换平移线之后,它将框放在中间, 如何?
我认为平移只会将 50% 的 x 轴和 50% 的 y 轴平移回原始位置。