Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Homework
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 4-Oct-12 17:15pm
Edited 5-Oct-12 13:13pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
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);
  Permalink  
v2
Comments
Mohibur Rashid at 5-Oct-12 5:07am
   
the right one :) +5
Richard MacCutchan at 5-Oct-12 5:15am
   
Thanks, but not too difficult to spot.
Marcus Kramer at 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 at 5-Oct-12 9:48am
   
Thanks, and strangely (or maybe not), no one noticed the bug in my code before I revised it.
diego14567 at 6-Oct-12 17:09pm
   
thanks i never would have guessed this method
diego14567 at 6-Oct-12 17:23pm
   
okay ive added this to my code but once it loops for the second number it will take too long and never display it.
Richard MacCutchan at 7-Oct-12 3:25am
   
You're still doing something wrong; it works fine for me.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I think you did not do fully:
/*---------------------------------------------------------------------------
// 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
  Permalink  
Comments
diego14567 at 4-Oct-12 23:52pm
   
i fail to see how that would solve the infinite loop
Merey Yerdauletov at 4-Oct-12 23:54pm
   
Why you don't have Do while loop?
diego14567 at 4-Oct-12 23:57pm
   
why add a do while loop when the while loop checks the condition the same
Merey Yerdauletov at 4-Oct-12 23:58pm
   
i don't know. the problem asks to add do-while loop
Merey Yerdauletov at 5-Oct-12 0:08am
   
did you solve the infine problem?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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....
 
 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        
}
  Permalink  
Comments
diego14567 at 5-Oct-12 22:11pm
   
i tried this but it still prints an infinite loop

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

  Print Answers RSS
0 OriginalGriff 278
1 Maciej Los 205
2 Nguyen.H.H.Dang 190
3 arvind mepani 187
4 Peter Leow 180


Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid