使用sharedpreferences后计算错误。

huangapple go评论94阅读模式
英文:

Wrong calculation after using sharedpreferences

问题

我正在构建一个应用程序,用于检查素数并显示它们。用户需要能够保存数字并在应用程序重新启动时加载它。

当从Sharedprefrences中检索保存的整数时,我遇到问题。代码保存整数并在“loaddata”函数中加载它。但是当我开始检查保存的数字是否是素数时,代码会跳过2个素数。例如,如果我保存“11”,下一个素数应该是“13”,但它跳到了“19”。

如果有人能指导我一下,那将非常好,因为我有点新手。

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private EditText editText;
    private Button applyPrimeButton;
    private Button saveButton;
    private Button loadButton;

    public static final String SHARED_PREFS = "sharedPrefs";

    int max = 500;
    int j = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textview);
        editText = (EditText) findViewById(R.id.edittext);
        applyPrimeButton = (Button) findViewById(R.id.apply_prime_button);
        saveButton = (Button) findViewById(R.id.save_button);
        loadButton = (Button) findViewById(R.id.load_button);

        applyPrimeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = j; i <= max; i++) {
                    if (isPrimeNumber(i)) {
                        textView.setText(i + "");
                        j = i + 1;
                        break;
                    }
                }
            }
        });

        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveData();
            }
        });

        loadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadData();
            }
        });
    }

    public boolean isPrimeNumber(int nummer) {
        for (int i = 2; i <= nummer / 2; i++) {
            if (nummer % i == 0) {
                return false;
            }
        }
        return true;
    }

    public void saveData() {
        SharedPreferences sp = getSharedPreferences(SHARED_PREFS, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        int nummer = Integer.parseInt(textView.getText().toString());
        editor.putInt("prime_save", nummer);
        editor.commit();

        Toast.makeText(this, "Data saved", Toast.LENGTH_SHORT).show();
    }

    public void loadData() {
        SharedPreferences sp = getSharedPreferences(SHARED_PREFS, Activity.MODE_PRIVATE);
        int nummer = sp.getInt("prime_save", 0);
        textView.setText(String.valueOf(nummer));
    }
}

希望这可以帮助你解决问题。

英文:

I'm building an app to check prime numbers and display them. the user need to be able to save the number and load it upon restart of the app.

I have problem when retrieving the saved int from Sharedprefrences. the codes saves the int and loads it on the "loaddata" function. But when I start checking if the saved in is a primenumber, the code jumps 2 primenumbers. Eg if I save "11", the next primenumber should be "13" but it jumps to "19".

Would be great if someone could point into the right direction since i'm a bit of newbie.

public class MainActivity extends AppCompatActivity {
private TextView textView;
private EditText editText;
private Button applyPrimeButton;
private Button saveButton;
private Button loadButton;
public static final String SHARED_PREFS = &quot;sharedPrefs&quot;;
int max = 500;
int j = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
editText = (EditText) findViewById(R.id.edittext);
applyPrimeButton = (Button) findViewById(R.id.apply_prime_button);
saveButton = (Button) findViewById(R.id.save_button);
loadButton = (Button) findViewById(R.id.load_button);
applyPrimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = j; i &lt;= max; i++) {
if (isPrimeNumber(i)) {
textView.setText(i+&quot;&quot;);
j = i+1;
break;
}
}
}
});
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveData();
}
});
loadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadData();
}
});
}
public boolean isPrimeNumber(int nummer) {
for (int i = 2; i &lt;= nummer / 2; i++) {
if (nummer % i == 0) {
return false;
}
}
return true;
}
public void saveData() {
SharedPreferences sp = getSharedPreferences(SHARED_PREFS, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
int nummer = Integer.parseInt(textView.getText().toString());
editor.putInt(&quot;prime_save&quot;, nummer);
editor.commit();
Toast.makeText(this, &quot;Data saved&quot;, Toast.LENGTH_SHORT).show();
}
public void loadData() {
SharedPreferences sp = getSharedPreferences(SHARED_PREFS, Activity.MODE_PRIVATE);
int nummer = sp.getInt(&quot;prime_save&quot;,0);
textView.setText(String.valueOf(nummer));
}
}

答案1

得分: 0

几个问题:

  1. 当加载数据时,您没有将 j 更新为正确的值,而是依赖于您在文本视图中显示的内容。
  2. 要找到素数,只需要检查原始数字的平方根,而不是它的一半。

尝试这样做:

applyPrimeButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (j < max) {
            if (isPrimeNumber(j)) {
                textView.setText(j + "");
                j++;
            }
        }
    }
});

public void saveData() {
    SharedPreferences sp = getSharedPreferences(SHARED_PREFS, Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putInt("prime_save", j);
    editor.commit();

    Toast.makeText(this, "数据已保存", Toast.LENGTH_SHORT).show();
}

public void loadData() {
    SharedPreferences sp = getSharedPreferences(SHARED_PREFS, Activity.MODE_PRIVATE);
    int nummer = sp.getInt("prime_save", 0);
    j = nummer;
    textView.setText(String.valueOf(nummer));
}

注意:我保留了变量和方法名称的原始拼写,因为这些是代码的一部分。

英文:

Several issues:

  1. You are not updating j to the correct value when loading data, and instead relying on what you are displaying in the textview.
  2. To find prime number, it is enough to check until the square root of the original number and not half of it.

Try this:

applyPrimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(j&lt;max ){
if (isPrimeNumber(j)) {
textView.setText(j+&quot;&quot;);
j++;
}
}
}
});
public void saveData() {
SharedPreferences sp = getSharedPreferences(SHARED_PREFS, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt(&quot;prime_save&quot;, j);
editor.commit();
Toast.makeText(this, &quot;Data saved&quot;, Toast.LENGTH_SHORT).show();
}
public void loadData() {
SharedPreferences sp = getSharedPreferences(SHARED_PREFS, Activity.MODE_PRIVATE);
int nummer = sp.getInt(&quot;prime_save&quot;,0);
j = nummer;
textView.setText(String.valueOf(nummer));
}

huangapple
  • 本文由 发表于 2020年9月23日 03:32:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64016562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定