Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么可以';字符串变量不能在此程序中打印吗?_Java - Fatal编程技术网

Java 为什么可以';字符串变量不能在此程序中打印吗?

Java 为什么可以';字符串变量不能在此程序中打印吗?,java,Java,此代码用于将月份更改为相应的字母代码 这是我迄今为止编写的代码: public static void main (String[ ] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month number. [1..12] --> "); int month = input.nextInt(); String MonthString; swi

此代码用于将月份更改为相应的字母代码

这是我迄今为止编写的代码:

public static void main (String[ ] args) 

  {
  Scanner input = new Scanner(System.in); 
  System.out.print("Enter month number. [1..12] --> ");
  int month = input.nextInt();
  String MonthString;
  switch (month)
  {
   case 1 : MonthString = "ZS"; break;
   case 2 : MonthString = "CN"; break;
   case 3 : MonthString = "YH"; break;
   case 4 : MonthString = "MT"; break;
   case 5 : MonthString = "CL"; break;
   case 6 : MonthString = "SS"; break;
   case 7 : MonthString = "WM"; break;
   case 8 : MonthString = "WY"; break;
   case 9 : MonthString = "SH"; break;
   case 10 : MonthString = "YJ"; break;
   case 11 : MonthString = "XG"; break;
   case 12 : MonthString = "HZ"; break;
   default : System.out.print("This is not a valid month number.");
  }
  System.out.println(MonthString);/*This is where it won't compile*/
}
Enter month number. [1..12] --> 1
ZS

您需要先初始化
MonthString

String MonthString = null;

您需要在
String monthString=“”)上添加一些内容第一个

注意变量引导中的小写和大写字母:

初始化变量时,应按如下方式初始化:

String firstExample=“Hello world”和非:
stringfirstexample=“helloworld”,类以大写字母命名

public static void main (String[ ] args)

      {
      Scanner input = new Scanner(System.in); 
      System.out.print("Enter month number. [1..12] --> ");
      int month = input.nextInt();
      String monthString = "";
      switch (month)
      {
       case 1 : monthString = "ZS"; break;
       case 2 : monthString = "CN"; break;
       case 3 : monthString = "YH"; break;
       case 4 : monthString = "MT"; break;
       case 5 : monthString = "CL"; break;
       case 6 : monthString = "SS"; break;
       case 7 : monthString = "WM"; break;
       case 8 : monthString = "WY"; break;
       case 9 : monthString = "SH"; break;
       case 10 : monthString = "YJ"; break;
       case 11 : monthString = "XG"; break;
       case 12 : monthString = "HZ"; break;
       default : System.out.print("This is not a valid month number.");
      }
      System.out.println(monthString);
    }
}
输出:

public static void main (String[ ] args) 

  {
  Scanner input = new Scanner(System.in); 
  System.out.print("Enter month number. [1..12] --> ");
  int month = input.nextInt();
  String MonthString;
  switch (month)
  {
   case 1 : MonthString = "ZS"; break;
   case 2 : MonthString = "CN"; break;
   case 3 : MonthString = "YH"; break;
   case 4 : MonthString = "MT"; break;
   case 5 : MonthString = "CL"; break;
   case 6 : MonthString = "SS"; break;
   case 7 : MonthString = "WM"; break;
   case 8 : MonthString = "WY"; break;
   case 9 : MonthString = "SH"; break;
   case 10 : MonthString = "YJ"; break;
   case 11 : MonthString = "XG"; break;
   case 12 : MonthString = "HZ"; break;
   default : System.out.print("This is not a valid month number.");
  }
  System.out.println(MonthString);/*This is where it won't compile*/
}
Enter month number. [1..12] --> 1
ZS

您需要在switch语句的默认值中指定该值,而不是将其打印出来,因为它将在switch/case结束后打印。

变量名应以小写开头,这样它们就不会与以大写开头的类名混淆。默认的case不会将
MonthString
指定给值。如果号码错了怎么办?如果未初始化MonthString,则最后一条语句是错误的。要么在开关之前分配它,要么在默认情况下分配它,要么抛出异常。它将编译,但不会像intended@RandomCoder_01为什么?他有2个system.out.println(MonthString)。因此,输出将是“这不是一个有效的数字”,后跟“Null”。这没什么大不了的,但真正的问题是他是如何使用他的默认声明的。