Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I\'m having trouble finishing my program. I am having trouble making the

ID: 3538629 • Letter: H

Question

Hello, I'm having trouble finishing my program. I am having trouble making the do while loop work, as well as getting a switch statement to work. I have to get the same results as I do with my if else statements.

I am having trouble understanding how to set up the switch statements, I Was told that they are very similar to the if else statements.


If anyone could help. I appreciate it!





import java.util.*;

public class P1

{

public static void main( String args[] )

{

char choice;

long num ;

int newNum;

int tenthDigit = 0;

Scanner scan = new Scanner(System.in);

String name = null;

String inputStr = null;

//do

//{

System.out.print("Enter student's name: ");

name = scan.next();

System.out.print("Enter numerical grade: ");

num = scan.nextLong();

while ( num < 0 )

{

System.out.print("ERROR: Enter a positive whole number! Enter numerical grade: ");

num = scan.nextLong();

}

for (long i = num; i > 100; i = num )

{

num = num/10;

}

newNum = (int) num;

if (newNum == 100)

{

System.out.print("Using if else, " + name + "'s grade of " + newNum + " is A++");

tenthDigit = 0;

}

else if (newNum >= 90 && newNum <= 99)

{

System.out.print("Using if else, " + name + "'s grade of " + newNum + " is A");

tenthDigit = 90;

}

else if (newNum >= 80 && newNum <= 89)

{

System.out.print("Using if else, " + name + "'s grade of " + newNum + " is B");

tenthDigit = 80;

}

else if (newNum >= 70 && newNum <= 79)

{

System.out.print("Using if else, " + name + "'s grade of " + newNum + " is C");

tenthDigit = 70;

}

else if (newNum >= 60 && newNum <= 69)

{

System.out.print("Using if else, " + name + "'s grade of " + newNum + " is D");

tenthDigit = 0;

}

else if (newNum < 60)

{

System.out.print("Using if else, " + name + "'s grade of " + newNum + " is F--");

tenthDigit = -10;

}

else

{

}

if ( (newNum - tenthDigit) <= 9 && (newNum - tenthDigit) >= 6)

{

System.out.print("+");

}

else if ((newNum - tenthDigit)<= 1)

{

System.out.print("-");

}

switch (newNum)

{

case (100):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A++");

break;

case (99):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A+");

break;

case (98):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A+");

break;

case (97):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A+");

break;

case (96):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A+");

break;

case (95):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A");

break;

case (94):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A");

break;

case (93):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A");

break;case (92):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A");

break;case (91):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A-");

break;case (90):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A-");

case (89):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is B+");

break;

case (88):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is B+");

break;

case (87):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is B+");

break;

case (86):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is B+");

break;

case (85):

System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is B");

break;

System.out.print("Want to calculate a grade (y/n)? ");

inputStr = scan.next();

choice = inputStr.charAt(0);

scan.close();

System.exit(0);

}


} while (choice != n && choice != N);

}



Explanation / Answer

I have updated the code as you required. Please check the code below.


import java.util.Scanner;

public class P1

{

    public static void main( String args[] ){
   
        char choice;
       
        long num ;
       
        int newNum;
       
        int tenthDigit = 0;
       
        Scanner scan = new Scanner(System.in);
       
        String name = null;
       
        String inputStr = null;
   
        do{
           
            System.out.print("Enter student's name: ");
           
            name = scan.next();
           
            System.out.print("Enter numerical grade: ");
           
            num = scan.nextLong();
           
            while ( num < 0 )
           
            {
           
            System.out.print("ERROR: Enter a positive whole number! Enter numerical grade: ");
           
            num = scan.nextLong();
           
            }
           
            for (long i = num; i > 100; i = num )
           
            {
           
            num = num/10;
           
            }
           
            newNum = (int) num;
           
            if (newNum == 100)
           
            {
           
            System.out.print("Using if else, " + name + "'s grade of " + newNum + " is A++");
           
            tenthDigit = 0;
           
            }
           
            else if (newNum >= 90 && newNum <= 99)
           
            {
           
            System.out.print("Using if else, " + name + "'s grade of " + newNum + " is A");
           
            tenthDigit = 90;
           
            }
           
            else if (newNum >= 80 && newNum <= 89)
           
            {
           
            System.out.print("Using if else, " + name + "'s grade of " + newNum + " is B");
           
            tenthDigit = 80;
           
            }
           
            else if (newNum >= 70 && newNum <= 79)
           
            {
           
            System.out.print("Using if else, " + name + "'s grade of " + newNum + " is C");
           
            tenthDigit = 70;
           
            }
           
            else if (newNum >= 60 && newNum <= 69)
           
            {
           
            System.out.print("Using if else, " + name + "'s grade of " + newNum + " is D");
           
            tenthDigit = 0;
           
            }
           
            else if (newNum < 60)
           
            {
           
            System.out.print("Using if else, " + name + "'s grade of " + newNum + " is F--");
           
            tenthDigit = -10;
           
            }
           
            else
           
            {
           
            }
           
            if ( (newNum - tenthDigit) <= 9 && (newNum - tenthDigit) >= 6)
           
            {
           
            System.out.print("+");
           
            }
           
            else if ((newNum - tenthDigit)<= 1)
           
            {
           
            System.out.print("-");
           
            }
           
            switch (newNum){
                case (100):
                System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A++");
                break;
                case (99):
                case (98):
                case (97):
                case (96):
                case (95):
                case (94):
                case (93):
                case (92):
                case (91):
                   
                case (90):
                System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is A");break;   
                case (89):
                case (88):
                case (87):
                case (86):
                case (85):
                case (84):
                case (83):
                case (82):
                case (81):
                case (80):
                System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is B");break;
                case (79):
                case (78):
                case (77):
                case (76):
                case (75):
                case (74):
                case (73):
                case (72):
                case (71):
                case (70):
                System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is C");break;
                case (69):
                case (68):
                case (67):
                case (66):
                case (65):
                case (64):
                case (63):
                case (62):
                case (61):
                case (60):
                System.out.print(" Using switch, " + name + "'s grade of " + newNum + " is D");break;
               
                default:
                    System.out.print("Using if else, " + name + "'s grade of " + newNum + " is F--");
               
                }
            while(true){
                System.out.print(" Want to calculate a grade (y/n)? ");
               
                inputStr = scan.next();
                choice = inputStr.charAt(0);
                scan.skip(" ");
                //if user select other than y/n
                if(choice != 'n' && choice != 'y' && choice != 'N' && choice != 'Y'){
                    System.out.print("Wrong input... Enter y/n :");
                    continue;
                }else if(choice == 'n' || choice == 'N'){
                    scan.close();
                    System.exit(0);
                }
                break;
                //if 'y' it will continue automatically..
            }
        }while(true);
   
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote