Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Java
Public class Main
{
  Public static void main (String [] args)
{
  Int num;
  System.out.println("Enter any numberequal to or between 1-12 to display the month");
switch (num)
    {
    case 1:
        System.out.println ("The name of month number 1 is January");
        break;
    case 2:
        System.out.println ("The name of month number 2 is February");
        break;
    case 3:
        System.out.println ("The name of month number 3 is March");
        break;
    case 4:
        System.out.println ("The name of month number 4 is April");
        break;
    case 5:
        System.out.println ("The name of month number 5 is May");
        break;
    case 6:
        System.out.println ("The name of month number 6 is June");
        break;
    case 7:
        System.out.println ("The name of month number 7 is July");
        break;
    case 8:
        System.out.println ("The name of month number 8 is August");
        break;
    case 9:
        System.out.println ("The name of month number 9 is September");
        break;
    case 10:
        System.out.println ("The name of month number 10 is October");
        break;
    case 11:
        System.out.println ("The name of month number 11 is November");
        break;
    case 12:
        System.out.println ("The name of month number 12 is December");
        break;
        default:
            System.out.println ("You have entered an invalid number");
        }
    } // main method
}<


What I have tried:

Main.java:9: error: variable num might not have been initialized
switch (num)
^
What I have to add to this code
Posted
Updated 2-Dec-20 4:01am
v2
Comments
Richard MacCutchan 2-Dec-20 9:29am    
This is the third Java question today, all tagged as C. Please use the correct language tags for your questions.
OriginalGriff 2-Dec-20 10:07am    
This is the third question today, all trivial syntax errors: you need to learn to fix these yourself - it will save you a lot of time if you do ...
KarstenK 2-Dec-20 12:30pm    
only have to read the compiler message carefully and think about it. It is my daily bread over 20 years in coding ;-)

See your code
Java
Int num;
  System.out.println("Enter any numberequal to or between 1-12 to display the month");
switch (num)
You have declared numwith
Java
Int num;
You then output a message to the user with
Java
System.out.println("Enter any numberequal to or between 1-12 to display the month");
But then the next thing you do is to try and use num
Java
switch (num)
You need to insert a line to get the input from the user into the variable nume.g. (not tested!)
Java
scanf("%d", &num);


I've just seen some of your other questions in this forum, this is not a good way to learn the C language (or any language) - get yourself a good book and work through it rather than asking questions here every time you hit a compiler error. Or have a read of this How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
v2
Comments
CHill60 2-Dec-20 9:43am    
I am sure of what you have to do - you need to insert one or more lines of code that will accept input from the user. Richard has given you a link to a tutorial that will show you how to do that in Java
Richard MacCutchan 2-Dec-20 9:30am    
The code is Java, so definitely not a good way to learn C. :)
CHill60 2-Dec-20 9:41am    
LOL! I actually put the java pre tags in as well. I'm deffo going senile.
You will learn Java quicker by studying The Java™ Tutorials[^].
 
Share this answer
 
C++
Public class Main
{
  Public static void main (String [] args)
{
  // beware, variables types are lowercase, aka int, not Int
  Int num; // here you declare num without value
  System.out.println("Enter any numberequal to or between 1-12 to display the month");
switch (num) // here you use num, but where did you gave it a value ?
    {
    case 1:
        System.out.println ("The name of month number 1 is January");
        break;
 
Share this answer
 
Comments
Richard MacCutchan 2-Dec-20 10:07am    
Look at the other questions OP posted today. Obviously never actually looks at what they have written, even when the error message tells them where to look.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900