2013-08-25 23 views
-1

中的不同用戶輸入返回數字值我試圖創建一個基於用戶輸入返回分數的應用程序。如何根據android

例如,如果用戶在特定網站上的帖子1000它會返回1.我將在10000

1000 = 1 2000 = 2結束吧等

這裏是我到目前爲止,謝謝。這個網站真棒。 現在我只是每個條目添加。值1 +值2等

public class DataIn extends Activity { 

EditText editPostCount; 
EditText editThanksCount; 
EditText editRomCount; 
EditText editThemeCount; 
EditText editKernelCount; 
EditText editTutorialCount; 
EditText editYearsJoined; 
Button mButton; 
TextView results; 
Button mButton1; 

@Override 
public void onCreate (Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.data_in); 
android.app.ActionBar actionBar = getActionBar(); 
actionBar.setDisplayHomeAsUpEnabled(true); 

editPostCount = (EditText)findViewById(R.id.editPostCount); 
editThanksCount = (EditText)findViewById(R.id.editThanksCount); 
editRomCount = (EditText)findViewById(R.id.editRomThreads); 
results = (TextView)findViewById(R.id.results); 
editThemeCount = (EditText)findViewById(R.id.editThemeCount); 
editKernelCount = (EditText)findViewById(R.id.editKernelCount); 
editTutorialCount = (EditText)findViewById(R.id.editTutorialCount); 
editYearsJoined = (EditText)findViewById(R.id.editYearsJoined); 

mButton = (Button)findViewById(R.id.results_button); 
mButton.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     //When the button is clicked, call the calucate method. 
     calculate(); 
     } 
}); 


private void calculate() { 
    try { 

     Double value1 = Double.parseDouble(editPostCount.getText().toString()); 
     Double value2 = Double.parseDouble(editThanksCount.getText().toString()); 
     Double value3 = Double.parseDouble(editRomCount.getText().toString()); 
     Double value4 = Double.parseDouble(editKernelCount.getText().toString()); 
     Double value5 = Double.parseDouble(editThemeCount.getText().toString()); 
     Double value6 = Double.parseDouble(editYearsJoined.getText().toString()); 
     Double value7 = Double.parseDouble(editTutorialCount.getText().toString()); 
     //do the calculation 

     Double calculatedValue = (value1+value2+value3+value4+value5+value6+value7); 
     //set the value to the textView, to display on screen. 
     results.setText(calculatedValue.toString()); 


    } catch (NumberFormatException e) { 
     // EditText EtPotential does not contain a valid double 
    } 

mButton1 = (Button)findViewById(R.id.clear_button); 
mButton1.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 

      editPostCount.setText(""); 
      editThanksCount.setText(""); 
      editRomCount.setText(""); 
      editThemeCount.setText(""); 
      editKernelCount.setText(""); 
      editTutorialCount.setText(""); 
      editYearsJoined.setText(""); 
      results.setText("");} 
}); 

    } } 
+0

請正確格式化您的代碼 – olevegard

+0

請解釋。 –

+0

看看它......有很多空行,第一行,'公共類DataIn extends Activity {'和最後一行'}'不是代碼標籤中的縮進,這個縮進遍佈整個地方。 4個空格用於堆棧溢出縮進。 – olevegard

回答

0

您可以使用一個簡單的除法得到每個值的得分,即切成一個整數。 在這個例子中,我還定義了一個常量來爲每個不同的值確定一個特定的分數因子。

private static final int TOTALCOUNT_SCOREFACTOR = 1000; 

int totalCountScore = totalCount/TOTALCOUNT_SCOREFACTOR; 

我建議你不要使用雙打,一般int就夠了。 我也建議你使用一個值的數組,而不是單獨定義它們。這樣,您可以在將來輕鬆添加或刪除值。

+0

不是每個值都會被1000除。只是前兩個值。所以我會根據需要定義每個整數? –

+0

當然,這就是爲什麼如果你製作更多不同的常數就更清楚了,所以你可以將任何值除以不同的分頻器,並將所有分頻器保留在同一個位置。 –

+0

謝謝。我怎麼會把這些加在一起,就像我在我的帖子中一樣? –

0

我希望我沒有誤解你的問題,但如果你想將比分加1點,每1000帖,你只需1000例如獲得職位的數量,除以:

//value1 is the post count 
int calculatedvalue = value1/1000; 

所以,如果職位(值)的數量是3500,calculatedvalue將是3(分裂過程中,其餘部分被切斷)

+0

謝謝你的回答。所以我可以爲每個值做對嗎?因爲一些條目將會是像ROM數量或內核數量這樣的小數字。我會將其添加到我當前的方法中嗎? –

+0

你會在你的方法中定義一個可以保持運行總數的變量。將計算的返回類型從void更改爲int,並在計算完成後返回運行總計。而不是你的函數調用是「calculate();」,它會返回一個值,所以你的調用看起來像「score = calculate();」 –