简体   繁体   中英

Do while loop will not read variable entered within loop

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 aa 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

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

...
int num;
do {
    ...
    num = kbReader.nextInt();
    ...;
}
...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM