簡體   English   中英

如何防止顯示null?

[英]How can I prevent null from showing up?

抱歉,我是一個初學者,所以答案可能很明顯,但是我正在嘗試編寫代碼,在該代碼中,您將基於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

問題包括:

  • 您正在實例初始化器中設置類變量dailyDescript 您需要一個實例成員。 從聲明中刪除static
  • 將在調用setZodiacSign()之前調用該初始化程序,然后再也不會調用它。 您可能想要移動該代碼,以便可以在setZodiacSign()之后調用它,或者在構造函數中初始化黃道帶符號和描述。
  • 當您移動代碼,允許從調用它setZodiacSign()你需要覆蓋代碼的所有星座,最好,提供最終else以某種方式處理錯誤值。
  • 您的類中包含main()全局變量既不需要也不使用。

我嘗試盡可能少地修改您的代碼以獲得答案(即使我體內的一切都與之抗爭)。

您的班級MysticLady

你有:

Thread.sleep(1000);

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

問題:

  1. MysticHoroscope1包含構造函數中的占星邏輯,但從未調用過它。

解。 只需在兩者之間添加一行:

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 -statements用冒號結束

解。 做這個:

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;

我強烈建議您重新考慮您的設計。

zodiacSign在您的程序中保存null的值,因此它就是打印的內容。 我建議您檢查是否為空以消除該問題。

您的null檢查應如下所示:以前是相同的,只是未評論

if (zodiacSign!=null && !zodiacSign.isEmpty) {
    //do your stuff here...
} else {
    //what you want to do if get null zodiacSign
}

這不是問題,但是我建議您嘗試對代碼進行格式化,以提高可讀性,因為它是交錯的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM