0

For my program I am to Write a program that accepts a number of rows between 2 and 10. Produce a multiplication triangle of n rows. Each row contains entries up to its row size. This I have no problem with. However after the user enters the number 0 into my question "Please enter the number of rows you would like to print: " It is supposed to terminate the loop and print "Thank you for using this program!" I have used a a DO...WHILE loop to determine if the user wishes to continue. In my loop i declared the number that the user wants to print as int num. and my loop should continue as long as the num>=1. However I keep on recieving an error message at the line while (num>=1); saying that It can not find symbol. Why is it saying that? Thank you in advance

import java.io.*;
import java.util.*;
public class Prog166g
{ //begin testshell 
public static void main (String[] args)
{ //begin main
 int i;
    int outer;
    int inner;
    int result;
    int example = 4;
    System.out.print("Number of rows for this triangle : " + example +"\n");
    for ( i = 1; i <= example; i++) {
        System.out.printf("%5s", i);
    }
    System.out.println("");
    for ( outer = 1; outer <= 4; outer++) {
        for ( inner = 1; inner <= outer; inner++) {
            result = outer * inner;
            System.out.printf("%5s", result);
        }
        System.out.println("");
    }
    System.out.println("");

        do{
        System.out.print("Please enter the number of rows you would like to print: ");
        Scanner kbReader = new Scanner(System.in);
        int num = kbReader.nextInt();
        System.out.println("");
        System.out.println("Number of rows for this triangle: " + num);
        for ( i = 1; i <= num; i++) {
            System.out.printf("%5s", i);
        }
        System.out.println("");
        for ( outer = 1; outer <= num; outer++) {
            for ( inner = 1; inner <= outer; inner++) {
                result = outer * inner;
                System.out.printf("%5s", result);
            }
        }
     }while (num>=1);


   System.out.println("Thank you for using this program"); 
}// ends main
}//ends testshell
1
  • It's saying it can't find the symbol num because the scope of the while loop (where you are using num) is outside of the do loop (where you declare num). Declare int num; before the start of do{} and the error should clear. Commented Oct 17, 2017 at 17:01

2 Answers 2

2

Your declaration of "num" must be before the "do". This could be:

...
int num;
do {
    ...
    num = kbReader.nextInt();
    ...;
}
...
Sign up to request clarification or add additional context in comments.

1 Comment

oh I understand. Thank you
1

Your num variable is out of scope. Just simply declare it somewhere above the loop.

int example = 4;
int num; //Declare num here
System.out.print("Number of rows for this triangle : " + example +"\n");
for ( i = 1; i <= example; i++) {
    System.out.printf("%5s", i);
}
System.out.println("");
for ( outer = 1; outer <= 4; outer++) {
    for ( inner = 1; inner <= outer; inner++) {
        result = outer * inner;
        System.out.printf("%5s", result);
    }
    System.out.println("");
}
System.out.println("");

    do{
    System.out.print("Please enter the number of rows you would like to print: ");
    Scanner kbReader = new Scanner(System.in);
    num = kbReader.nextInt();
    System.out.println("");
    System.out.println("Number of rows for this triangle: " + num);
    for ( i = 1; i <= num; i++) {
        System.out.printf("%5s", i);
    }
    System.out.println("");
    for ( outer = 1; outer <= num; outer++) {
        for ( inner = 1; inner <= outer; inner++) {
            result = outer * inner;
            System.out.printf("%5s", result);
        }
    }
 }while (num>=1);


  System.out.println("Thank you for using this program"); 
}// ends main
}//ends testshell

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.