简体   繁体   中英

Can I run a while loop inside a for loop? [Java] If so, how would I do it in this scenario?

Im doing some simple maths using loops and my task is as follows:

"Given an integer,N , print its first 10 multiples. Each multiple N * i (where 1 <= N <= ) should be printed on a new line in the form: N xi = result."

My input has to be :

N

My output:

Print 10 lines of output; each line 'i' (where 1 <= N <= ) contains the result N * i of  in the form: N x i = result.

Sample Input:

2

Sample Output:

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

This is what I have done so far:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        
        for (int i = 0; i >= 1 && i <= 10; i++){
            
            
        }

        scanner.close();
    }
}

Would I be able to use a while loop to state the the integer, N, should be able to print out all the numbers in the inclusive range of 1 to 10 to the calculation of the (sample) input: 2x1=2. 2x2=4 etc.

If I can do it without the while loop ie just using the for loop or for-each, please advise on how I can do it. Im really confused with this. Thanks.

I really don't see the problem, you don't need a while loop at all, you just need simple maths:

public static void main(String[] args) {
    int N = scanner.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        
    // I removed the i >= 1 because it hinders the for loop from even staring,
    // you had i = 0 before, so the i >= 1 would output false. So the for loop would stop
    // instantly. You can just start the loop with the number 1 so you dont multiply by zero.
    for (int i = 1; i <= 10; i++) {
        System.out.println(N + " x " + i + " = " + (N * i));
        /*
        lets say N is 2, then it will output the following:
        2 x 1 = 2
        2 x 2 = 4
        2 x 3 = 6
        2 x 4 = 8
        2 x 5 = 10
        2 x 6 = 12
        2 x 7 = 14
        2 x 8 = 16
        2 x 9 = 18
        2 x 10 = 20
        Just as you want it to.
        */
    }
    
    scanner.close();
}

As i can understand you want to show the mult table of a number, showing multiples from 1 to 10 of a given number. What i dont understand is why you need a while inside the for, as you have the base number (input) and a constant for loop (1 <= N <= 10). If i understood well the question, this is the code you need to achive this:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    
        for (int i = 1; i < 11; i++){
            System.out.println(String.format("%d x %d = %d", n, i, n*i);
        
        }

        scanner.close();
    }
}

Reading your question the comment from @Toastrackenigma is the right answer, yes you can use nested while or for loops inside other loops. What you need to do is to be more clear in your questions to obtain better help from the community

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;



public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(bufferedReader.readLine().trim());
        
        for (int i=1; i<=10; i++){
            System.out.println(n + " x " + i + " = " + n*i);
        }

        bufferedReader.close();
    }
}

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