如何保持得分在疑问句并回答游戏? [英] How to keep score in a ques and answer game?

查看:181
本文介绍了如何保持得分在疑问句并回答游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一问一答的游戏,我需要不断得分,而无需创建任何数据库。我们为您的每一个问题的个人活动和.xml和用户需要输入答案文本字段如果答案是正确的,它会自动进入下一个活动(即下一个问题)。现在,我需要保持得分在这样一种方式,用户输入正确答案之后得分应在屏幕的右上角显示。我怎么做?请帮忙。这里是我的第一个级别的第二个问题,在java活动和xml文件:

 包com.golo.user.gaunkhanekatha;进口android.app.Activity;
进口android.content.Intent;
进口android.support.v7.app.AppCompatActivity;
进口android.os.Bundle;
进口android.view.Gravity;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;
进口android.os.Handler;公共类TwoActivity延伸活动{    公共按钮检查;
    公众的EditText typeh;
    私人吐司面包;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_two);
        敬酒= Toast.makeText(TwoActivity.this,,Toast.LENGTH_SHORT);
        检查=(按钮)findViewById(R.id.check1); //R.id.button是你的XML中的id
        typeh =(EditText上)findViewById(R.id.type1); //这是ID的EditText
        check.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                //执行上的点击动作
                //在这里,你必须让你的EditText文本
                字符串答案=(字符串)typeh.getText()的toString()。 //这里你有用户键入的文本
                //你可以让if语句来检查它是否是正确与否
                如果(Answer.equals(钢琴)||(Answer.equals(键盘)))
                {
                    ///正确吐司
                    toast.setText(正确的现在,下一个问题......!);
                    toast.setGravity(Gravity.TOP | Gravity.LEFT,500,300);
                    toast.show();
                    意图I =新意图(TwoActivity.this,ThreeActivity.class);
                    startActivity(ⅰ);
                    完();                }
                其他{
                    //这不是正确的答案
                    toast.setText(!错了再试一次!);
                    toast.setGravity(Gravity.TOP | Gravity.LEFT,500,300);
                    toast.show();
                }
            }
        });
    }    @覆盖
    保护无效的onDestroy(){
        super.onDestroy();
        如果(干杯!= NULL){
            toast.cancel();
        }
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.menu_aboutus,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();        // noinspection SimplifiableIfStatement
        如果(ID == R.id.action_settings){
            返回true;
        }        返回super.onOptionsItemSelected(项目);
    }
}

中的XML文件:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=@绘制/背景
    机器人:weightSum =1
    机器人:textAlignment =中心
    机器人:ID =@ + ID / 1级>    <的TextView
        机器人:layout_width =300dp
        机器人:layout_height =200dp
        机器人:文字=什么有88个键,但不能开单门?
        机器人:ID =@ + ID / que1
        机器人:宽=255dp
        机器人:TEXTSIZE =30dp
        机器人:layout_margin =50dp
        机器人:TEXTSTYLE =斜体
        机器人:比重=中心/>    <的EditText
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / TYPE1
        机器人:layout_gravity =CENTER_HORIZONTAL
        机器人:提示=请在此处键入.../>    <按钮
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=检查答案......
        机器人:ID =@ + ID / CHECK1
        机器人:layout_gravity =CENTER_HORIZONTAL/>
< / LinearLayout中>


解决方案

如果你想保持在得分过程之间,你可以使用沿着此线的东西写分数到一个文本文件:
<一href=\"http://stackoverflow.com/questions/2885173/how-to-create-a-file-and-write-to-a-file-in-java\">How创建一个文件,写Java中的文件吗?

然后再从它时,你想要显示的比分如下:
在读取文本文件的Java

另外,如果你想只保持可以使用的比分单应用程序的运行期间的得分。如:

 公共类分数{    私有静态诠释得分= 0;    //所以这个类不能被实例化
    私人得分(){
    }    //返回当前比分
    公共静态INT getScore(){
        返回分数;
    }    //增加分数
    公共静态无效increaseScoreByOne(){
        得分=得分++;
    }    //递减分数
    公共静态无效decreaseScoreByOne(){
        得分= score--;
    }    //分数重置回0
    公共静态无效resetScore(){
        得分= 0;
    }
}

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

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

I've created a question and answer game and I need to keep score without creating any database. I've created an individual activity and .xml for each question and user needs to input answer in a text field and if the answer is correct, it automatically goes to next activity (i.e. next question). Now, I need to keep score in such a way that after user enters a correct answer the score should be shown in the top right corner of the screen. How do I do that? please help. Here's the java activity and xml file for my Second question of first level:

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);
    }
}

The xml file:

<?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>

解决方案

If you want the scores to be kept between runs, you could write the scores to a text file using something along the lines of this: How to create a file and write to a file in Java?

And then read from it when you want to display the score: Reading a text file in Java

Alternatively if you wanted to just maintain a score for the duration of the running of the application you could use a singleton of the score. Such as:

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;
    }
}

This could then be accessed anywhere in the application by calling:

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

这篇关于如何保持得分在疑问句并回答游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