Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

java - Variables might not have been initialized

import java.util.Scanner;

public class ZodiacSign{

        public static void main(String args[]){

        Scanner input = new Scanner(System.in);
        int   Feb,March,ZodiacSign,Pisces,Aquarius,Aries,Taurus;

        int selection;
        System.out.println("Feb");

        System.out.println("Feb 1,2,3,4
 Feb 5,6,7,8
 Feb 9,10,11,12
 Feb 13,14,15,16
 Feb 17,18,19,20
 Feb 21,22, 23, 24
 Feb25,26,27,28
 March 1,2,3,4
 March 5,6,7,8
 March 9,10,11,12
 March 13,14,15,16
 March17,18,19,20
 March 21,22,23,24
 March 25,26,27,28
 March 29,30,31");
        System.out.println("Please enter your date of birth");
        System.out.println("Please enter your month of birth");


        selection = input.nextInt();
        if(Feb>= 19){
            ZodiacSign = Pisces ;
            System.out.println("Your zodiac sign is Pisces");
        }   
        else
        {
            System.out.println("Your zodiac sign is Aquarius");
        }
        if(March>=22){
            ZodiacSign = Aries;
            System.out.println("Your zodiac sign is Aries");
        }
        else
        { 
            System.out.println("Your zodiac sign is Taurus");
        }
            System.out.println("End of Program");



        }
}

Its giving me four errors:

ZodiacSign.java:19:error: variable Feb might not have been initialized
                if(Feb>= 19){
                   ^
ZodiacSign.java:20:error: variable Pisces might not have been initialized
                       ZodiacSign= Pisces ;
                                   ^ 
ZodiacSign.java:27:error: variable March might not have been initialized
         if(March>= 22){
            ^
ZodiacSign.java:28:error: variable Aries might not have been initialized
           ZodiacSign= Aries ;
                       ^
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're never assigning a value to Feb, so what do you expect this comparison to achieve?

if (Feb >= 19)

Think about what you really wanted to achieve with that comparison, and then work out what you need to change to make it happen.

In general, you can't read from a local variable before it's definitely assigned - in other words, until the compiler can prove that you'll have been through some execution path which assigns it a value.

However, rather than trying to just make it compile with values assigned at the point of declaration, I would suggest you have a closer think about your overall design. You probably want to change Pisces, Aries etc to be enum values for example.

Additionally, Java code usually uses pascalCase names for local variables.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...