任何人都可以解释循环的这种情况如何工作我理解代码而不是for循环条件 [英] Can any one explain how this condition of the loop works I understand code but just not the for loop condition

查看:62
本文介绍了任何人都可以解释循环的这种情况如何工作我理解代码而不是for循环条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究这段代码来检查是否是素数问题是我无法理解语法plz帮助

我使用这段代码来查找是否是素数问题是在for循环中我无法理解为什么我们需要这个condn语句(i< = num / i)。请问我在语法上没有问题我在理解循环如何工作方面存在问题(i< = num / i)condn。 **** / *******包javalearning; //这段代码试图用2 3 4等等来判断是否为素数,直到除数小于或等于被除数/除数

I've been working on this code to check whether no is prime or not the problem is i cannot understand syntax plz help
I am using this code to find whether a no is prime or not the problem is in the for loop i cannot understand why we need to us this condn statment (i<=num/i). Please i don't have problem in syntax i have problem in understanding how the loop works just (i<=num/i) condn. ****/******* package javalearning; // This code tries to check the whether no is prime or not by divinding it with 2 3 4 and so on .. until the the divisor is less then or equal to the dividend/divisor

import java.util.Scanner;
 public class forDemo {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input=new Scanner(System.in);
    int num=input.nextInt();
    boolean isPrime=false;
    // this code is to illustrates prime no test using for loop
    for (int i=2; i<=num;i++)
    {
        if (num%i==0 ) {isPrime=false; break;}
        else isPrime=true;
    }
    System.out.println("IS YOUR NO A PRIME : "+isPrime);
}
} /************/

**************请原谅我使用长行代码。感谢您的帮助



添加评论



我的尝试: < br $>


i尝试使用condn作为i< = num但我知道这不会有效所以plz help

************** Plese forgive me for using long line of codes. Thanks for help

Add Comment

What I have tried:

i tried to use condn as i<=num but i know that would not works so plz help

推荐答案

一开始,你的变量被初始化:
At the start, your variable is initialised:
int i=2



然后每次迭代,检查这个条件:


Then every iteration, this condition is checked:

i<=num



每次迭代后,直到满足条件,i增加:


After each iteration, until the condition is met, i is increased:

i++





素数是除1 之外的任何整数都不能整除的数字。所以要检查一个素数,你的条件应该是:



A prime number is a number not divisible by any integers except 1 and itself. So to check for a prime number, your condition should be:

i<num





注意你的循环,您不必每次都指定isPrime为true。您可以将其简化为:



Note that in your loop, you don't have to assign isPrime every time to true. You can simplify it to:

boolean isPrime=true;
// this code is to illustrates prime no test using for loop
for (int i=2; i<num;i++)
{
    if (num%i==0 ) {isPrime=false; break;}
}


for 循环非常简单 - 它分为四个部分:

A for loop is pretty simple - it comes in four parts:
for (a; b; c) d

其中:

a 初始化循环并设置运行。这只执行一次,每次你的代码只到达

b 在循环代码的主体( d )执行之前立即运行,如果它的计算结果为 true ,那么身体被执行。如果没有,则退出循环。

c 在循环代码的主体后立即运行( d )以设置下一次迭代。

d 是循环的主体,并且是每次循环循环时执行一次的代码。

所以如果a是

Where:
a initialises the loop and sets up to run. This is executed once, and once only each time you code reaches the for
b is run immediately before the body of the loop code ( d ) is executed, and if it evaluates to true then the body is executed. If it doesn't, then teh loop is exited.
c is run immediately after the body of the loop code ( d ) is executed to set up for the next iteration.
d is the body of the loop, and is teh code that is executred once each time the loop goes round.
So if a was

int i = 0

b

i < 3

c

i++

然后循环体将执行三次,一次为i = 0;那么i = 1;然后最后为i = 2

你会得到

a 将我设为0

b 我是< 3?是 - 继续循环

d 执行机构

c 加一个我

b 我是< 3?是 - 继续循环

d 执行机构

c 加一个我

b 我是< 3?是 - 继续循环

d 执行机构

c 加一个我

b 我是< 3?否 - 退出循环。



在您的情况下, c i< num 确保在值达到数字时停止测试( i< = num 将无济于事,因为根据定义,所有数字都可以自行整除。



至于为什么你可以使用 i< = num / i 用一个例子来考虑它。

假设num是41,你的测试序列是:

then the loop body would execute three times, once for i = 0; then i=1; then finally for i = 2
And you would get
a Set i to 0
b Is i < 3? Yes - continue with loop
d Execute body
c Add one to i
b Is i < 3? Yes - continue with loop
d Execute body
c Add one to i
b Is i < 3? Yes - continue with loop
d Execute body
c Add one to i
bIs i < 3? No - exit loop.

In your case, c is i < num which is making sure that you prime test stops when the value reaches the number ("i <= num" won't help, because by definition, all numbers are divisible by themselves)

As to why you might use i <= num / i Think about it with an example.
Assume num is 41, and your test sequence is:

i    num / i
2         20
3         13
4         10
5         8
6         6
7         5

一旦你到达6 x 6,进一步测试没有意义:因为根据定义,你已经通过5,4,3和2测试了可分性!

Once you have got to 6 x 6, there is no point in further testing: because by definition you have already tested for divisibility by 5, 4, 3, and 2!


引用:

我无法理解为什么我们需要这个condn语句(i< = num / i)。

i cannot understand why we need to us this condn statment (i<=num/i).



这是结束循环的条件。

要查看发生了什么,请将此代码添加为循环中的第一行


This is the end condition of the loop.
To see what is going on, add this code as first line in loop

System.out.println("Divisor : "+i);
System.out.println("End Condition : "+(num/i));



并使用素数或正方形运行程序,看看哪些除数被测试。

其他解决方案是将您的程序视为它用调试器执行。

-----

你的代码没有你想象的那样,你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里显示你的代码在做什么并且你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有找到bug,它只是帮助你向你展示发生了什么。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows /jdb.html [ ^ ]

https: //www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器仅显示您的代码正在执行的操作,您的任务是与什么进行比较应该这样做。


and run the program with a prime numbers or a square and see which divisors are tested.
Other solution is to watch your program as it execute with the debugger.
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于任何人都可以解释循环的这种情况如何工作我理解代码而不是for循环条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