gpt4 book ai didi

java - 如何在问答游戏中保持得分?

转载 作者:行者123 更新时间:2023-11-29 07:38:42 25 4
gpt4 key购买 nike

我创建了一个问答游戏,我需要在不创建任何数据库的情况下保持得分。我为每个问题创建了一个单独的 Activity 和 .xml,用户需要在文本字段中输入答案,如果答案正确,它会自动转到下一个 Activity (即下一个问题)。现在,我需要以这样一种方式保持分数,即在用户输入正确答案后,分数应显示在屏幕的右上角。我怎么做?请帮忙。这是我的第一级第二个问题的 java Activity 和 xml 文件:

package com.golo.user.gaunkhanekatha;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Handler;



public class TwoActivity extends Activity {

public Button check;
public EditText typeh;
private Toast toast;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
toast = Toast.makeText(TwoActivity.this, "", Toast.LENGTH_SHORT);
check = (Button)findViewById(R.id.check1); //R.id.button is the id on your xml
typeh = (EditText)findViewById(R.id.type1); //this is the EditText id
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//Here you must get the text on your EditText
String Answer = (String) typeh.getText().toString(); //here you have the text typed by the user
//You can make an if statement to check if it's correct or not
if(Answer.equals("piano") || (Answer.equals("keyboard")))
{
///Correct Toast
toast.setText("Correct! Now, next question...");
toast.setGravity(Gravity.TOP | Gravity.LEFT, 500, 300);
toast.show();
Intent i = new Intent(TwoActivity.this, ThreeActivity.class);
startActivity(i);
finish();

}
else{
//It's not the correct answer
toast.setText("Wrong! Try Again!");
toast.setGravity(Gravity.TOP | Gravity.LEFT, 500, 300);
toast.show();
}
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
if(toast!= null) {
toast.cancel();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_aboutus, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:weightSum="1"
android:textAlignment="center"
android:id="@+id/level1">

<TextView
android:layout_width="300dp"
android:layout_height="200dp"
android:text="What has 88 keys but cannot open a single door?"
android:id="@+id/que1"
android:width="255dp"
android:textSize="30dp"
android:layout_margin="50dp"
android:textStyle="italic"
android:gravity="center" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/type1"
android:layout_gravity="center_horizontal"
android:hint="Type here..." />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check answer..."
android:id="@+id/check1"
android:layout_gravity="center_horizontal" />
</LinearLayout>

最佳答案

如果您希望在运行之间保留分数,您可以使用类似以下内容的内容将分数写入文本文件: How do I create a file and write to it in Java?

然后当你想显示分数时从中读取: Java: How to read a text file

或者,如果您只想在应用程序运行期间维护分数,则可以使用分数的单例。如:

public class Score {

private static int score = 0;

//So that this class can't be instantiated
private Score () {
}

//Return current score
public static int getScore() {
return score;
}

//Increment score
public static void increaseScoreByOne() {
score = score++;
}

//Decrement score
public static void decreaseScoreByOne() {
score = score--;
}

//Reset score back to 0
public static void resetScore() {
score = 0;
}
}

然后可以通过调用在应用程序的任何地方访问它:

Score.getScore();
Score.increaseScoreByOne();
Score.decreaseScoreByOne();
Score.resetScore();

关于java - 如何在问答游戏中保持得分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32605313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com