简体   繁体   中英

Nothing shows up on output screen

I'm trying to do a really simple code, but whenever I try to make it print the first statement to get an input information from the user, nothing shows up on the output screen.

Here's the code:

public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    int grade[] = new int[3];

    for(int i = 0; i < grade[i]; grade[i]++){
    System.out.println("Input the student's degree");
    grade[i] = sc.nextInt();
        if(grade[i] < 10 ){
            grade[i] += 0.5;
        }
        System.out.println(grade[i]);
    }       
}

I can't really see the problem here. If anyone is wondering, I am using Eclipse Neon 0.2 IDE. Thanks for reading

grade[] is empty, so the for-loop never runs. An int-array is initialized with 0's.

The Value of grade[i ] is zero as has not been set. Thus you are not entering the loop.

 for(int i = 0; i < grade[i]; grade[i]++){
 //                   ^^^^^^^^ here 

Try this:

for(int i = 0; i < 3; i++){

This will help you understand:

    int grade[] = new int[3];

Add this line under the above code.

    System.out.print(grade[0]); 

Grade array is empty. It has 0 in all indices. grade[0]=0, grade[1]=0 and so on... Therefore the loop never runs and nothing happens.

maybe like this:

public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int grade[] = new int[3];

for(int i = 0; i < grade.length; i++){
    System.out.println("Input the student's degree");
    grade[i] = sc.nextInt();
    if(grade[i] < 10 ){
        grade[i] += 0.5;
    }
    System.out.println(grade[i]);
}       

}

Try with this before for loop

Scanner sc = new Scanner(System.in);
        int grade[] = new int[3];
        System.out.println("Input the student's degree");
        grade[0] = sc.nextInt();
        grade[1] = sc.nextInt();
        grade[2] = sc.nextInt();

you must rewrite your code, this is a sample code similar to yours with some modifications.

import java.util.Scanner; // import scanner package
public class demo {
    public static void main(String args[]){

            // create  a scanner
            Scanner sc = new Scanner(System.in);

            // double data type array
            double grade[] = new double[3]; 

            for(int i = 0; i < grade.length; i++){
            System.out.println("Input the student's degree");

            // input double data type value
            grade[i] = sc.nextDouble();
                if(grade[i] < 10 ){
                    grade[i] += 0.5;
                }
                System.out.println(grade[i]);
            }       
    }
}

here is your code with a little modifications.

import java.util.Scanner; // import scanner package
public class demo{
    public static void main(String args[]){

    // create a scanner
    Scanner sc = new Scanner(System.in);


    //create a double data type array
    double grade[] = new double[3];

    // create a for loop with modified condition
    for(int i = 0; i <= grade[i]; grade[i]++){

    // prompt input message
    System.out.println("Input the student's degree");

    // assign a double data type value
    grade[i] = sc.nextDouble();

    // test if grade is less than 10
        if(grade[i] < 10 ){
            grade[i] += 0.5;
        }
        System.out.println(grade[i]);
    }       
}
}

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