Android for循环不像java那样工作

时间:2016-04-04 12:19:01

标签: java android math primes

这是我的Android Java代码。我不明白为什么它不像java代码那样工作。它是素数的例子。假设我们想要找到介于1到5之间的素数。所以我期待结果2,3,5。但我只得到了结果5。在我的Java代码中,我得到了正确的结果。我的意思是2,3,5。请帮我弄清楚这个问题。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prime);
        Button btn = (Button)this.findViewById(R.id.click_btn);
        btn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v)  {
                TextView resp = (TextView) findViewById(R.id.response);
                // Get number from EditText
                EditText startnumber = (EditText) findViewById(R.id.first_number);
                EditText endnumber = (EditText) findViewById(R.id.second_number);

                // get the Strings from the EditTexts
                String number1 = startnumber.getText().toString();
                String number2 = endnumber.getText().toString();

                // Convert Strings to int
                int x1number = Integer.parseInt(number1);
                int x2number = Integer.parseInt(number2);

                String str = "List of prime numbers between " + x1number + " and " + x1number + ": ";
                //resp.setText(str);
                for(int i = x1number; i <= x2number; i++){
                    if(isPrime(i)){
                        resp.setText( str + String.valueOf(i));
                    }
                }

            }
        });
    }
    public static boolean isPrime(int n){
        if( n <= 1) {
            return false;
        }
        for( int i = 2; i <= n/2; i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

这是我的Java代码。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package primenumberstwo;
import java.util.Scanner;

/**
 *
 * @author vubon
 */
public class PrimeNumberstwo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner s = new Scanner(System.in);
        System.out.println("Enter your first number: ");
        int start = s.nextInt();
        System.out.println("Enter your second number: ");
        int end = s.nextInt();
        System.out.println("List of prime numbers bettween " + start + " and " + end);
        for(int i = start; i <= end; i++){
            if(isPrime(i)){
                System.out.println(String.valueOf(i));

            }
        }
    }

    public static boolean isPrime(int n){
        if( n <= 1) {
            return false;
        }
        for( int i = 2; i <= n/2; i++) {
           if (n % i == 0) {
               return false;
           } 
        }
        return true;
    }
}

4 个答案:

答案 0 :(得分:2)

您正在覆盖每次迭代中TextView resp的内容,这就是为什么您只看到最后一次。

尝试这样的事情:

    String str = "List of prime numbers between " + x1number + " and " + x1number + ": ";
    String result = "";
    for(int i = x1number; i <= x2number; i++){
        if(isPrime(i)){
            result = result + " " + i;
        }
    }
    if(!("".equalsIgnoreCase(result.trim()))){
        resp.setText(str + result);
    }

答案 1 :(得分:0)

尝试:

str = str + String.valueOf(i);
resp.setText(str);

答案 2 :(得分:0)

Android SDK中没有问题。你的逻辑错了。

            String str = "List of prime numbers between " + x1number + " and " + x1number + ": ";
            //resp.setText(str);
            for(int i = x1number; i <= x2number; i++){
                if(isPrime(i)){
                    resp.setText( str += String.valueOf(i));//see change here
                }
            }

答案 3 :(得分:0)

您已找到所有素数但在最后一次循环迭代时覆盖最后一个。

试试;

String str = "List of prime numbers between " + x1number + " and " + x1number + ": ";
    resp.setText(str);
    for(int i = x1number; i <= x2number; i++){
        if(isPrime(i)){
           resp.setText( resp.getText() + String.valueOf(i));
         }
    }