我有一个程序,如下所示 其中我用不同的条件检查开放价值(买方价格大于或小于开盘价的程度)
有没有更好的方法来处理这个
package com;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test extends Thread {
public static void main(String[] args) {
double firstbuyer1 = 1.34;
double firstbuyer2 = 2.34;
double firstbuyer3 = 3.45;
double firstbuyer4 = 2.45;
double firstbuyer5 = 1.50;
double open = 3.40;
int positivevalue =0;
int lessthan2 =0;
// checking the positive conditions
if(firstbuyer1==open||firstbuyer1-open<0.50)
{
positivevalue = positivevalue+1;
}
if(firstbuyer2==open||firstbuyer1-open<0.50)
{
positivevalue = positivevalue+1;
}
if(firstbuyer3==open||firstbuyer1-open<0.50)
{
positivevalue = positivevalue+1;
}
if(firstbuyer4==open||firstbuyer1-open<0.50)
{
positivevalue = positivevalue+1;
}
if(firstbuyer5==open||firstbuyer1-open<0.50)
{
positivevalue = positivevalue+1;
}
// // checking the negative conditions
if(firstbuyer1-open<2)
{
lessthan2 = lessthan2;
}
if(firstbuyer2-open<2)
{
lessthan2 = lessthan2+1;
}
if(firstbuyer3-open<2)
{
lessthan2 = lessthan2+1;
}
if(firstbuyer4-open<2)
{
lessthan2 = lessthan2+1;
}
if(firstbuyer5-open<2)
{
lessthan2 = lessthan2+1;
}
// similarly i need to write for lessthan 3 , lessthan 4 , lessthan 5
}
}
提前致谢。
答案 0 :(得分:1)
而不是按如下方式声明5个不同的变量 -
double firstbuyer1 = 1.34;
double firstbuyer2 = 2.34;
double firstbuyer3 = 3.45;
double firstbuyer4 = 2.45;
double firstbuyer5 = 1.50;
使用集合。例如数组如下 -
double[] firstbuyers = {1.34, 2.34, 3.45, 2.45, 1.50};
现在条件相似,您可以使用循环。 e.g。
for(int i = 0; i < firstbuyers.length; i++) {
// ... refer to current firstbuyer as firstbuyers[i]
if((firstbuyer[i] - open) < 2) {
lessthan2 = lessthan2 + 1;
}
}
答案 1 :(得分:1)
尝试使用数组。
double[] buyer = {1.34, 2.34, 3.45, 2.45, 1.50};
double open = 3.4;
int positivevalue = 0;
int lessthan2 = 0;
for(int a=0; a<buyer.Length; a++)
{
if(buyer[a] == open || buyer[a]-open<0.50)
positivevalue++;
if(buyer[a]-open<2)
lessthan2++;
}
答案 2 :(得分:0)
package com;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test extends Thread {
public static void main(String[] args) {
double firstbuyer1 = 1.34;
double firstbuyer2 = 2.34;
double firstbuyer3 = 3.45;
double firstbuyer4 = 2.45;
double firstbuyer5 = 1.50;
double open = 3.40;
int positivevalue =0;
int lessthan2 =0;
// checking the positive conditions
if(firstbuyer1==open||firstbuyer1-open<0.50)
{
positivevalue = positivevalue + 5;
}
// checking the negative conditions
if(firstbuyer1-open<2)
{
lessthan2 = lessthan2 + 4;
}
// similarly i need to write for lessthan 3 , lessthan 4 , lessthan 5
}
}
答案 3 :(得分:0)
首先,这段代码在精度方面不正确。你不能使用双 - &lt; some_other_double。您可以尝试例如:
double d1 =0.83;
double d2= 0.5;
double diff = d1 -d2;
不会像你期望的那样产生0.33,而是0.32999999999999996。 所以代码中的问题不仅是方便的方法,而且是正确的方法。您应该使用BigDecimal以获得正确的结果。然后,您可以使用具有以下签名的方法,例如:
boolean isLessThan(BigDecimal d1, BigDecimal d2, BigDecimal lessThanLimit);
答案 4 :(得分:0)
使用函数封装条件逻辑
public class Test extends Thread {
public static int buyerPositive(double buyer, double open){
return buyer == open || buyer - open < 0.50 ? 1 : 0;
}
public static int buyerNegative(double buyer, double open, int amount) {
return buyer - open < amount ? 1 : 0;
}
public static void main(String[] args) {
double firstbuyer1 = 1.34;
double firstbuyer2 = 2.34;
double firstbuyer3 = 3.45;
double firstbuyer4 = 2.45;
double firstbuyer5 = 1.50;
double[] buyers = {firstbuyer1, firstbuyer2, firstbuyer3, firstbuyer4, firstbuyer5 };
double open = 3.40;
int positivevalue =0;
int lessthan2 =0;
int lessthan3=0;
int lessthan4 =0;
int lessthan5 =0;
for (double buyer : buyers){
positivevalue += buyerPositive(buyer, open);
lessthan2 += buyerNegative(buyer, open, 2);
lessthan3 += buyerNegative(buyer, open, 3);
lessthan4 += buyerNegative(buyer, open, 4);
lessthan5 += buyerNegative(buyer, open, 5);
}
}
}