如何防止null显示?

时间:2017-05-11 22:35:59

标签: java if-statement null

对不起,我是初学者,所以答案可能很明显,但我正在尝试制作一个代码,它会根据你的dob告诉你你的星座,并根据它告诉你每日,每月和每年的星座运势。

import java.io.*;
public class MysticLady {

static String zodiacSign = "";
static String dailyDescript = "";
private static java.util.Scanner myScanner;

public static void main(String[] args) throws Exception
{
    BufferedReader keyIn = new BufferedReader (new InputStreamReader(System.in));
    Thread.sleep(1000);
    System.out.println("Welcome user to the world of the mystic truths!");
    Thread.sleep(3000);
    System.out.println("My name is Mirela and will be telling you all about you...");
    Thread.sleep(3000);
    System.out.println("But first, what is your name?");

    myScanner = new java.util.Scanner(System.in);
    String name = myScanner.nextLine(); 

    Thread.sleep(1000);
    System.out.println( name + ", what a lovely name");
    Thread.sleep(3000);
    System.out.println("Well " + name + ", I need to first know your date of birth");
    Thread.sleep(3000);
    System.out.println("Keep in mind that by knowing this I will be able to tell you certain things");
    Thread.sleep(3000);
    System.out.println("We currently have daily, monthly, and yearly horoscopes available");
    Thread.sleep(3000);
    System.out.println("Now first tell me what month were you born (1-12)");

    int month = Integer.parseInt(keyIn.readLine());

    System.out.println("Now what day were you born?");

    int day = Integer.parseInt(keyIn.readLine());

    Thread.sleep(1000);

    System.out.println(name + ", the galaxy is telling me that your zodiac sign is " + MysticHoroscope1.getZodiacSign());

    System.out.println("Now that we know your sign, would you like to know your daily, monthyly, or yearly horoscope?");
    String horotype = myScanner.nextLine();
    {
    if (horotype.equalsIgnoreCase("daily"))
        {
        Thread.sleep(1000);
        System.out.println("Today is May 11, 2017");
        System.out.println("Your horoscope for today is...");
        System.out.println("" + MysticHoroscope1.getDailyDescript());
    if (horotype.equalsIgnoreCase("monthly"))
        {System.out.println("");
    if (horotype.equalsIgnoreCase("yearly"))        
        {System.out.println("");
    }   
}}}}}


