新程序员,做第三个项目,不能让应用程序运行



我是一名新程序员,在Android Studio中使用Java语言创建了一个更高或更低的猜谜游戏应用。我这个项目的目标是让用户能够输入1到100之间的数字,让应用程序能够在每次用户玩游戏时随机选择一个数字,并让应用程序能够告诉用户他们选择的数字是正确答案,还是更高或更低。我使用Android工作室使用Java。我把所有的代码都写在这里了,只是一直得到一些错误答案。例如,在我的"if (guess

ckage com.example.billyhodgesguessinggame;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.billyhodgesguessinggame.databinding.ActivityMainBinding;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText txtGuess;
private Button btnGuess;
private TextView lblOutput;
private int theNumber;
public void checkGuess() {
String guessText = txtGuess.getText().toString();
String message = "";
try {
int Guess = Integer.parseInt(guessText);
if (guess < theNumber)
message = guess + "is too low. Try again.";
else if (guess > theNumber)
message = guess + "is too high. Try again.";
else {
message = guess +
"is correct. You win! Let's play again!";
newGame();
}
} catch (Exception e) {
message = "Enter a whole number between 1 and 100.";
} finally {
lblOutput.setText(message);
txtGuess.requestFocus();
txtGuess.selectAll();
}
}

private AppBarConfiguration appBarConfiguration;
public void newGame() {
theNumber = (int)(Math.random() * 100 + 1);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtGuess = findViewById(R.id.txtGuess);
Button btnGuess = findViewById(R.id.btnGuess);
lblOutput = findViewById (R.id.lblOutput);
newGame();
btnGuess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
checkGuess();
}
});

com.example.billyhodgesguessinggame.databinding.ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
binding.fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@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_main, 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);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
}

  1. 使用Guessguess作为变量名。使用小写版本,只使用guess

  2. 本行中,去掉开头的Button。否则,您将声明一个新变量,而不是重用您已经在顶部声明的现有变量。

Button btnGuess = findViewById(R.id.btnGuess);

相关内容

  • 没有找到相关文章

最新更新