Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

My new program skips everything inside it? Help

import java.util.; import java.io.;

public class m {

public static void main(String[] args) {
  Scanner reader = new Scanner(System.in);
  Random rand = new Random();
  Console console = System.console();

  String again = new String("Yes");

  while(again.equalsIgnoreCase("Yes")){
    String morg = console.readLine("Graphing or Multiplication?(G or M):  ");
    while(morg.equalsIgnoreCase("M")){
      int multi = rand.nextInt(12);
      int multi2 = rand.nextInt(12);
      int ans = multi * multi2;
      System.out.println(multi + "*" + multi2);
      int an = reader.nextInt();
      if(an == ans){
        again = console.readLine("Correct!\n\nWould you like to try again?(Yes or No)\n");
      }
      if(an != ans){
        console.printf("Incorrect! Try again...\n");
        }

      if(again.equalsIgnoreCase("No")){
          System.out.println("Exiting...");
          System.exit(0);  
        }
     }
     while(morg.equalsIgnoreCase("G")){
       int multi = rand.nextInt(12);
       int multi2 = rand.nextInt(12);
       int multi3 = rand.nextInt(20) - 40;
       if(multi2 > multi3 || multi2 > multi){
        break;
       }
       int ans = multi / multi2;
       int ans2 = multi3 / multi2;
       System.out.println(multi + "x + " + multi2 + "y =" + multi3);
       System.out.println("Enter X:  ");
       int an = reader.nextInt();
       System.out.println("Enter the second number:  ");
       int an2 = reader.nextInt();
       if(an == ans && an2 == ans2){
         again = console.readLine("Correct!\n\nWould you like to try again?(Yes or No)\n");
       }
       if(an != ans){
         console.printf("Incorrect! Try again...\n");
         }
         if(again.equalsIgnoreCase("No")){
           System.out.println("Exiting...");
           System.exit(0);  
         }
        }
     again = console.readLine("Would you like to go again?:  ");
       }
      }
     }

1 Answer

This should do it for you.

import java.io.Console; import java.util.Random; import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    Random rand = new Random();

    String again = new String("Yes");

    while(again.equalsIgnoreCase("Yes")){
        System.out.println("Graphing or Multiplication?(G or M):  ");
        String morg = reader.nextLine();
        while(morg.equalsIgnoreCase("M")){
            int multi = rand.nextInt(12);
            int multi2 = rand.nextInt(12);
            int ans = multi * multi2;
            System.out.println(multi + "*" + multi2);
            int an = reader.nextInt();
            if(an == ans){
                System.out.println("Correct!\n\nWould you like to try again?(Yes or No)\n");
                again = reader.nextLine();
            }
            if(an != ans){
                System.out.printf("\"Incorrect! Try again...\\n\"");
            }

            if(again.equalsIgnoreCase("No")){
                System.out.println("Exiting...");
                System.exit(0);
            }
        }
        while(morg.equalsIgnoreCase("G")){
            int multi = rand.nextInt(12);
            int multi2 = rand.nextInt(12);
            int multi3 = rand.nextInt(20) - 40;
            if(multi2 > multi3 || multi2 > multi){
                break;
            }
            int ans = multi / multi2;
            int ans2 = multi3 / multi2;
            System.out.println(multi + "x + " + multi2 + "y =" + multi3);
            System.out.println("Enter X:  ");
            int an = reader.nextInt();
            System.out.println("Enter the second number:  ");
            int an2 = reader.nextInt();
            if(an == ans && an2 == ans2){
                System.out.println("Correct!\n\nWould you like to try again?(Yes or No)\n");
                again = reader.nextLine();
            }
            if(an != ans){
                System.out.println("Incorrect! Try again...\n");
            }
            if(again.equalsIgnoreCase("No")){
                System.out.println("Exiting...");
                System.exit(0);
            }
        }
        System.out.println("\"Would you like to go again?:  \"");
        again = reader.nextLine();
    }
}

}

It still didn't work but I found the issue thanks!