public class MysticHoroscope1 { 
private static String zodiacSign;

int month;
int day;
    public MysticHoroscope1(int m, int d)
    {

    int month = m;
    int day = d;
    switch (month)
    {
    case 1 :
    if (day > 0 && day <= 20)
    setZodiacSign("Capricorn");
    else if (day > 20 && day < 32)
    setZodiacSign("Aquarius");
    else
    setZodiacSign("Sorry, that's not a valid date");
    break;

    case 2 :
    if (day > 0 && day <= 19)
    setZodiacSign("Aquarius");
    else if (day > 19 && day < 30)
    setZodiacSign("Pisces");
    else
    setZodiacSign("Sorry, that's not a valid date");
    break;

    case 3 :
    if (day > 0 && day <= 20)
    setZodiacSign("Pisces");
    else if (day > 20 && day < 32)
    setZodiacSign("Aries");
    else
    setZodiacSign("Sorry, that's not a valid date");
    break;
    case 4 :
    if (day > 0 && day <= 20)
    setZodiacSign("Aries");
    else if (day > 20 && day < 31)
    setZodiacSign("Taurus");
    else
    setZodiacSign("Sorry, that's not a valid date");
    break;
    case 5 :
    if (day > 0 && day <= 21)
    setZodiacSign("Taurus");
    else if (day > 21 && day < 32)
    setZodiacSign("Gemini");
    else
    setZodiacSign("Sorry, that's not a valid date");
    break;
    case 6 :
    if (day > 0 && day <= 21)
    setZodiacSign("Gemini");
    else if (day > 21 && day < 31)
    setZodiacSign("cancer");
    else
    setZodiacSign("Sorry, that's not a valid date");
    break;
    case 7 :
    if (day > 0 && day <= 22)
    setZodiacSign("Cancer");
    else if (day > 22 && day < 32)
    setZodiacSign("Leo");
    else
    setZodiacSign("Sorry, that's not a valid date");
    break;

    case 8 :
    if (day > 0 && day <= 21)
    setZodiacSign("Leo");
    else if (day > 21 && day < 32)
    setZodiacSign("Virgo");
    else
    setZodiacSign("Sorry, that's not a valid date");
    break;

    case 9 :
    if (day > 0 && day <= 23)
    setZodiacSign("Virgo");
    else if (day > 23 && day < 31)
    setZodiacSign("Libra");
    else
    setZodiacSign("Sorry, that's not a valid date");
    break;

    case 10 :
    if (day > 0 && day <= 23)
    setZodiacSign("Libra");
    else if (day > 23 && day < 32)
    setZodiacSign("Scorpio");
    else
    setZodiacSign("Sorry, that's not a valid date");
    break;

    case 11 :
    if (day > 0 && day <= 22)
    setZodiacSign("Scorpio");
    else if (day > 22 && day < 31)
    setZodiacSign("Sagittarius");
    else
    setZodiacSign("Sorry, that's not a valid date");
    break;
    case 12 :
    if (day > 0 && day <= 22)
    setZodiacSign("Sagittarius");
    else if (day > 22 && day < 32)
    setZodiacSign("Capricorn");
    else
    setZodiacSign("Sorry, that's not a valid date");
    break;
    default :
    setZodiacSign("Sorry, that's not a valid date");
    }

    }

public void MysticHoroscope(int month2, int day2) {
    }

public static String getZodiacSign() {
    return zodiacSign;
}

public static void setZodiacSign(String zodiacSign) {
    MysticHoroscope1.zodiacSign = zodiacSign;
}
    public static String dailyDescript;
    {
        if (zodiacSign.equals("Capricorn"))     
            setDailyDescript("There are many ways to journey through life,\nbut you will only know the path you choose to travel.\nThis is a wonderful day to expose yourself to new art,\nespecially if it's from a different culture.\nTurn the dial on your radio or television to a foreign-language broadcast.\nLeave it there for a few minutes. You'll be surprised how intriguing you find it.\nEvery now and then, it's wise to travel down an unfamiliar side street.");
        if (zodiacSign.equals("Aquarius"));
            setDailyDescript("Stand up for yourself and watch how quickly people back down.\nToday, you will be able to talk to absolutely anyone--even the folks who usually intimidate you.\nIt's a great day to ask for a raise, try to get some face time with the big boss, or ask someone out on a date.\nYou'll give off an unmistakable air of confidence, which might be a bit stronger than what you actually feel.\nThis level of boldness could open up some very important doors for you.");
        if (zodiacSign.equals("Pisces"));
            setDailyDescript("Finding a balance between what you want and what is best for everyone involved is important today.\nBefore making any decisions, you have to factor in other people's feelings and goals.\nThis could be difficult, because you can't exactly read minds.\nLuckily, your intuition is strong, and you can sense what people want.\nDon't bend too far backward to accommodate them, though.\nJust let them know you considered their feelings -- that's plenty of incentive.");
    }
    public static String getDailyDescript() {
    return dailyDescript;
    }
    private static void setDailyDescript(String dailyDescript) {
    MysticHoroscope1.dailyDescript = dailyDescript;
}



}

除了实际答案外,一切都“有效”。

name, the galaxy is telling me that your zodiac sign is null
Now that we know your sign, would you like to know your daily, monthyly, or yearly horoscope?
daily
Today is May 11, 2017
Your horoscope for today is...
null

2 个答案:

答案 0 :(得分:1)

问题包括:

  • 您在实例初始值设定项中设置了类变量dailyDescript。您想要一个实例成员。从声明中删除static
  • 在调用setZodiacSign()之前调用初始化程序,然后再也不会。您希望移动该代码以便在setZodiacSign()之后调用它,或者在构造函数中初始化星座和描述。
  • 当您移动该代码以允许从setZodiacSign()调用它时,您需要覆盖该代码中的所有星座,理想情况下,提供处理错误值的最终else以某种方式。
  • 您在包含main()的类中包含全局变量,既不需要也不使用。

