使用开关错误编写程序

时间:2015-09-09 17:30:14

标签: java switch-statement

请检查为什么我的“stringYear”似乎尚未初始化?每次我在jcreator中构建文件时,都会出现1个错误,它表示 - 变量yearString可能尚未初始化。

指令是获取输入(年份代码1 - 4)并将其输出为1表示第一年和新生,2表示第二年和大二,3表示第3年和初级,4表示第4年和高级。

以下代码。

import java.util.Scanner;
import java.io.*;

public class yearSWITCH {

    public static void main(String[] a) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter year code: ");  
    System.out.println("");

        int year = 4;
        String yearString;
        switch (year) {
            case 1: yearString = "First Year";
                    yearString = "Freshmen";
                    break;
            case 2: yearString = "Second Year";
                    yearString = "Sophomore";
                    break;
            case 3: yearString = "Third Year";
                    yearString = "Junior";
                    break;
            case 4: yearString = "Fourth Year";
                    yearString = "Senior";
                    break;
        }
        System.out.println(yearString);
    }
}

2 个答案:

答案 0 :(得分:1)

您必须使用任何值初始化fileRD = open('csv_file.csv', 'r') # Input file fileST = open('states.csv', 'r') # States mapping file fileWT = open('Output.csv', 'wb') #Output file to write to reader_RD = csv.reader(fileRD) reader_ST = csv.reader(fileST) writer = csv.writer(fileWT) ,对于您的情况,如果您按以下方式初始化String,它将正常工作:

String

但请注意,String yearString = ""; //String is empty 中的第二个语句覆盖变量any case (1,2,3,4)的所有先前值,因此,唯一的输出yearString print语句中的particular case将是高级。解决此问题的方法是初始化2 Strings并执行以下更改:

String yearString1 = "";
String yearString2 = "";
    switch (year) {
        case 1: yearString1 = "First Year";
                yearString2 = "Freshmen";
                break;
        case 2: yearString1 = "Second Year";
                yearString2 = "Sophomore";
                break;
        case 3: yearString1 = "Third Year";
                yearString2 = "Junior";
                break;
        case 4: yearString1 = "Fourth Year";
                yearString2 = "Senior";
                break;
        default:
                System.out.println("Invalid Input");
                break;
    }
    System.out.println(yearString1 + "\n " +yearString2);

答案 1 :(得分:1)

请找到以下代码..我认为这将清除您的疑问。在编写代码时,请使用IDE。 IDE将帮助您找到错误。

代码是:

package com.raju;

import java.util.Scanner;


public class YearCheck {

    public static void main(String[] a) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter year code: ");  

        int year = keyboard.nextInt();
        String yearString="";
        switch (year) {
            case 1: yearString = "First Year";
                    yearString = "Freshmen";
                    break;
            case 2: yearString = "Second Year";
                    yearString = "Sophomore";
                    break;
            case 3: yearString = "Third Year";
                    yearString = "Junior";
                    break;
            case 4: yearString = "Fourth Year";
                    yearString = "Senior";
                    break;
            default:
                System.out.println("you have entered the value greater than 4");
                break;
        }
        System.out.println(yearString);
    }
}