【问题标题】:intro java coding. don't understand what i'm doing wrong介绍java编码。不明白我做错了什么
【发布时间】:2013-11-04 21:32:46
【问题描述】:

我真的不确定我的代码有什么问题。它应该通过接受用户的选择,将其与随机的计算机选择进行比较,并显示结果来对计算机进行剪刀石头布。

我得到两个错误,我没有第 3 和第 4 方法的返回语句。另外,当我在不修复错误的情况下运行它时,从第 60 行开始的嵌套 if 语句只打印出两个 println 语句之一,这对我来说真的是零意义。

import java.util.Random;
import java.util.Scanner;
public class Chapter5ProjectPart2 {

    public static void main(String[] args) {

    Random generator = new Random();
    Scanner keyboard = new Scanner(System.in);

    int userNum;
    int compNum;
    String userChoice = "";
    String compChoice = "";
    int rnum;
    int result = 0;
    boolean keepPlaying;
    int input = 1;


    do
    {   
        compNum = generator.nextInt(2)+1;
        compChoice = numToChoice(compNum);

        menu();

        userNum = keyboard.nextInt();
        userChoice = numToChoice(userNum);
        keyboard.nextInt();

        System.out.println();
        System.out.println("you chose " + userChoice);
        System.out.println("the computer chose " + compChoice);

        result = resultCheck(userNum, compNum);


        if (result == 1) // user wins
        {
            if (userNum == 1) //user won choosing rock
            {
                System.out.println("rock beats scissors");
                System.out.println("you win");
            }
            else if (userNum == 2) //user won choosing paper
            {
                System.out.println("paper beats rock");
                System.out.println("you win");
            }
            else if (userNum == 3)  //user won choosing scissors
            {
                System.out.println("scissors beats paper");
                System.out.println("you win");
            }
        }
        else if (result == 3) //user loses
        {
            if (userNum == 1)  //user lost choosing rock
            {
                System.out.println("paper beats rock");
                System.out.println("you lose");
            }
            else if (userNum == 2)  //user lost choosing paper
            {
                System.out.println("scissors beats paper");
                System.out.println("you lose");
            }
            else if (userNum == 3)  //user lost choosing scissors
            {
                System.out.println("rock beats scissors");
                System.out.println("you lose");
            }
        else if (result == 2) //draw
            System.out.println("draw");
        }

        System.out.println("would you like to play again?");
        System.out.println("1 = yes");
        System.out.println("2 = no");
        input = keyboard.nextInt();
        keepPlaying = play(input);

    } while (keepPlaying == true);

}


// method 1 (menu)
public static void menu()
{
System.out.println("Enter your choice of rock, paper, or scissors\n" + "1 = rock\n" + "2 = paper\n" + "3 = scissors");
}

// method 2 (result check)
public static int resultCheck(int userNum, int compNum)
{
    if (userNum == 2 && compNum == 1)
        return 1;
    else if (userNum == 1 && compNum == 3)
        return 1;
    else if (userNum == 3 && compNum == 2)
        return 1;
    else if (userNum == compNum)
        return 2;
    else
        return 3;
}

// method 3 (converting number choice to rock/paper/scissors
public static String numToChoice(int num)
{
    if (num == 1)
        return "rock";
    else if (num == 2)
        return "paper";
    else if (num == 3)
        return "scissors";
}

//method 4 (play again)
public static boolean play(int input)
{
    if (input == 1)
        return true;
    else if (input == 2)
        return false;
}


}

【问题讨论】:

  • 如果numToChoice(4) 会发生什么?还有play(7)?那些方法应该返回什么?
  • “缺少返回值”是因为并非所有路径都返回一些东西。输入整数实际上可能是 1、2 或 3 以外的其他值。在这些情况下,应该返回什么?在后面加上else return "invalid"; 或类似的东西。
  • "当我运行它而不修复错误时..." 你如何运行它?存在编译时错误。
  • @Lion:通过在以前编译的版本上运行它,或者使用 Eclipse 的编译器,它会生成带有非编译部分并引发运行时异常的类文件。

标签: java


【解决方案1】:

我收到两个错误,我没有针对第 3 和第 4 方法的返回语句。