答案 1 :(得分:0)

我尝试尽可能少地修改你的代码以获得答案(即使我身体中的所有内容都在努力反对)。

您的班级MysticLady

你有:

Thread.sleep(1000);

System.out.println(name + ", the galaxy is telling me that your zodiac sign is " + MysticHoroscope1.getZodiacSign());

问题:

  1. MysticHoroscope1包含构造函数中的horoscopy逻辑,但永远不会调用它。
  2. 解决方案。只需在其间添加一行:

    Thread.sleep(1000);
    
    new MysticHoroscope1(month, day);
    
    System.out.println(name + ", the galaxy is telling me that your zodiac sign is " + MysticHoroscope1.getZodiacSign());
    

    班级MysticHoroscope1包含

    下一课MysticHoroscope1

    你有:

    public static void setZodiacSign(String zodiacSign) {
        MysticHoroscope1.zodiacSign = zodiacSign;
    }
    public static String dailyDescript;
    {
        if (zodiacSign.equals("Capricorn"))
        setDailyDescript("There are many ways to journey through life,\nbut you will only know the path you choose to travel.\nThis is a wonderful day to expose yourself to new art,\nespecially if it's from a different culture.\nTurn the dial on your radio or television to a foreign-language broadcast.\nLeave it there for a few minutes. You'll be surprised how intriguing you find it.\nEvery now and then, it's wise to travel down an unfamiliar side street.");
        if (zodiacSign.equals("Aquarius"));
        setDailyDescript("Stand up for yourself and watch how quickly people back down.\nToday, you will be able to talk to absolutely anyone--even the folks who usually intimidate you.\nIt's a great day to ask for a raise, try to get some face time with the big boss, or ask someone out on a date.\nYou'll give off an unmistakable air of confidence, which might be a bit stronger than what you actually feel.\nThis level of boldness could open up some very important doors for you.");
        if (zodiacSign.equals("Pisces"));
        setDailyDescript("Finding a balance between what you want and what is best for everyone involved is important today.\nBefore making any decisions, you have to factor in other people's feelings and goals.\nThis could be difficult, because you can't exactly read minds.\nLuckily, your intuition is strong, and you can sense what people want.\nDon't bend too far backward to accommodate them, though.\nJust let them know you considered their feelings -- that's plenty of incentive.");
    }
    

    问题:

    1. 当实例化类并且取决于zodiacSign时,星座的描述在匿名块中初始化。但zodiacSign在构造函数中初始化(实际上在评估匿名块之后)
    2. if - 语句以冒号结尾
    3. 解决方案。制作此:

      public static void setZodiacSign(String zodiacSign) {
          MysticHoroscope1.zodiacSign = zodiacSign;
      
          if (zodiacSign.equals("Capricorn"))
              setDailyDescript("There are many ways to journey through life,\nbut you will only know the path you choose to travel.\nThis is a wonderful day to expose yourself to new art,\nespecially if it's from a different culture.\nTurn the dial on your radio or television to a foreign-language broadcast.\nLeave it there for a few minutes. You'll be surprised how intriguing you find it.\nEvery now and then, it's wise to travel down an unfamiliar side street.");
          if (zodiacSign.equals("Aquarius"))
              setDailyDescript("Stand up for yourself and watch how quickly people back down.\nToday, you will be able to talk to absolutely anyone--even the folks who usually intimidate you.\nIt's a great day to ask for a raise, try to get some face time with the big boss, or ask someone out on a date.\nYou'll give off an unmistakable air of confidence, which might be a bit stronger than what you actually feel.\nThis level of boldness could open up some very important doors for you.");
          if (zodiacSign.equals("Pisces"))
              setDailyDescript("Finding a balance between what you want and what is best for everyone involved is important today.\nBefore making any decisions, you have to factor in other people's feelings and goals.\nThis could be difficult, because you can't exactly read minds.\nLuckily, your intuition is strong, and you can sense what people want.\nDon't bend too far backward to accommodate them, though.\nJust let them know you considered their feelings -- that's plenty of incentive.");
      }
      public static String dailyDescript;
      

      我强烈建议您重新考虑您的设计。