package com.davidcleary.numberguesser;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int randomNumber;
boolean isRight;
public void randomGenerator(View view) {
boolean isRight = false;
EditText guess = (EditText) findViewById(R.id.guessText);
String myGuess = guess.getText().toString();
int guessInt = new Integer(guess.getText().toString());
if (guessInt == randomNumber) {
Toast.makeText(getApplicationContext(), "You Guessed The Right Number!!! " + "( " + guessInt + " )" , Toast.LENGTH_LONG).show();
guess.setText("");
isRight = true;
} else if (guessInt > randomNumber) {
Toast.makeText(getApplicationContext(), "Your Guess Is Too High! " + "( " + guessInt + " )", Toast.LENGTH_SHORT).show();
guess.setText("");
} else {
Toast.makeText(getApplicationContext() , "Your Guess Is Too Low! " + "( " + guessInt + " )", Toast.LENGTH_SHORT).show();
guess.setText("");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random randNum = new Random();
randomNumber = randNum.nextInt(21);
if (isRight == true) {
randomNumber = randNum.nextInt(21);
}
}
}
我正在尝试更改' randomNumber'当用户猜到正确的数字到不同的随机值但我没有找到解决方案。我试图使用一个布尔值但是没有用,因为它提出了一个错误"变量' randNum'已经在范围"
中定义非常感谢任何帮助。
答案 0 :(得分:0)
调整一些变量来修复范围......
package com.androidapps.numberguesser;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int randomNumber;
Random randNum = new Random();
public void randomGenerator(View view) {
EditText guess = (EditText) findViewById(R.id.guessText);
String myGuess = guess.getText().toString();
int guessInt = new Integer(guess.getText().toString());
if (guessInt == randomNumber) {
Toast.makeText(getApplicationContext(), "You Guessed The Right Number!!! " + "( " + guessInt + " )" , Toast.LENGTH_LONG).show();
guess.setText("");
randomNumber = randNum.nextInt(21);
} else if (guessInt > randomNumber) {
Toast.makeText(getApplicationContext(), "Your Guess Is Too High! " + "( " + guessInt + " )", Toast.LENGTH_SHORT).show();
guess.setText("");
} else {
Toast.makeText(getApplicationContext() , "Your Guess Is Too Low! " + "( " + guessInt + " )", Toast.LENGTH_SHORT).show();
guess.setText("");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
randomNumber = randNum.nextInt(21);
}
}
答案 1 :(得分:0)
我不完全确定我理解你的问题,但是......
您可能想要重复使用您正在使用的java.util.Random,因此我将其移至您的Activity
而非本地的属性中:
public class MainActivity extends AppCompatActivity {
int randomNumber;
Random generator;
然后您可以在onCreate
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
generator = new Random();
randomNumber = generator.nextInt(21);
}
然后,最后,在正确猜测之后,将randomNumber设置为新值:
if (guessInt == randomNumber) {
Toast.makeText(getApplicationContext(),
"You Guessed The Right Number!!! (" + guessInt + ")",
Toast.LENGTH_LONG).show();
guess.setText("");
randomNumber = generator.nextInt(21);
} // ... etc
答案 2 :(得分:0)
@ECHO OFF
DIR "C:\Users\user\Desktop\prueba" /q >"C:\Temp\output.txt"
感谢大家的快速回复!我的问题已经解决了。