我不太确定在标题中说明问题的更好/更明确的方式,所以请原谅我。
我目前正在尝试做的是根据用户的输入更改变量的值。目前我可以根据输入设置一个值,但是我的程序需要一个循环,用户必须多次输入。
我遇到的问题是,当我调用该类时,它只返回第一个输入的相关数据。因此,当用户可以继续进行不同的输入时,数据永远不会超过第一个输入。
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);
}
}
下面是我的类,它返回数据/值。
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;
}
}
此外,下面是获取每个输入分数和加总/减去总分
的类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;
}
}
任何建议,提示和建议都将非常感激。谢谢。
答案 0 :(得分:1)
我注意到有三件事:
<强> 1)强>
在您的Buddy
课程中,您使用的方法setBuddyScore(int buddyScore)
buddyscore = buddyscore;
而不是
this.buddyscore = buddyscore;
(可能会导致成员变量和参数混淆)
2)(编辑)
在您的主课程中,新userChoice
没有任何反应。也许你可以这样做:
首先更改BuddyScore
类,以便它存储整个Buddy
对象而不是仅存储得分。这样,BuddyScore
对象类知道 Buddy
对象的更改位置。然后,您需要在getBuddyScore()
内调用getFinalScore()
方法,以便始终添加当前分数。
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
}
}
然后回到主要课程中,您需要更新Buddy
对象(在循环内)以始终知道最新的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
这实际上有效。但请注意,只有在致电getFinalScore()
时才会更新您的最终得分...也许您应该引入另一种方法进行更新,并让getFinalScore()
仅返回已计算的最终得分。
答案 1 :(得分:0)
当您致电本声明时:
userChoice = keyboard.nextLine();
您正在将该输入分配给变量userChoice
。然后再打电话
userChoice = keyboard.nextLine();
将新输入重新分配给覆盖前一个变量的同一个变量。尝试更改第二个变量的名称,否则您每次只会得到相同的响应。