简体   繁体   中英

Java- Updating Return Data from a Class based on User Input

I'm not quite sure of a better/more clear way to state the issue in the title, so please forgive me.

What I currently am trying to do is change the value of a variable based on the user's input. Currently I am able to set a value depending on the input, however my program requires a loop where the user must make an input multiple times.

The issue I am having is that when I call for the class, it only returns the first input's relevant data. Therefor while the user can continue to make different inputs, the data is never updated past the first input.

import java.util.Scanner;

public class Hw6Wargolet {
   public static void main(String[] args) {
    Scanner keyboard = new Scanner (System.in); 
    String buddyName;
    String userChoice;

    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 finalScore = new BuddyScore();
     //the loop. essentially repeating the prompt for input until a score is reached
     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();

        System.out.println(score.getBuddyScore());
        System.out.println(finalScore.getFinalScore());
    }while(finalScore.getFinalScore() < 5);

   }

}

Below is my class which returns the data/value.

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){
        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; 
    }

}

Additionally, below is the class for taking each inputs score and adding/subtracting for a total score

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

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

   public BuddyScore(Buddy object2){
      buddyScore = object2.getBuddyScore();
   }


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

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

Any advice, tips, suggestions would be most appreciated. Thank you.

There are three things I noticed:

1)

in your Buddy class, in method setBuddyScore(int buddyScore) you do

buddyscore = buddyscore;

instead of

this.buddyscore = buddyscore;

(could lead to a mixup of member variables and parameters)

2) (edit)

In your main class nothing happens with the new userChoice . Maybe you could do something like this:

first change the BuddyScore class so that it would store the whole Buddy object instead of only the score. That way the BuddyScore object sort of knows that there where changes in the Buddy object. And then you need to call the getBuddyScore() method within the getFinalScore() to always add the current score.

public class BuddyScore {
    private Buddy buddyScore;

    public BuddyScore(Buddy buddyScore) {
        this.buddyScore = buddyScore;      // here you store the 'reference' of your Buddy object
    }

    public int getFinalScore() {
        return finalScore += buddyScore.getBuddyScore();  // here you retrieve the most recent 'buddyScore' from your 'Buddy' object and add it to the finalScore
    } 
}

Then back in you main class all you need is update the Buddy object (within the loop) to always know the latest userChoice:

do {
    // stuff
    userChoice = keyboard.nextLine();
    score.setUserChoice(userChoice);
}while (finalScore.getFinalScore() < 5);   // here your finalScore object will add the recent score from the buddy class to the final score 

This actually works. But be aware that your final score will only be updated when you call getFinalScore() ... maybe you should introduce another method to make the update and let getFinalScore() only return the already calculated final score.

When you call this statement:

 userChoice = keyboard.nextLine();

You are assigning that input to the variable userChoice . Then again you call

userChoice = keyboard.nextLine();

Which reassigns the new input to the same variable overriding the previous one. Try changing the name of the second variable, otherwise you're only going to get the same response every time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM