I would like to be able to calculate the user's grade using the array for loop and if else statements, but the result of my work is always printing the failed grades even if the grade is 90.
import java.util.Scanner;
public class ArrayInputGrade {
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
//allow user input;
System.out.println("How many grades you want to enter?");
int num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter the " num " numbers now.");
for (int grade = 0; grade < array.length; grade ) {
array[grade] = input.nextInt();
if(grade <= 100 && grade >= 90){
System.out.println("Excellent: Grade A");
}else if(grade <= 90 && grade >= 80){
System.out.println("Very Good: Grade B");
}else if(grade <= 80 && grade >= 70){
System.out.println("Good: Grade C");
}else if(grade <= 70 && grade >= 60){
System.out.println("Satisfactory: Grade D");
}else if(grade <= 60 && grade >= 50){
System.out.println("Work Hard: Grade E");
}else if(grade <= 50 && grade >= 40){
System.out.println("Just Passed: Grade F");
}else {
System.out.println("Failed!");
}
}
}
CodePudding user response:
The culprit is the for loop,
for (int grade = 0; grade < array.length; grade ) {
}
because you are checking the loop variable grade
in the if condition.
Modify the loop as below,
for (int i = 0; i < array.length; i ) {
int grade = input.nextInt();
array[i] = grade;
}
CodePudding user response:
package com.alpian.imm.backend;
import java.util.Random;
import java.util.Scanner;
import java.util.stream.IntStream;
public class ArrayInputGrade {
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
//allow user input;
System.out.println("How many grades you want to enter?");
int num = input.nextInt();
// Simulate logic
Random random = new Random();
int[] array = IntStream.range(0, num).map(v -> random.nextInt(101)).toArray();
for (int grade : array) {
if (grade <= 100 && grade >= 90) {
System.out.println("Excellent: Grade A");
} else if (grade <= 90 && grade >= 80) {
System.out.println("Very Good: Grade B");
} else if (grade <= 80 && grade >= 70) {
System.out.println("Good: Grade C");
} else if (grade <= 70 && grade >= 60) {
System.out.println("Satisfactory: Grade D");
} else if (grade <= 60 && grade >= 50) {
System.out.println("Work Hard: Grade E");
} else if (grade <= 50 && grade >= 40) {
System.out.println("Just Passed: Grade F");
} else {
System.out.println("Failed!");
}
}
}
}
Output:
How many grades you want to enter?
5
Failed!
Just Passed: Grade F
Good: Grade C
Excellent: Grade A
Just Passed: Grade F
Process finished with exit code 0
CodePudding user response:
public class UserGrade{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How many grades you want to enter?");
int num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter the " num " numbers now.");
for (int i = 0; i < array.length; i )
{
int grade = input.nextInt();
array[i] = grade;
if (grade <= 100 && grade >= 90)
{
System.out.println("Excellent: Grade A");
}
else if (grade <= 90 && grade >= 80)
{
System.out.println("Very Good: Grade B");
}
else if (grade <= 80 && grade >= 70)
{
System.out.println("Good: Grade C");
}
else if (grade <= 70 && grade >= 60)
{
System.out.println("Satisfactory: Grade D");
}
else if (grade <= 60 && grade >= 50)
{
System.out.println("Work Hard: Grade E");
}
else if (grade <= 50 && grade >= 40)
{
System.out.println("Just Passed: Grade F");
}
else
{
System.out.println("Failed!");
}
}
}
}