| | |
loop within a loop
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Run this:
int N = 4;
int M = 3;
for (int i=0;i<N;i++) {
System.out.println("I= "+i);
for (int j=0;j<M;j++) {
System.out.print("J="+j+" ");
} // end inside loop
System.out.println();
} // end outside loop Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Jun 2009
Posts: 99
Reputation:
Solved Threads: 4
Here is how i can explain loop within a loop(nested loop)
when i=0 j goes from 0 to 3// both working as counters
when i=1 j again goes from 0 to 3
.
.
when i=3 j will go again from 0 to 3
think this way
for an exercise try sorting 5 numbers in ascending order u will need it in future(using nested loop).Use arrays ..arrays commonly need such a concept
.
Java Syntax (Toggle Plain Text)
for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { ...... } }
when i=0 j goes from 0 to 3// both working as counters
when i=1 j again goes from 0 to 3
.
.
when i=3 j will go again from 0 to 3
think this way
for an exercise try sorting 5 numbers in ascending order u will need it in future(using nested loop).Use arrays ..arrays commonly need such a concept
.
//this is my unfinished code
import java.util.Scanner;
public class listProduct {
public static void main (String[] args){
int productChoice;{
Scanner scanAnything = new Scanner(System.in);
System.out.print("Processed Milk");
System.out.print("\t\t\t\t" + "Price");
System.out.println("\t\t" + "grams/size");
System.out.println("");
System.out.println("Condensed");
System.out.println("");
Double[] price = new Double[100];
price [1] = 46.30;
price [2] = 52.10;
String[] milk = new String [51];
milk [0] ="0 - Alaska Filled Milk" + "\t\t\t" + "46.30" + "\t\t" + "300ml";
milk [1] ="1 - Milkmaid Full Cream Milk " + "\t\t" + "52.10" + "\t\t" + "305ml";
milk [2] ="2 - Alaska Filled Milk " + "\t\t\t" + "33.10" + "\t\t" + "370ml";
milk [3] ="3 - Angel Filled Milk" + "\t\t\t" + "34.15" + "\t\t" + "410ml";
milk [4] ="4 - Alpine Full Cream Milk " + "\t\t" + "44.30" + "\t\t" + "378ml";
milk [5] ="5 - Alaska Filled Milk " + "\t\t\t" + "41.50" + "\t\t" + "150g";
milk [6] ="6 - Bear Brand Filled Milk " + "\t\t" + "51.00" + "\t\t" + "180g";
milk [7] ="7 - Nido Fortified Full Cream Milk" + "\t" + "72.00" + "\t\t" + "150g";
milk [8] ="8 - Blend 45" + "\t\t\t\t" + "14.60" + "\t\t" + "25g";
milk [9] ="9 - Great Taste (granules)" + "\t\t" + "16.75" + "\t\t" + "25g";
for (int r=0; r<=1; r++)
System.out.println(milk [r] +"\t\t");
System.out.println("");
System.out.println("Evaporated");
System.out.println("");
for (int r=2; r<=4; r++)
System.out.println(milk [r] +"\t\t");
System.out.println("");
System.out.println("Powdered");
System.out.println("");
for (int r=5; r<=7; r++)
System.out.println(milk [r] +"\t\t");
System.out.println("");
System.out.println("Coffee-refill");
System.out.println("");
for (int r=8; r<=9; r++)
System.out.println(milk [r] +"\t\t");
productChoice = scanAnything.nextInt();
if (productChoice == 0){
System.out.println("Alaka Filled Milk is selected");
System.out.println("Do you want to include more items?");
System.out.println("1 - yes!");
System.out.println("2 - no!");
}else if(productChoice == 1){
System.out.println("Milkmaid Full Cream Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 2){
System.out.println("Alaska Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 3){
System.out.println("Angel Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 4){
System.out.println("Alphine Full Cream Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 5){
System.out.println("Alaska Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 6){
System.out.println("Bear Brand Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 7){
System.out.println("Nido Fortified Full Cream Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 8){
System.out.println("Bleand 45 is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 9){
System.out.println("Great Taste (granules) is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 10){
System.out.println("Nescafe Classic is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 11){
System.out.println("CDO Chinese Style luncheon meat is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 12){
System.out.println("Purefoods Chinese Style luncheon meat is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 13){
System.out.println("Argentina corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 14){
System.out.println("Argentina corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 15){
System.out.println("CDO corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 16){
System.out.println("Swift Juicy corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 17){
System.out.println("Purefoods corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 18){
System.out.println("Ligo corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 19){
System.out.println("Lucky Me instant noodles is selected");
System.out.println("Do you want to include more items?");
}else{
System.out.println("No such item");
System.out.println("Do you want to choose again?");
System.exit(1);}
}
}
}
import java.util.Scanner;
public class listProduct {
public static void main (String[] args){
int productChoice;{
Scanner scanAnything = new Scanner(System.in);
System.out.print("Processed Milk");
System.out.print("\t\t\t\t" + "Price");
System.out.println("\t\t" + "grams/size");
System.out.println("");
System.out.println("Condensed");
System.out.println("");
Double[] price = new Double[100];
price [1] = 46.30;
price [2] = 52.10;
String[] milk = new String [51];
milk [0] ="0 - Alaska Filled Milk" + "\t\t\t" + "46.30" + "\t\t" + "300ml";
milk [1] ="1 - Milkmaid Full Cream Milk " + "\t\t" + "52.10" + "\t\t" + "305ml";
milk [2] ="2 - Alaska Filled Milk " + "\t\t\t" + "33.10" + "\t\t" + "370ml";
milk [3] ="3 - Angel Filled Milk" + "\t\t\t" + "34.15" + "\t\t" + "410ml";
milk [4] ="4 - Alpine Full Cream Milk " + "\t\t" + "44.30" + "\t\t" + "378ml";
milk [5] ="5 - Alaska Filled Milk " + "\t\t\t" + "41.50" + "\t\t" + "150g";
milk [6] ="6 - Bear Brand Filled Milk " + "\t\t" + "51.00" + "\t\t" + "180g";
milk [7] ="7 - Nido Fortified Full Cream Milk" + "\t" + "72.00" + "\t\t" + "150g";
milk [8] ="8 - Blend 45" + "\t\t\t\t" + "14.60" + "\t\t" + "25g";
milk [9] ="9 - Great Taste (granules)" + "\t\t" + "16.75" + "\t\t" + "25g";
for (int r=0; r<=1; r++)
System.out.println(milk [r] +"\t\t");
System.out.println("");
System.out.println("Evaporated");
System.out.println("");
for (int r=2; r<=4; r++)
System.out.println(milk [r] +"\t\t");
System.out.println("");
System.out.println("Powdered");
System.out.println("");
for (int r=5; r<=7; r++)
System.out.println(milk [r] +"\t\t");
System.out.println("");
System.out.println("Coffee-refill");
System.out.println("");
for (int r=8; r<=9; r++)
System.out.println(milk [r] +"\t\t");
productChoice = scanAnything.nextInt();
if (productChoice == 0){
System.out.println("Alaka Filled Milk is selected");
System.out.println("Do you want to include more items?");
System.out.println("1 - yes!");
System.out.println("2 - no!");
}else if(productChoice == 1){
System.out.println("Milkmaid Full Cream Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 2){
System.out.println("Alaska Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 3){
System.out.println("Angel Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 4){
System.out.println("Alphine Full Cream Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 5){
System.out.println("Alaska Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 6){
System.out.println("Bear Brand Filled Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 7){
System.out.println("Nido Fortified Full Cream Milk is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 8){
System.out.println("Bleand 45 is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 9){
System.out.println("Great Taste (granules) is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 10){
System.out.println("Nescafe Classic is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 11){
System.out.println("CDO Chinese Style luncheon meat is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 12){
System.out.println("Purefoods Chinese Style luncheon meat is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 13){
System.out.println("Argentina corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 14){
System.out.println("Argentina corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 15){
System.out.println("CDO corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 16){
System.out.println("Swift Juicy corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 17){
System.out.println("Purefoods corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 18){
System.out.println("Ligo corned beef is selected");
System.out.println("Do you want to include more items?");
}else if(productChoice == 19){
System.out.println("Lucky Me instant noodles is selected");
System.out.println("Do you want to include more items?");
}else{
System.out.println("No such item");
System.out.println("Do you want to choose again?");
System.exit(1);}
}
}
}
•
•
Join Date: Jun 2009
Posts: 99
Reputation:
Solved Threads: 4
JAVA Syntax (Toggle Plain Text)
import java.util.Scanner; public class listProduct { public static void main (String[] args){ int productChoice;{ Scanner scanAnything = new Scanner(System.in); System.out.print("Processed Milk"); System.out.print("\t\t\t\t" + "Price"); System.out.println("\t\t" + "grams/size"); System.out.println(""); System.out.println("Condensed"); System.out.println(""); Double[] price = new Double[100]; price [1] = 46.30; price [2] = 52.10; String[] milk = new String [51]; milk [0] ="0 - Alaska Filled Milk" + "\t\t\t" + "46.30" + "\t\t" + "300ml"; milk [1] ="1 - Milkmaid Full Cream Milk " + "\t\t" + "52.10" + "\t\t" + "305ml"; milk [2] ="2 - Alaska Filled Milk " + "\t\t\t" + "33.10" + "\t\t" + "370ml"; milk [3] ="3 - Angel Filled Milk" + "\t\t\t" + "34.15" + "\t\t" + "410ml"; milk [4] ="4 - Alpine Full Cream Milk " + "\t\t" + "44.30" + "\t\t" + "378ml"; milk [5] ="5 - Alaska Filled Milk " + "\t\t\t" + "41.50" + "\t\t" + "150g"; milk [6] ="6 - Bear Brand Filled Milk " + "\t\t" + "51.00" + "\t\t" + "180g"; milk [7] ="7 - Nido Fortified Full Cream Milk" + "\t" + "72.00" + "\t\t" + "150g"; milk [8] ="8 - Blend 45" + "\t\t\t\t" + "14.60" + "\t\t" + "25g"; milk [9] ="9 - Great Taste (granules)" + "\t\t" + "16.75" + "\t\t" + "25g"; for (int r=0; r<=1; r++) System.out.println(milk [r] +"\t\t"); System.out.println(""); System.out.println("Evaporated"); System.out.println(""); for (int r=2; r<=4; r++) System.out.println(milk [r] +"\t\t"); System.out.println(""); System.out.println("Powdered"); System.out.println(""); for (int r=5; r<=7; r++) System.out.println(milk [r] +"\t\t"); System.out.println(""); System.out.println("Coffee-refill"); System.out.println(""); for (int r=8; r<=9; r++) System.out.println(milk [r] +"\t\t"); productChoice = scanAnything.nextInt(); // dont scan anything ask the user to choose between 0 to 19 use switch case instead of so many if else or an array if (productChoice == 0){ System.out.println("Alaka Filled Milk is selected"); System.out.println("Do you want to include more items?"); System.out.println("1 - yes!"); System.out.println("2 - no!"); }else if(productChoice == 1){ System.out.println("Milkmaid Full Cream Milk is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 2){ System.out.println("Alaska Filled Milk is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 3){ System.out.println("Angel Filled Milk is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 4){ System.out.println("Alphine Full Cream Milk is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 5){ System.out.println("Alaska Filled Milk is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 6){ System.out.println("Bear Brand Filled Milk is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 7){ System.out.println("Nido Fortified Full Cream Milk is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 8){ System.out.println("Bleand 45 is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 9){ System.out.println("Great Taste (granules) is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 10){ System.out.println("Nescafe Classic is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 11){ System.out.println("CDO Chinese Style luncheon meat is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 12){ System.out.println("Purefoods Chinese Style luncheon meat is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 13){ System.out.println("Argentina corned beef is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 14){ System.out.println("Argentina corned beef is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 15){ System.out.println("CDO corned beef is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 16){ System.out.println("Swift Juicy corned beef is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 17){ System.out.println("Purefoods corned beef is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 18){ System.out.println("Ligo corned beef is selected"); System.out.println("Do you want to include more items?"); }else if(productChoice == 19){ System.out.println("Lucky Me instant noodles is selected"); System.out.println("Do you want to include more items?"); }else{ System.out.println("No such item"); System.out.println("Do you want to choose again?"); System.exit(1);} } } }
for now dont include the option ..if user wants to include more items
u seem to have repeated the code.Use a simple switch case
Java Syntax (Toggle Plain Text)
System.out.println("enter ur choice between 0 to 19"); //scan (productchoice) swicth(productchoice) { case 0: System.out.println("System.out.println("Alaka Filled Milk is selected");//add whatever u want here break; case 1: .... break;
![]() |
Similar Threads
- Make a for loop, loop until eof (C++)
- Could you tell me what about the difference between While..loop, for..loop ... (Pascal and Delphi)
- My loop won't loop in coin simulation! (C++)
- Loop...without the loop (Java)
Other Threads in the Java Forum
- Previous Thread: working with netbeans
- Next Thread: Making Your Own Programming Language
Views: 448 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle os pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






