Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
import java.util.Scanner;


public class Lab5
{
	
	 public static void main (String[]args)
         {
		Scanner in = new Scanner(System.in);
            System.out.println("How big is the multiplication");
		int choice = in.nextInt();
                while(choice>=1 && choice<=20)
                {
                System.out.print("\nX\t");
            for(int x = 1; x <= choice; x++)
            {   
                    System.out.print(x+"\t"); 
                     System.out.print("\n\t");  
            }
            for( int l=choice; l<=choice; l++)
            {
                System.out.print("-------");
                System.out.println();
            }
                
            for (int j = 1; j <= choice; j++)
{
System.out.print("row"+choice+"|\t");
for (int i = 1; i <= choice; i++)
{
    System.out.print(i * j + "\t");
}

System.out.println();
}
         
            }
                }
         }
Posted
Updated 5-Oct-12 13:13pm
v3

Java
int choice = in.nextInt();
while(choice>=1 && choice<=20)
{
    ...

Your loop is based in the value returned in the variable choice. However, at the end of the loop you do not get a new value or have any way of breaking out of the loop. A better way would be something like:
Java
int choice;
do
{
    System.out.println("How big is the multiplication");
    choice = in.nextInt();
    if (choice < 1 || choice > 20)
        break;  // exit the loop if choice out of range

    // all other code here

} while(choice > 0);
 
Share this answer
 
v2
Comments
Mohibur Rashid 5-Oct-12 5:07am    
the right one :) +5
Richard MacCutchan 5-Oct-12 5:15am    
Thanks, but not too difficult to spot.
fjdiewornncalwe 5-Oct-12 9:41am    
+5. It was easy to spot. I like your solution. I was going to suggest a revised loop, but in this case I didn't think of using the while instead. Nice one.
Richard MacCutchan 5-Oct-12 9:48am    
Thanks, and strangely (or maybe not), no one noticed the bug in my code before I revised it.
diego14567 6-Oct-12 17:09pm    
thanks i never would have guessed this method
The value of the variable 'choice' is not getting incremented anywhere. So the fix will be to increment it. My solution for the issue. Tell me if I am correct....

C#
 while(choice>=1 && choice<=20)
                {
                System.out.print("\nX\t");
            for(int x = 1; x <= choice; x++)
            {   
                    System.out.print(x+"\t"); 
                     System.out.print("\n\t");  
            }
            for( int l=choice; l<=choice; l++)
            {
                System.out.print("-------");
                System.out.println();
            }
                
            for (int j = 1; j <= choice; j++)
{
System.out.print("row"+choice+"|\t");
for (int i = 1; i <= choice; i++)
{
    System.out.print(i * j + "\t");
}
 
System.out.println();
}
  choice++; // Added this line of code as fix        
}
 
Share this answer
 
Comments
diego14567 5-Oct-12 22:11pm    
i tried this but it still prints an infinite loop
I think you did not do fully:
VB
/*---------------------------------------------------------------------------
// AUTHOR:          (Put your name here)
// FILENAME:        Lab5.java
// SPECIFICATION:   This program is for practicing nested loops.
//                    The first task is to print a multiplication table
//                      whose size is specified by the user.
//                    The second (optional, no extra credit, just for fun)
//                      task is to encode a string entered by the user.
//                      The encryption you will use is as follows: in the ith
//                      word, shift every letter up by i (so if the sentence
//                      is "abc abc abc", it becomes "bcd cde def").
// INSTRUCTIONS:    Read the following code skeleton and add your own code
//                  according to the comments.  Ask your TA or your class-
//                  mates for help and/or clarification.  When you see
//                  //--> or ??? that is where you need to add code.
// LAB LETTER:      (Put your Lab Letter here)
//-------------------------------------------------------------------------*/

//  Import required package (Scanner)
//-->

//Declare class (Lab5)
//-->
//-->
    //Declare the main method
    //-->
    //-->
        //Declare some variables: a Scanner (and initialize) and an integer that holds
        //the user's choice of how big the multiplication table is.
        //-->
        //-->

        //For the second task (optional) declare the following:
        //an integer variable to keep track of what word we're currently operating on,
        //a String array that holds each word int he user's sentence separately
        //a String that holds the encrypted text,
        //Give the strings initial values of "" (empty string but not null),
        //and give the first integer variable an initial value of 0
        //-->
        //-->
        //-->

        //make a do while loop that will repeat until the user enters a valid
        //input (an integer from 1 to 20).

        ???
        {

            //Ask the user how large to make the multiplication table
            //-->
            //Read in the user's input
            //-->

            //output the multiplication table if the user's input is within the
            //appropriate range (1-20)
            ???(???)
            {
                //Print out the following string used for formatting the table "\nX\t"
                //use print, not println
                //-->

                //make a for loop that prints each of the numbers from 1 to the user-specified number
                //putting a tab '\t' character in between each number (all on the same line)
                ???(???; ???; ???)
                        //-->

                //Print out the following string used for formatting the table "\n\t"
                //use print, not println
                        //-->

                //make a for loop that prints out the string "--------"  (on the same line)
                //a number of times equal to the user specified size of the multiplication
                //table (more formatting)
                ???(???; ???; ???)
                        //-->

                //Print a blank line
                //-->


                //make a for loop that will range a variable i from 1 to the user-specified
                //size of the multiplication table. At the start of each row (the outer loop),
                //you should print the current row number and the string "|\t" for formatting
                //Inside of that for loop, do the same for
                //a variable j. Each table entry will be i*j and you should print this as well
                //as tab character '\t' after each entry. Also, after each row is done, you
                //should print a blank line.
                ???(???; ???; ???)
                {
                    System.out.print(??? + "|\t");
                    ???(???; ???; ???)
                    {
                        System.out.print(??? + "\t");
                    }
                    //Print a blank line
                    //-->
                }
            }
            //otherwise print out an error telling the user the input is out of range
            ???
            {
                //-->
            }
        }???(???);
        /*


See this last part
 
Share this answer
 
Comments
diego14567 4-Oct-12 23:52pm    
i fail to see how that would solve the infinite loop
[no name] 4-Oct-12 23:54pm    
Why you don't have Do while loop?
diego14567 4-Oct-12 23:57pm    
why add a do while loop when the while loop checks the condition the same
[no name] 4-Oct-12 23:58pm    
i don't know. the problem asks to add do-while loop
[no name] 5-Oct-12 0:08am    
did you solve the infine problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900