我的程序上有逻辑错误,如果语句有效,但是当用户输入血液型& Rhfactor它仍然显示O+不论uinput
这是第一部分:
package Week5;
class BloodData
{
private String bloodType;
private String rhFactor;
public BloodData()
{
bloodType = "O";
rhFactor = "+";
}
public void setBloodType (String bloodType){
this.bloodType = bloodType;
}
public void setRhFactor (String rhFactor){
this.rhFactor = rhFactor;
}
String getBloodType(){
return this.bloodType;
}
String getRhFactor(){
return this.rhFactor;
}
}
这是主要方法:
package Week5;
import java.util.Scanner;
public class RunBloodData
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
BloodData bd = new BloodData();
System.out.print("Enter blood type of patient : ");
bd.setBloodType(input.nextLine());
System.out.print("Enter the Rhesus factor (+ or -) : ");
bd.setRhFactor(input.nextLine());
if(bd.getBloodType().equals("") && bd.getRhFactor().equals(""))
{
bd = new BloodData();
System.out.println (bd.getBloodType().toUpperCase() + bd.getRhFactor() + " is added to the bloodbank.");
}
else
{
bd = new BloodData();
System.out.println (bd.getBloodType().toUpperCase() + bd.getRhFactor() + " is added to the bloodbank.");
}
}
}
注意:我高度怀疑我必须在新的Blooddata内部放一些东西:
else
{
bd = new BloodData();
我只是不知道考虑到Bloodtype和Rhfactor是私人且不静态的。
Here's the first part:
package Week5;
class BloodData
{
private String bloodType;
private String rhFactor;
public BloodData()
{
bloodType = "O";
rhFactor = "+";
}
public void setBloodType (String bloodType){
this.bloodType = bloodType;
}
public void setRhFactor (String rhFactor){
this.rhFactor = rhFactor;
}
String getBloodType(){
return this.bloodType;
}
String getRhFactor(){
return this.rhFactor;
}
}
here's the main method:
package Week5;
import java.util.Scanner;
public class RunBloodData
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
BloodData bd = new BloodData();
System.out.print("Enter blood type of patient : ");
bd.setBloodType(input.nextLine());
System.out.print("Enter the Rhesus factor (+ or -) : ");
bd.setRhFactor(input.nextLine());
if(bd.getBloodType().equals("") && bd.getRhFactor().equals(""))
{
bd = new BloodData();
System.out.println (bd.getBloodType().toUpperCase() + bd.getRhFactor() + " is added to the bloodbank.");
}
else
{
bd = new BloodData();
System.out.println (bd.getBloodType().toUpperCase() + bd.getRhFactor() + " is added to the bloodbank.");
}
}
}
NOTE: I HIGHLY SUSPECT THAT I HAVE TO PUT SOMETHING INSIDE of new bloodData in:
else
{
bd = new BloodData();
I just don't know what to call considering bloodType and rhFactor are private and non-static.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我修复了它,我只需要在我的其他语句中删除
bd = new Blooddata();
,以便将注册任何输入。I fixed it, I just had to remove
bd = new BloodData();
in my else statement so any input will be registered.