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

This is an assignment for a coding class teaching java. I need a code that will

ID: 3813790 • Letter: T

Question

This is an assignment for a coding class teaching java. I need a code that will end the program if you type in q or quit. I tried doing a while loop that if a string is not equal to Quit then it should continue, but after it asks for input to quit it skips the quitting input and goes straight to the beginning of the program where you need to enter a 12 digit code.

class EAN13
{
public static void main (String[] args)
{
   int digit,lastdigit,weight,diff,sum;
   String q = "DontQuit";
   digit = 0;
   Scanner scan = new Scanner(System.in);
   while(!q.equalsIgnoreCase("Quit")){
   System.out.println("Enter the first 12 digits of EAN13 code press enter after each digit:");
   sum = 0;
   for(int n =1; n<=12;n++){
       digit = scan.nextInt();
       if(n%2 == 0)
       weight =3;
       else
           weight =1;
       sum = sum + digit*weight;
         
   }
   System.out.println("sum = "+sum);
   System.out.println("Enter the last digit of EAN13");
lastdigit = scan.nextInt();
   diff = 10 - (sum % 10);
   if (diff == lastdigit)
       System.out.println("EAN13 is validated and correct");
else
System.out.println("EAN13 is not validated and is incorrect");
   System.out.println("Enter "Quit" to end program, enter anything else to continue:");
   q = scan.nextLine();
   }
}

}

Explanation / Answer

HI, PLease find mu fixes.

import java.util.Scanner;

public class EAN13

{

   public static void main (String[] args)

   {

       int digit,lastdigit,weight,diff,sum;

       String q = "DontQuit";

       digit = 0;

       Scanner scan = new Scanner(System.in);

       while(true){

           System.out.println("Enter the first 12 digits of EAN13 code press enter after each digit:");

           sum = 0;

           for(int n =1; n<=12;n++){

               digit = scan.nextInt();

               if(n%2 == 0)

                   weight =3;

               else

                   weight =1;

               sum = sum + digit*weight;

           }

           System.out.println("sum = "+sum);

           System.out.println("Enter the last digit of EAN13");

           lastdigit = scan.nextInt();

           diff = 10 - (sum % 10);

           if (diff == lastdigit)

               System.out.println("EAN13 is validated and correct");

           else

               System.out.println("EAN13 is not validated and is incorrect");

           System.out.println("Enter "Quit" to end program, enter anything else to continue:");

           q = scan.next();

          

           if(q.equalsIgnoreCase("quit") || q.equalsIgnoreCase("q"))

               break;

       }

   }

}

/*

Sample run:

Enter the first 12 digits of EAN13 code press enter after each digit:

1 2 3 4 5 6 7 8 9 11 12 13

sum = 169

Enter the last digit of EAN13

9

EAN13 is not validated and is incorrect

Enter "Quit" to end program, enter anything else to continue:

quit

*/

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