我似乎无法在java中声明这个变量

时间:2016-10-14 22:20:17

标签: java variables if-statement

我必须进行星座运算,在此过程中我选择声明一个字符串,然后在if语句中将其设置为相等。

我在最后一行收到错误,说明s& t未初始化。我错过了一些非常简单的事吗?非常感谢任何帮助。

import java.util.Scanner;

class main {
    public static void main(String[] args)
     {

     Scanner scan = new Scanner(System.in);

    System.out.println("What day of the month were you born? (number)");
    int d = scan.nextInt();
    System.out.println("Which month were you born? (number)");
    int m = scan.nextInt();

    if (m==3 && d>=21 && d<=31)
      System.out.println("Your sign is Aries");
    else if (m==4 && d<=19 && d>=1)
      System.out.println("Your sign is Aries");
    else if (m==4 && d>=20 && d<=31)
      System.out.println("Your sign is Taurus");
    else if (m==5 && d<=20 && d>=1)
      System.out.println("Your sign is Taurus");
    else if (m==5 && d>=21 && d<=31)
      System.out.println("Your sign is Gemini");
    else if (m==6 && d>=1 && d<=20)
      System.out.println("Your sign is Gemini");
    else if (m==6 && d>=21 && d<=31)
      System.out.println("Your sign is Cancer");
    else if (m==7 && d>=1 && d<=22)
      System.out.println("Your sign is Cancer");
    else if (m==7 && d>=23 && d<=31)
      System.out.println("Your sign is Leo");
    else if (m==8 && d>=1 && d<=22)
      System.out.println("Your sign is Leo");
    else if (m==8 && d>=23 && d<=31)
      System.out.println("Your sign is Virgo");
    else if (m==9 && d>=1 && d<=22)
      System.out.println("Your sign is Virgo");
    else if (m==9 && d>=23 && d<=31)
      System.out.println("Your sign is Libra");
    else if (m==10 && d>=1 && d<=22)
      System.out.println("Your sign is Libra");
    else if (m==10 && d>=23 && d<=31)
      System.out.println("Your sign is Scorpio");
    else if (m==11 && d>=1 && d<=21)
      System.out.println("Your sign is Scorpio");
    else if (m==11 && d>=22 && d<=31)
      System.out.println("Your sign is Sagittarius");
    else if (m==12 && d>=1 && d<=21)
      System.out.println("Your sign is Sagittarius");
    else if (m==12 && d>=22 && d<=31)
      System.out.println("Your sign is Capricorn");
    else if (m==1 && d>=1 && d<=19)
      System.out.println("Your sign is Capricorn");
    else if (m==1 && d>=20 && d<=31)
      System.out.println("Your sign is Aquarius");
    else if (m==2 && d>=1 && d<=18)
      System.out.println("Your sign is Aquarius");
    else if (m==2 && d>=19 && d<=31)
      System.out.println("Your sign is Pisces");
    else if (m==3 && d>=1 && d<=20)
      System.out.println("Your sign is Pisces");
    else
      System.out.println("error");

    String s;

    if (m==1)
      s=("January");
    else if (m==2)
      s=("February");
    else if (m==3)
      s=("March");
    else if (m==4)
      s=("April");
    else if (m==5)
      s=("May");
    else if (m==6)
      s=("June");
    else if (m==7)
      s=("July");
    else if (m==8)
      s=("August");
    else if (m==9)
      s=("September");
    else if (m==10)
      s=("October");
    else if (m==11)
      s=("November");
    else if (m==12)
      s=("December");

     String t;

    if (d==1)
      t=("first");
    else if (d==2)
      t=("second");
    else if (d==3)
      t=("third");
    else if (d==4)
      t=("fourth");
    else if (d==5)
      t=("fifth");
    else if (d==6)
      t=("sixth");
    else if (d==7)
      t=("seventh");
    else if (d==8)
      t=("eighth");
    else if (d==9)
      t=("ninth");
    else if (d==10)
      t=("tenth");
    else if (d==11)
      t=("eleventh");
    else if (d==12)
      t=("twelfth");
    else if (d==13)
      t=("thirteenth");
    else if (d==14)
      t=("fourteenth");
    else if (d==15)
      t=("fifteenth");
    else if (d==16)
      t=("sixteenth");
    else if (d==17)
      t=("seventeenth");
    else if (d==18)
      t=("eighteenth");
    else if (d==19)
      t=("nineteenth");
    else if (d==20)
      t=("twentieth");
    else if (d==21)
      t=("twenty-first");
    else if (d==22)
      t=("twenty-second");
    else if (d==23)
      t=("twenty-third");
    else if (d==24)
      t=("twenty-fourth");
    else if (d==25)
      t=("twenty-fifth");
    else if (d==26)
      t=("twenty-sixth");
    else if (d==27)
      t=("twenty-seventh");
    else if (d==28)
      t=("twenty-eighth");
    else if (d==29)
      t=("twenty-ninth");
    else if (d==30)
      t=("thirtieth");
    else if (d==31)
      t=("thirty-first");

    System.out.println("Your birthday is: " + s + " " + t);

    }
}

