有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何组合if/else语句

基本上,这个程序在一台计算机之间扮演一个骰子角色,然后显示谁得分更高。所以在我最后的if/else声明中,主要的区别是你赢了,还是你输了。我能不能把它混合起来,这样更干净?我试着把它组合起来,但没能搞清楚。有什么想法吗?谢谢

//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();
  }
}

共 (2) 个答案

  1. # 1 楼答案

    我想说的是,使用此代码可以改进的一点是避免复制和粘贴代码。永远不要重复你自己

    第二件需要改进的事情是,你可以让你的循环while((String.valueOf(playAgain)).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);
        }
    }
    

    进行这些更改可以让您的代码更清楚地了解您打算让程序做什么

  2. # 2 楼答案

    只是将重复的内容移出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;
    }