我以前的问题已经解决。但是我从另一个类中提取数据的准确性存在问题。我知道currentScore.finalScore仅在使用时更新。但是,我有finalScore = currentScore.finalScore。我的打印行显示它会更新,但是看似随机的数字,即使该数字导致while语句变为false,它也会无限继续。

     public static void main(String[] args) {
       Scanner keyboard = new Scanner (System.in);
       String buddyName;
       String userChoice;
       int maxScore = 10;
       int minScore = 0;

       System.out.print("Nurse: The Patient's first name is ");
       buddyName = keyboard.nextLine();

       System.out.print("You: Let's see, I should (C)hange " + buddyName + "'s bandages," +
                       "(G)ive " + buddyName + " pain medication, " +
                    "(A)dd antibiotics to " + buddyName + "'s I.V," +
                    " or (D)ischarge " + buddyName + " ");

      userChoice = keyboard.nextLine();

      Buddy score = new Buddy(userChoice);
      BuddyScore currentScore = new BuddyScore(score);

      int finalScore;
      do {
         System.out.print("You: Let's see, I should (C)hange " + buddyName + "'s"
                + " bandages, (G)ive " + buddyName + " pain medication, " +
                    "(A)dd antibiotics to " + buddyName + "'s I.V," +
                    " or (D)ischarge " + buddyName + " ");

          userChoice = keyboard.nextLine();
          score.setUserChoice(userChoice);
          finalScore = currentScore.getFinalScore();
          System.out.println(currentScore.getFinalScore());
          }while (currentScore.getFinalScore() < maxScore && currentScore.getFinalScore() >= minScore);

           System.out.println("Curren score: " + currentScore.getFinalScore());




        }

  }


下面是我处理finalScore的类,也是我怀疑的类。

public class BuddyScore {
    private Buddy buddyScore;
    public int finalScore;


    public BuddyScore(Buddy buddyScore){
        this.buddyScore = buddyScore;
        this.finalScore = finalScore;
    }

    public void setFinalScore(int finalScore){
        this.finalScore = finalScore;
    }

    public int getFinalScore(){
      return finalScore += buddyScore.getBuddyScore();
    }

}


下面是处理finalScore所来自的buddyScore的类。

public class Buddy {
    private String userChoice;
    public int buddyScore;


    public Buddy(String userChoice){
        this.userChoice = userChoice;
    }


    public void setUserChoice(String userChoice){
        this.userChoice = userChoice;
    }


    public String getUserChoice(){
       return userChoice;
    }


    public void setBuddyScore(int buddyScore){
        this.buddyScore = buddyScore;
    }



    public int getBuddyScore(){

      switch (userChoice){
        case "C":
            buddyScore = 1;
            break;
        case "G":
            buddyScore = -2;
            break;
        case "A":
            buddyScore = 3;
            break;
        case "D":
            buddyScore = 7;
            break;
        default:
            buddyScore = 0;
            break;
      };
     return buddyScore;

  }


先感谢您。

最佳答案

getFinalScore方法的副作用是,它会在最终返回之前用返回值finalScore更改getBuddyScore。结果,每次调用getFinalScore都会增加该值。在您的循环中,我看到您四次调用该方法。如果单步执行代码,则应该在同一迭代中看到四个完全不同的值。

我猜你想代替吗?

return finalScore + buddy.getBuddyScore();

09-15 20:29