4 个答案:

答案 0 :(得分:0)

您的问题来自您的if-elseif代码长串。你在哪里

else if (d==31)
    t=("thirty-first");

应该是

else
    t = ("thirty-first");

和s类似。如果您的变量仅在条件语句中初始化,Java将始终抱怨,因为它们可能永远不会被初始化。您也可以使用

初始化它们
String s = "";

避免这种情况。

答案 1 :(得分:0)

不检查剩下的代码我想告诉你尝试修复java告诉你的错误,即初始化s&amp; t:例如

String s = "";

在这里阅读有关类似内容的更多信息:

答案 2 :(得分:0)

有关您的代码的一些常规指示:

不需要围绕字符串文字进行限制。 ("first")可以替换为"first"

if-else的长链难以阅读,被认为是坏的。 switch会更好,但实际上我会选择Map来解决日期和月份。

您的代码应该拆分为方法,每个方法负责一个“事物”。这里至少有4件事:(1)日和月输入,(2)符号计算,(3)编号月份转换为文本,(4)编号日转换为文本。

例如,这是(3)的方法:

private String getMonthAsString(final Integer monthAsNumber) {
    return createMonthNumberToMonthStringMap().get(monthAsNumber);
}

private Map<Integer, String> createMonthNumberToMonthStringMap() {
    final Map<Integer, String> monthNumberToMonthString = new HashMap<>();
    monthNumberToMonthString.put(1, "January");
    monthNumberToMonthString.put(2, "February");
    monthNumberToMonthString.put(3, "March");
    monthNumberToMonthString.put(4, "April");
    monthNumberToMonthString.put(5, "May");
    monthNumberToMonthString.put(6, "June");
    monthNumberToMonthString.put(7, "July");
    monthNumberToMonthString.put(8, "August");
    monthNumberToMonthString.put(9, "September");
    monthNumberToMonthString.put(10, "October");
    monthNumberToMonthString.put(11, "November");
    monthNumberToMonthString.put(12, "Desember");

    return monthNumberToMonthString;
}

如果向getMonthAsString发送了无效的数字(-1,0,13等),它将返回null。在这些情况下,您应该抛出异常,或向用户显示详细说明问题的错误消息。

答案 3 :(得分:-1)

你期待Strings像原语一样工作吗?

public static void main(String ... args){
  int myInt; //implicitly initialized to 0
  String myString; //not implicitly initialized; objects don't have default values
  myInt=myInt+1; //works! myInt now is one
  myString=yString+"test"; //compilier error, myString was never initiallized 
}

编辑,看到发布的代码:

请注意,编译器足够聪明,可以知道代码中有一种方法,其中变量未初始化:

public static void main(String ... args){
  String myString; //not implicitly initialized
  Scanner scan = new Scanner(System.in);
  int d = scan.nextInt();
  if(d>100){
    myString="hello big world!";
  }else if(d<10){
    myString="hello little world!";
  }
  System.out.println(myString); //error, the compiler knows that myString is uninitialized in some cases.
}

尝试这样的事情:

public static void main(String ... args){
  String myString; //not implicitly initialized
  Scanner scan = new Scanner(System.in);
  int d = scan.nextInt();
  if(d>100){
    myString="hello big world!";
  }else if(d<10){
    myString="hello little world!";
  }else{
    myString="hello world!";
  }
  System.out.println(myString); //works! the compiler knows that there is no way through that if block without myString getting set.
}