没错。我们来看第三个:

public static String numToChoice(int num)
{
    if (num == 1)
        return "rock";
    else if (num == 2)
        return "paper";
    else if (num == 3)
        return "scissors";
}

假设 num 不是 1、2、 3?那么方法的返回值应该是什么呢?这就是您收到错误的原因,您需要一个最终的else(没有if)说明当早期分支都没有返回值时该返回值应该是什么。没有它,该方法会导致编译时错误。

另外,当我在不修复错误的情况下运行它时,从第 60 行开始的嵌套 if 语句只打印出两个 println 语句之一,这对我来说真的是零意义。

无法在不修复错误的情况下运行它,因为这些是编译时错误。如果您尝试在存在这些错误的情况下编译此源代码,则会失败,并且您不会获得更新的类文件。因此,如果您随后尝试运行,并且它似乎可以工作,那么您正在运行您在出现这些错误之前编译的类文件的早期副本。该类文件与当前的源代码无关,因此对您来说毫无意义是可以理解的。您没有查看 JVM 正在运行什么。

如果您更正方法以便编译(通过添加最终的else 而没有if),然后运行编译结果,事情应该更有意义。同时,您可能希望删除之前的 Chapter5ProjectPart2.class 文件,因为它已过期。

【讨论】:

    【解决方案2】:
    public static String numToChoice(int num)
    {
        if (num == 1)
            return "rock";
        else if (num == 2)
            return "paper";
        else if (num == 3)
            return "scissors";
    }
    
    //method 4 (play again)
    public static boolean play(int input)
    {
        if (input == 1)
            return true;
        else if (input == 2)
            return false;
    }
    

    这些方法应该总是返回一些东西。例如方法 3,如果 int num 为 4,它不会返回任何内容。通过添加解决它:

    else return "";
    

    【讨论】:

    • 不过,您不能从布尔方法返回空字符串。
    • 在第三种方法中添加我所说的。在第四种方法中,将“else if”简单地替换为“else”
    【解决方案3】:

    这是因为您没有在方法 3 和 4 中以 else 子句结束您的 else-if 语句。

    【讨论】:

      【解决方案4】:

      您确实缺少return 语句。 Java 期望非 void 方法总是返回值或抛出异常。以非 void 方法结束而不返回或抛出异常是错误的。

      public static String numToChoice(int num)
      {
          if (num == 1)
              return "rock";
          else if (num == 2)
              return "paper";
          else if (num == 3)
              return "scissors";
      }
      

      这里,如果 num 不是 1、2 或 3,您的方法不会返回任何内容。同样:

      public static boolean play(int input)
      {
          if (input == 1)
              return true;
          else if (input == 2)
              return false;
      }
      

      这里不是 1 或 2 的输入缺少它们的 return 语句。

      【讨论】:

        【解决方案5】:

        首先,永远不要运行不能编译的代码。

        第二:检查这个方法:

        public static boolean play(int input)
        {
            if (input == 1)
                return true;
            else if (input == 2)
                return false;
        }
        

        如果输入为 6、-67 或 789,该方法会返回什么?你没有说出来,编译器也猜不到。所以它拒绝编译,直到你告诉它在这些情况下要返回什么。

        如果其他情况不应该发生,则抛出异常:

        public static boolean play(int input)
        {
            if (input == 1)
                return true;
            else if (input == 2)
                return false;
            else {
                throw new IllegalStateException("input is " + input + ". Something's really wrong");
            }
        }
        

        【讨论】:

          【解决方案6】:

          Java 方法必须返回一个值,除非将 void 声明为返回。

          在您的play 方法中,如果input 不是1 或2,Java 将不会返回任何值。这不允许在 Java 中编译。

          在您的numToChoice 方法中,如果num 不是1、2 或3,Java 将不会返回任何值。这不允许在 Java 中编译。

          添加 else 关闭以在“意外”情况下返回值,并允许 Java 编译。

          【讨论】:

            猜你喜欢
            • 2013-05-11
            • 1970-01-01
            • 1970-01-01
            • 2023-04-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-01-04
            • 2014-04-19
            相关资源
            最近更新 更多