我有一個程序如下所示 其中我正在檢查不同條件的開放價值(多大程度上買方價格大於或低於開放價格)如何消除在這種情況下,如果條件
有沒有什麼更好的辦法事先處理這個
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
}
}
感謝。
爲什麼你連續做5次? – Gladhus
「if(firstbuyer1 == open || firstbuyer1-open <0.50)」有時經常出現或者?請格式化您的代碼,比如「lessthan2 + 1」;變成「小於2 + 1」; - 「if(firstbuyer2-open <2)」「if(firstbuyer2 - open <2)」將提高可讀性:-) – Gizzmo
創建一個列表/數組/無論哪個買家,並將邏輯應用於包含的每個元素?! – Morfic