简体   繁体   English

返回到原始开始位置,而无需退出应用程序并再次打开它

[英]Go back to original start position without Quitting the application and opening it again

purpose of the app really simple. 该应用程序的目的真的很简单。 Ask the user to enter a number and checks if the user number and randomly created number are the same. 要求用户输入数字,并检查用户数字和随机创建的数字是否相同。 Until the user enters the correct number it goes on. 在用户输入正确的数字之前,它会继续。 Once the user enters correct number how can i change my random number so they can continue without quitting the application.Right now i have to quit once i find the correct number.And open it back again. 一旦用户输入正确的号码,我该如何更改我的随机号码,以便他们可以继续而不退出应用程序。现在,一旦我找到正确的号码,我就必须退出,然后再次打开它。
public class MainActivity extends AppCompatActivity { int number; 公共类MainActivity扩展了AppCompatActivity {int number; //Global Variabble. //全局可变性。

    public void clicked (View view ){
       EditText userinput = (EditText)findViewById(R.id.userinput);
       String inputstring = userinput.getText().toString();
       if (inputstring == " "){
          Toast.makeText(getApplicationContext(),"You havent entered anything",Toast.LENGTH_SHORT).show();
    }

    int inputint = Integer.parseInt(inputstring);

    if (inputint > number ){
        Toast.makeText(getApplicationContext(),"No, too high",Toast.LENGTH_SHORT).show();
    }
    else if (inputint < number){
        Toast.makeText(getApplicationContext(),"No, too small",Toast.LENGTH_SHORT).show();
    }
    else{
        Toast.makeText(getApplicationContext(),"Well Done! You Guessed it",Toast.LENGTH_SHORT).show();

    }

        //System.out.println("Computer guessed number is: " + number);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    Random r = new Random();
    number = r.nextInt(21);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

} }

you can try random function like this 你可以尝试这样的随机函数

public void randomGenerator()
{
  Random r = new random
  number = r.nextInt(21) //put the int number creation in global
}

then call it at onCreate() 然后在onCreate()上调用它

    @Override
protected void onCreate(Bundle savedInstanceState) {

    randomGenerator()
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

also at the else statement 也在else语句中

else{
        Toast.makeText(getApplicationContext(),"Well Done! You Guessed it",Toast.LENGTH_SHORT).show();

        randomGenerator(); //random number after user get correct answer

    }

you can put 你可以放

Random r = new Random(); number = r.nextInt(21); userinput.setText("");

in a function, and call it when your intent is call for the first time (in the on create) and cal it again when the use has found the good number 在函数中,并在首次调用您的意图时调用它(在创建时),并在使用中找到合适的数字时再次对其进行校准

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM