Java 如何组合if/else语句
基本上,这个程序在一台计算机之间扮演一个骰子角色,然后显示谁得分更高。所以在我最后一次的if/else声明中,主要的区别是你赢了,还是你输了。有没有什么方法可以让我把它组合起来,这样更干净?我试着把它组合起来,但想不出来。有什么想法吗?谢谢你Java 如何组合if/else语句,java,Java,基本上,这个程序在一台计算机之间扮演一个骰子角色,然后显示谁得分更高。所以在我最后一次的if/else声明中,主要的区别是你赢了,还是你输了。有没有什么方法可以让我把它组合起来,这样更干净?我试着把它组合起来,但想不出来。有什么想法吗?谢谢你 //import scanner and random import java.util.Random; import java.util.Scanner; //declare variables and methods class Main { int
//import scanner and random
import java.util.Random;
import java.util.Scanner;
//declare variables and methods
class Main {
int userOne, userTwo, compOne, compTwo, userTotal, compTotal;
char playAgain;
Scanner scan = new Scanner(System.in);
Random gen = new Random();
//method to run entire program
public void runProgram()
{
int r=1;
//this will run the roll of the dice for the user and the computer
for(r=1; r<2;)
{
System.out.println("Your turn:");
userOne = gen.nextInt(6)+1;
System.out.println("Your first roll was: " + userOne);
userTwo = gen.nextInt(6)+1;
System.out.println("Your second roll was: " + userTwo);
userTotal = userOne + userTwo;
System.out.println("Your total of the two rolls was: " + userTotal);
System.out.println("Computers turn:");
compOne = gen.nextInt(6)+1;
System.out.println("The computers first roll was: " + compOne);
compTwo = gen.nextInt(6)+1;
System.out.println("The computers second roll was: " + compTwo);
compTotal = compOne + compTwo;
System.out.println("The computers total of the two rolls was: " + compTotal);
//This determines win or loss and lets the user choose if they want to play again
if (userTotal > compTotal)
{
//winning- statement to ask if the user wants to play again
System.out.println("You won! Would you like to play again? Respond with Yes or No: ");
playAgain = scan.next().charAt(0);
if ((String.valueOf(playAgain)).equalsIgnoreCase("y") == true)
{
r=1;
}
else
{
r=2;
}
}
else
{
//losing- statement to ask if the user wants to play again
System.out.println("Sorry, you lost. Would you like to play again? Yes or No?");
playAgain = scan.next().charAt(0);
if ((String.valueOf(playAgain)).equalsIgnoreCase("y") == true)
{
r=1;
}
else
{
r=2;
}
}
}
}
public static void main(String[] args) {
Main prog = new Main();
prog.runProgram();
}
}
//导入扫描仪和随机
导入java.util.Random;
导入java.util.Scanner;
//声明变量和方法
班长{
int userOne,userTwo,compOne,compTwo,userTotal,compTotal;
再次播放;
扫描仪扫描=新扫描仪(System.in);
Random gen=新的Random();
//方法来运行整个程序
公共程序()
{
int r=1;
//这将为用户和计算机运行掷骰子
对于(r=1;r合计)
{
//获胜-询问用户是否想再次玩的声明
System.out.println(“你赢了!你想再玩一次吗?回答是或否:”);
playreach=scan.next().charAt(0);
if((String.valueOf(playreach)).equalsIgnoreCase(“y”)==true)
{
r=1;
}
其他的
{
r=2;
}
}
其他的
{
//losing-询问用户是否希望再次播放的语句
System.out.println(“对不起,您输了。您想再玩一次吗?是还是否?”);
playreach=scan.next().charAt(0);
if((String.valueOf(playreach)).equalsIgnoreCase(“y”)==true)
{
r=1;
}
其他的
{
r=2;
}
}
}
}
公共静态void main(字符串[]args){
主程序=新主程序();
prog.runProgram();
}
}
正如khelwood所建议的,您可以在win/loss if语句之外组合播放消息
if (userTotal > compTotal) {
System.out.println("You won!");
} else {
System.out.println("Sorry, you lost.");
}
System.out.println("Would you like to play again? Yes or No?");
playAgain = scan.next().charAt(0);
if ((String.valueOf(playAgain)).equalsIgnoreCase("y")) {
r = 1;
} else {
r = 2;
}
我想说的是,使用此代码可以改进的一点是避免复制和粘贴代码。永远不要重复你自己 第二件需要改进的事情是,您可以在((String.valueOf(playreach)).equalsIgnoreCase(“y”)时执行
循环,而不是执行奇怪的for
循环。这样,您就不会有一个令人困惑的r变量,该变量基于playreach是否为“y”
public void runProgram()
{
playAgain = 'y';
//this will run the roll of the dice for the user and the computer
while ((String.valueOf(playAgain)).equalsIgnoreCase("y") == true)
{
System.out.println("Your turn:");
userOne = gen.nextInt(6)+1;
System.out.println("Your first roll was: " + userOne);
userTwo = gen.nextInt(6)+1;
System.out.println("Your second roll was: " + userTwo);
userTotal = userOne + userTwo;
System.out.println("Your total of the two rolls was: " + userTotal);
System.out.println("Computers turn:");
compOne = gen.nextInt(6)+1;
System.out.println("The computers first roll was: " + compOne);
compTwo = gen.nextInt(6)+1;
System.out.println("The computers second roll was: " + compTwo);
compTotal = compOne + compTwo;
System.out.println("The computers total of the two rolls was: " + compTotal);
//This determines win or loss and lets the user choose if they want to play again
if (userTotal > compTotal)
{
//winning- statement to ask if the user wants to play again
System.out.println("You won! Would you like to play again? Respond with Yes or No: ");
}
else
{
//losing- statement to ask if the user wants to play again
System.out.println("Sorry, you lost. Would you like to play again? Yes or No?");
}
playAgain = scan.next().charAt(0);
}
}
进行这些更改可以使您的代码更清楚地了解您打算让程序做什么。只需将重复的内容移出if-else语句。
另外,==true
可以从if()
中删除,因为它已经返回布尔值
if (userTotal > compTotal){
//winning- statement to ask if the user wants to play again
System.out.println("You won! Would you like to play again? Respond with Yes or No: ");
}else{
//losing- statement to ask if the user wants to play again
System.out.println("Sorry, you lost. Would you like to play again? Yes or No?");
}
playAgain = scan.next().charAt(0);
if ((String.valueOf(playAgain)).equalsIgnoreCase("y")){
r=1;
}else{
r=2;
}
是的,通过将负责确定是否再次运行程序的代码移到if/else语句之外,因为用户赢了或输了都无关紧要,最后问题仍然是:是否要再次播放?因此,无论结果如何,这种逻辑都不应该被负责确定谁是一个的逻辑所封装(因为同样,这是不相关的)
第二件事,这是完全可选的,但它会使您的代码更干净,似乎您使用if/else语句只是为了确定哪些值应该分配给变量,在这种情况下,使用
还有一件事,当使用返回布尔值true/false的方法时,没有必要键入==false/true,因为函数本身已经这样做了,例如equalsIgnoreCase()函数
因此,在所有这些编辑之后,您的代码如下所示:
//import scanner and random
import java.util.Random;
import java.util.Scanner;
//declare variables and methods
class Main {
int userOne, userTwo, compOne, compTwo, userTotal, compTotal;
char playAgain;
Scanner scan = new Scanner(System.in);
Random gen = new Random();
String result;
//method to run entire program
public void runProgram() {
int r=1;
//this will run the roll of the dice for the user and the computer
for(r=1; r<2;)
{
System.out.println("Your turn:");
userOne = gen.nextInt(6)+1;
System.out.println("Your first roll was: " + userOne);
userTwo = gen.nextInt(6)+1;
System.out.println("Your second roll was: " + userTwo);
userTotal = userOne + userTwo;
System.out.println("Your total of the two rolls was: " + userTotal);
System.out.println("Computers turn:");
compOne = gen.nextInt(6)+1;
System.out.println("The computers first roll was: " + compOne);
compTwo = gen.nextInt(6)+1;
System.out.println("The computers second roll was: " + compTwo);
compTotal = compOne + compTwo;
System.out.println("The computers total of the two rolls was: " + compTotal);
//This determines win or loss and lets the user choose if they want to play again
result = userTotal > compTotal ? "won! " : "lost. ";
// Ternary operator
System.out.println("you " + result + "Would you like to play again? Yes or No?");
playAgain = scan.next().charAt(0);
// another ternary operator
r = String.valueOf(playAgain).equalsIgnoreCase("y") ? 1 : 2;
}
}
public static void main(String[] args) {
Main prog = new Main();
prog.runProgram();
}
}
//导入扫描仪和随机
导入java.util.Random;
导入java.util.Scanner;
//声明变量和方法
班长{
int userOne,userTwo,compOne,compTwo,userTotal,compTotal;
再次播放;
扫描仪扫描=新扫描仪(System.in);
Random gen=新的Random();
字符串结果;
//方法来运行整个程序
公共程序(){
int r=1;
//这将为用户和计算机运行掷骰子
对于(r=1;r总计?“赢了!”:“输了”;
//三元运算符
System.out.println(“您”+结果+“您想再次播放吗?是还是否?”);
playreach=scan.next().charAt(0);
//另一个三元算子
r=String.valueOf(再次播放).equalsIgnoreCase(“y”)?1:2;
}
}
公共静态void main(字符串[]args){
主程序=新主程序();
prog.runProgram();
}
}
为什么不将两个分支中的所有内容都移动到else块之后呢?使用while(true)代替r
,并且当他们不想再次播放时,中断。请删除==true
equalsIgnoreCase
已返回boolean@MrCodingB这是真的。然而,有些人可能会说,包含==true
有助于提高可读性。是的,但大多数约定告诉您不要使用它,因此最好不要发布它回答