英文:
Why can't the String variable be printed in this program?
问题
以下是我翻译好的部分:
这段代码的目的是将月份转换为相应的字母代码。
**以下是我迄今为止编写的代码:**
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("输入月份数字。[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("这不是一个有效的月份数字。");
}
System.out.println(MonthString);/*这是无法编译的地方*/
}
英文:
This code is for the purpose of changing months to the corresponding letter codes.
Here is my code I wrote so far:
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*/
}
答案1
得分: 2
你需要先初始化MonthString
:
String MonthString = null;
英文:
You need to initialize MonthString
first:
String MonthString = null;
答案2
得分: 2
默认情况下:MonthString = "This is not a valid number";
你需要在 switch 语句的默认情况下分配该值,而不是打印它,因为它将在 switch/case 结束后被打印出来。
英文:
default: MonthString = "This is not a valid number";
you need to assign the value in the default of your switch statement, not print it out since it's going to get printed after the switch/case is over.
答案3
得分: 0
你需要首先放置一些内容在 String monthString = "";
。
注意变量 "boot" 中的小写和大写字母:
当你初始化一个变量时,应该按照以下方式进行初始化:
String firstExample = "Hello world";
而不是:String FirstExample = "Hello world";
,类名使用大写字母命名。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("输入月份数字。[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("这不是一个有效的月份数字。");
}
System.out.println(monthString);
}
输出:
输入月份数字。[1..12] --> 1
ZS
英文:
You need to put something on String monthString = "";
first.
Note lowercase and uppercase letters in a variable boot :
When you initialize a variable it should be initialized as follows:
String firstExample= "Hello world";
and not : String FirstExample= "Hello world";
, Classes are given names with capital letters.
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);
}
}
Output :
Enter month number. [1..12] --> 1
ZS
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论