简体   繁体   中英

Cannot be resolved to a variable println

I get this error: Exception in thread "main" java.lang.Error: Unresolved compilation problem: studentName cannot be resolved to a variable

I don't know what's wrong or how to fix it. The error is at the bottom of the code, the second println System.out.println(studentName); . Any help would be wonderful.

import java.util.Scanner;

public class allDone {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String studentNames = "";
        String studentScores = "";
        
        while (true){
            //Prompt user to enter Student Name.
            System.out.println("Please Enter StudentName or If Finished Enter alldone:");
            String studentName = in.nextLine();

            if (studentName.equals("alldone")){
                break;
            }
            
            studentNames = studentNames + studentName +";";                          

            System.out.println("Please enter student's score:");

            String studentScore = in.nextLine();

            studentScores = studentScores + studentScore + ";";
        }
        
        String studentNamesArry[] = studentNames.split(";");
        int studentScoresArry[] = getStudentArry(studentScores.split(";"));
        displayHighestScore(studentNamesArry,studentScoresArry);
    }
    
    public static int[] getStudentArry(String[] stringArray){
        int[] studentScoresArry = new int [stringArray.length];
        int i = 0;
        for (String str: stringArray){
            studentScoresArry[i] = Integer.parseInt(str.trim());
            i++;
        }
            
        return studentScoresArry;
    }
        
    public static void displayHighestScore(String studentNamesArry[],int studentScoresArry[]){
        int studentScore = studentScoresArry[0];
                
        for (int i = 1; i < studentScoresArry.length; i++){
            if (studentScoresArry[i] > studentScore){
                studentScore = studentScoresArry[i];
                String studentName = studentNamesArry[i];
            }
        }
        System.out.println("nHighestScore:");
        System.out.println(studentName);
        System.out.println(studentScore);
    }
}

Does this fix your problem?

    public static void displayHighestScore(String studentNamesArry[],int studentScoresArry[]){
        if(studentNamesArry.length > 0 && studentScoresArry.length > 0){
            int studentScore = studentScoresArry[0];
            String studentName = studentNamesArry[0];
                
            for (int i = 1; i < studentScoresArry.length; i++){
                if (studentScoresArry[i] > studentScore){
                    studentScore = studentScoresArry[i];
                    studentName = studentNamesArry[i];
                }
            }
            System.out.println("nHighestScore:");
            System.out.println(studentName);
            System.out.println(studentScore);
        }
    }

Note: I added an if statement to check that the arrays were not empty.

you just need to initialize studentName earlier because it might not be initialized because of the if statement

public static void displayHighestScore(String studentNamesArry[],int studentScoresArry[])
                        {
                    int studentScore = studentScoresArry[0];
                    String studentName="";
                    for (int i = 1; i < studentScoresArry.length; i++)
                            {
                        if (studentScoresArry[i] > studentScore)
                                {
                            studentScore = studentScoresArry[i];
                             studentName = studentNamesArry[i];
                                }
                            }
                    System.out.println("nHighestScore:");
                    System.out.println(studentName);
                    System.out.println(studentScore);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM