我正在使用Android Studio构建一个简单的应用程序;这是我的相关代码:
import java.util.Random;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Random r = new Random();
int num = r.nextInt(1000);
EditText e = (EditText) findViewById(R.id.enterText);
int userNum = Integer.parseInt(e.getText().toString());
TextView info = (TextView) findViewById(R.id.userInfo);
private void compare()
{
if(userNum > num)
{
info.setText("Your number is too high!");
}
else if(userNum < num)
{
info.setText("Your number is too low!");
}
userNum = Integer.parseInt(e.getText().toString());
}
while(userNum != num)
{
compare();
}
}
我不知道为什么,但是在第37行,它说:
Unknown class: 'userNum'
Unknown class: 'num'
Unexpected token
这可能非常简单,但我似乎无法弄明白。
答案 0 :(得分:0)
请尝试以下代码,
import java.util.Random;
public class Sample extends AppCompatActivity {
private int userNum;
private int num;
private EditText e;
private TextView info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random r = new Random();
num = r.nextInt(1000);
e = (EditText) findViewById(R.id.enterText);
userNum = Integer.parseInt(e.getText().toString());
info = (TextView) findViewById(R.id.userInfo);
}
private void compare() {
if (userNum > num) {
info.setText("Your number is too high!");
} else if (userNum < num) {
info.setText("Your number is too low!");
}
userNum = Integer.parseInt(e.getText().toString());
while (userNum != num) {
compare();
}
}
}
答案 1 :(得分:0)
尝试:
private void compare()
{
Random r = new Random();
int num = r.nextInt(1000);
EditText e = (EditText) findViewById(R.id.enterText);
int userNum = Integer.parseInt(e.getText().toString());
TextView info = (TextView) findViewById(R.id.userInfo);
if(userNum > num)
{
info.setText("Your number is too high!");
}
else if(userNum < num)
{
info.setText("Your number is too low!");
}
userNum = Integer.parseInt(e.getText().toString());
}
while(userNum != num)
{
compare();
}