Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
JavaScript
import java.util.Scanner;


public class Lab5 {

    
   
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner in = new Scanner(System.in);
            System.out.println("How big is the multiplication");
		int choice = in.nextInt();
              
               do
               {
                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=1; l<=choice; l++)
                     {
                System.out.print("-------");
                System.out.print("");
                     }
            
            
            
             
          for(int i=1; i<=choice; i++)
				{
					System.out.print(i + "|\t");
					for(int j=1; j<=i; j++)
					{
						System.out.print(j*i + "\t");
                                               
					}
					System.out.println();
                                       
					
				}
          
                                }
              
               
   }while(choice>=1 && choice<=20);
         
            
             
            }
              
}
Posted
Updated 5-Oct-12 20:53pm
v4

Folow up to first question:

anyone know why this code keeps going in an infinite loop[^]


This Question:
You will have to make a decision - let the loop run or stop it.
That can be done by a if/else. Using the keyword break will cause the loop to break and stop.

Java
System.out.println("Enter number");
Scanner input = new Scanner( System.in ); 
int iDecision = input.nextInt();
if(0 == iDecision){ // do something when number is 0
  // do something e.g. ....
  break; // ...loop breaks
}
else{ // optional
 // do other thingy
}


EDIT:
I deleted Question number 3 for this homework. Please ask here to keep the information together and our programming suggestions in line - we might confuse you otherwise with contrasting suggestions.

while loop

You can make a while loop that runs infinit:
Java
while(true){
System.out.println("still running");
}


Such a loop needs a break condition. You want the user to decide when that is. So inside the while loop:

- ask User for input
- check input(if/else)
- when user wants to quit "break" the loop.


Have fun.
 
Share this answer
 
v3
Comments
diego14567 6-Oct-12 10:26am    
how would i make a while loop to keep reading the users input even after they entered a wrong number
TorstenH. 6-Oct-12 12:30pm    
PLease check addition while loop.
diego14567 6-Oct-12 14:33pm    
i was asking if the user inputted say 20 it would do the table for it then let them input again instead of just quitting the program as it only lets the user input once
TorstenH. 7-Oct-12 5:41am    
ask inside loop
react inside loop
quit when needed, let loop run (and ask for input again) otherwise.
I gave you the answer here[^], why not try it?
 
Share this answer
 

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