2013-09-16 85 views
0

我想在兩個editText中獲得輸入值的乘積。例如,我將在xValues中輸入[1,2,3,4,5],然後我將在freqValues中輸入[6,7,8,9,10],然後它將乘以(1 * 6),(2 * 7),(3 * 8),(4 * 9),(5 * 10)。我將如何做到這一點?請幫幫我。謝謝提前:)如何在兩個edittext中獲得輸入值的乘積?

final AutoCompleteTextView xValues = (AutoCompleteTextView) findViewById(R.id.x_Values); 
    final AutoCompleteTextView freqValues = (AutoCompleteTextView) findViewById(R.id.frequency_Values);  

    Button btnCalculate = (Button) findViewById(R.id.btncalculate); 
    btnCalculate.setOnClickListener(new OnClickListener(){ 

     public void onClick(View arg0) 
     {  
      String[]x = (xValues.getText().toString().split(",")); 
      String []freq = (freqValues.getText().toString().split(",")); 

      int[]convertedx=new int[x.length]; 
      int[]convertedfreq=new int[freq.length]; 
     }  
    }); 

回答

0

你必須做一些錯誤捕獲,以確保只有數字輸入,但一旦你得到的是想通了,做這樣的事情:

... 
String[]x = (xValues.getText().toString().split(",")); 
String []freq = (freqValues.getText().toString().split(",")); 

int product = 0; 

for(int i = 0; i < x.length(); i++) { 
    int tempX = Integer.parseInt(x[i]); 
    int tempFreq = Integer.parseInt(freq[i]); 

    product += (tempX * tempFreq); 
} 

假設數組被正確分割並且只包含整數,這個循環將從X []和Freq []中獲取第一個int,然後將它們相乘,並將它們添加到product中,然後從這些數組中獲取第二個int,分析字符串放入一個int中,然後將它們相乘並循環,直到數組結束。

相關問題