【问题标题】:I can't seem to declare this variable in java我似乎无法在 java 中声明这个变量
【发布时间】:2016-10-14 22:20:17
【问题描述】:

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

我在最后一行收到一个错误,指出 st 未初始化。我错过了一些非常简单的东西吗?非常感谢任何帮助。

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);

    }
}

【问题讨论】:

  • 如果条件都不成立怎么办?那么ts 会是什么?在这里发布您的代码,阅读错误,并去掉那些多余的括号..
  • 不要做 pastebin!
  • 将代码放入问题中。如果它不漂亮,有人会为你清理它
  • "寻求调试帮助的问题 [...] 必须包含 [...] 在问题本身中重现 [问题] 所需的最短代码。 [...] 请参阅:How to create a Minimal, Complete, and Verifiable example 。”
  • 只需将第 65 行更改为 String s = "";,将第 92 行更改为 String t = "";。初始化完成!

标签: java variables if-statement


【解决方案1】:

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

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

应该是

else
    t = ("thirty-first");

同样适用于 s。如果您的变量仅在条件语句中初始化,Java 总是会抱怨,因为它们可能永远不会被初始化。你也可以用

来初始化它们
String s = "";

为了避免这种情况。

【讨论】:

  • 啊,这是你提到的第二件事,非常感谢!
  • 我没有做你首先提到的事情就提交了它。如果数字不是真实的一天,我需要输出一条错误消息,这不是行不通的吗?
  • 那么如果我输入 42 作为我出生的那一天,程序会告诉我我是 31 号出生的吗?
【解决方案2】:

在不检查其余代码的情况下,我想告诉您尝试修复 java 告诉您的错误,即初始化 s & t:例如

String s = "";

在此处阅读有关类似内容的更多信息:

【讨论】:

    【解决方案3】:

    关于您的代码的一些一般指示:

    字符串文字不需要括号。 ("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;
    }
    

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

    【讨论】:

      【解决方案4】:

      您是否希望字符串像原语一样工作?

      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.
      }
      

      【讨论】:

      • 关于答案中的第一段,当变量是方法中的局部变量时,变量(无论它们是基元还是对象引用)都不会被初始化。当它们是对象的字段时,它们总是被初始化。
      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2020-06-14
      • 1970-01-01
      相关资源
      最近更新 更多