GVKun编程网logo

Windows Powershell Do While 循环(powershellfor循环)

2

对于想了解WindowsPowershellDoWhile循环的读者,本文将提供新的信息,我们将详细介绍powershellfor循环,并且为您提供关于2-18分别用for、while和do-whil

对于想了解Windows Powershell Do While 循环的读者,本文将提供新的信息,我们将详细介绍powershellfor循环,并且为您提供关于2-18 分别用 for、while 和 do-while 循环语句以及递归方法计算 n!,并输出算式、2-4 Java 循环语句之 while , do...while、AWS is updating minimum requirements for AWS Tools for PowerShell to Windows PowerShell 3.0 and ....、C++ while 循环 v[i++] vs i++ in while body的有价值信息。

本文目录一览:

Windows Powershell Do While 循环(powershellfor循环)

Windows Powershell Do While 循环(powershellfor循环)

继续与终止循环的条件

do-while()会先执行再去判断,能保证循环至少执行一次。

复制代码 代码如下:

PS C:Powershell> do { $n=Read-Host } while( $n -ne 0)
10
100
99
2012
世界末日
为什么不退出
因为条件不满足
怎样才能满足
请输入一个0,试一试
0
PS C:Powershell>

单独使用While

复制代码 代码如下:

$n=5
while($n -gt 0)
{
    $n
    $n=$n-1
}
5
4
3
2
1

终止当前循环

使用continue关键字,可是终止当前循环,跳过continue后其它语句,重新下一次循环。

复制代码 代码如下:

$n=1
while($n -lt 6)
{
    if($n -eq 4)
    {
        $n=$n+1
        continue
 
    }
    else
    {
        $n
    }
    $n=$n+1
}
1
2
3
5

跳出循环语句

跳出循环语句使用break关键字

复制代码 代码如下:

$n=1
while($n -lt 6)
{
    if($n -eq 4)
    {
        break
    }
    $n
    $n++
}

您可能感兴趣的文章:

2-18 分别用 for、while 和 do-while 循环语句以及递归方法计算 n!,并输出算式

2-18 分别用 for、while 和 do-while 循环语句以及递归方法计算 n!,并输出算式

(1)首先建立一个 Java 项目和 Java 的类并命名为 “阶乘计算”

(2)由于要调用输入函数 java.util.Scanner.nextInt ()(这个函数是专门提供输入类型为整形的数据),所以要导入包 java.util.Scanner,Java 语句为 import java.util.Scanner

(3)建立一个主函数,新建一个 参数为 System.in 的 Scanner 对象 其中名称为 sc,System.in 参数是键盘输入内容,Java 语句为:Scanner sc = new Scanner (System.in);

(4)设置提示输入 n,然后创建一个 int 类型来传入参数 n,然后设置一些中间变量,具体语句是:System.out.print ("请输入 n:");int num = sc.nextInt ();int i=1,n=num,sum=1; 其中第二个语句表示将输入的参数赋值给整形 i

(5)编写 while 循环阶乘代码,由于要输出阶乘算式,所以循环中加入了输出语句,这里的代码和 c 语言相似,不再具体的介绍

(6)do-while 和 for 循环语句与 while 语句类似,主要是递归方法,首先要在类外建立一个递归方法的类,然后在主函数内调用递归方法并传入参数(我在调用递归方法时添加了异常机制,即如果出现错误就中断操作,并输出异常)

(7)运行程序,输入阶乘 n 为 8,代码和结果如图:

 

 

 

 

 

总结心得:

(1)首次学会了调用输入包 java.util.Scanner,并掌握了 nextInt () 函数

(2)为满足输出阶乘算式,必须在每次循环中输出当前循环的乘数以及乘号,最后在添上计算结果

(3)在建立递归函数时,必须要先构造函数,即 BigInteger (String val); 要注意与 c++ 的构造函数的区别,同时开头要导入包 java.math.*

(4)由于阶乘数据量比较大,所以最好要进行异常处理,避免程序卡死

(5)要注意区分 while,do-while 和 for 循环中循环次数的控制

 

 

 

源代码如下:

 1 import java.util.Scanner;
 2 import java.math.*;
 3 public class 阶乘 {
 4     public static void main(String[] args){
 5         Scanner sc = new Scanner(System.in);
 6         System.out.print("请输入n:");
 7         int num = sc.nextInt();
 8         int i=1,n=num,sum=1;
 9         System.out.print("while语句的阶乘算式:1");
10         while(i<n){
11             i+=1;
12             sum*=i;
13             System.out.print("*"+i);
14         }
15         System.out.println("="+sum);
16         
17         i=1;sum=1;
18         System.out.print("do-while语句的阶乘算式:1");
19         do{
20             i+=1;
21             sum*=i;
22             System.out.print("*"+i);
23         }while(i<n);
24         System.out.println("="+sum);
25         
26         
27         System.out.print("for语句的阶乘算式:1");
28         for(i=2,sum=1;i<=n;i+=1){
29             sum*=i;
30             System.out.print("*"+i);
31         }
32         System.out.println("="+sum);
33         
34         
35         阶乘 test = new 阶乘();
36         System.out.print("递归方式的阶乘算式:1");
37         for(i=2,sum=1;i<=n;i+=1){
38             
39             System.out.print("*"+i);
40         }
41         try {
42             System.out.println("="+test.bigsum(num));
43         } catch (Exception e) {
44             // TODO Auto-generated catch block
45             e.printStackTrace();
46         }
47 
48             
49         }
50     public BigInteger bigsum(int i) {
51         if (i == 1) {
52             return BigInteger.ONE;
53         }
54         return BigInteger.valueOf(i).multiply(bigsum(i-1));
55     }
56 }

 

原文出处:https://www.cnblogs.com/fjcy/p/10556836.html

2-4 Java 循环语句之 while , do...while

2-4 Java 循环语句之 while , do...while

Java 常用的 3 种循环: while 、 do...while 、 for

1.while

while(判断条件) {
    循环操作  }

执行过程:

<1>、 判断 while 后面的条件是否成立 ( true /false )

<2>、 当条件成立时,执行循环内的操作代码 ,然后重复执行 < 1 >、< 2 >, 直到循环条件不成立为止

特点:先判断,后执行    如:

int i=1;
while(i<=1000) {
System.out.println("i love imooc");
i++;
}

2.do...while

do{ 
循环操作
} while(判断条件);

执行过程:

<1>、 先执行一遍循环操作,然后判断循环条件是否成立

<2>、 如果条件成立,继续执行 < 1 > 、< 2 >,直到循环条件不成立为止

特点: 先执行,后判断

由此可见,do...while 语句保证循环至少被执行一次

 

 

AWS is updating minimum requirements for AWS Tools for PowerShell to Windows PowerShell 3.0 and ....

AWS is updating minimum requirements for AWS Tools for PowerShell to Windows PowerShell 3.0 and ....

https://amazonaws-china.com/blogs/developer/aws-is-updating-minimum-requirements-for-aws-tools-for-powershell-to-windows-powershell-3-0-and-net-framework-4-5/


On January 14th 2020 Microsoft ended extended support for Windows Server 2008 and Windows Server 2008 R2. The oldest supported Windows Server version, Windows Server 2012, comes with Windows PowerShell 3.0 and .NET Framework 4.5.

The legacy variant of AWS Tools for PowerShell (AWSPowerShell) is currently compatible with Windows PowerShell 2.0 and .NET Framework 3.5. Starting May 1st 2020, AWS will require Windows PowerShell 3.0+ and .NET Framework 4.5+ as prerequisite for all new releases of AWSPowerShell. The existing versions of AWSPowerShell that are compatible with Windows PowerShell 2 and .NET Framework 3.5 will continue to be available for download from PowerShell Gallery, but will not be updated or supported.

What do I need to do?

C++ while 循环 v[i++] vs i++ in while body

C++ while 循环 v[i++] vs i++ in while body

如何解决C++ while 循环 v[i++] vs i++ in while body?

我有一个关于在 while 循环中迭代的问题。所以假设我们有一个大小为 10 的向量,元素从 0 到 9。我们有一个只迭代一次的 for 循环,在 for 循环中我们有一个 while 循环。当我打印循环时,我得到了两种不同的结果,但我不确定为什么。

我理解第一个循环背后的想法,我们在 while 的主体中增加 j 并且我们没有在最后一次迭代中进入循环,因为 j 是 5,所以我们只打印 0-4。 第二个循环是我在理解方面遇到问题的地方。我的逻辑是首先我们将指针 j 增加到 1,所以这就是我们计算 1 而不是 0 的原因。但是一旦 j 是 4 v[j++] = 5 并且在循环中的条件中,当 v[j++] = 5 时,为什么要这样做我们还是 5 吗? 任何帮助表示赞赏。

  vector<int> v(10);
  for(int i = 0; i < v.size(); i++){
    v[i] = i;
  }
  int j = 0;
  for(int i = 0; i < 1; i++){ // first loop
    while(v[j] != 5){
      cout << v[j]; //prints 0 1 2 3 4
      j++;
    }
  }
  for(int i = 0; i < 1; i++){ //second loop
    while(v[j++] != 5){
      cout << v[j]; //prints 1 2 3 4 5
    }
  }

解决方法

j++ 会增加 j 的值,但会在增加之前返回 j 的原始值。

 int i =0;
 int j = 0;
 i = 1;
 j = i++;
 (i is 2,j is 1)

Source

,

后缀递增在概念上复制内存中的操作数,递增原始操作数,最后产生副本的值。

下面的代码片段相当简单。打印 j 的内容后,vector 已递增。

vector<int> v(10);
for(int i = 0; i < v.size(); i++){
    v[i] = i;
}
int j = 0;

for(int i = 0; i < 1; i++){ // first loop
    while(v[j] != 5){
        cout << v[j]; //prints 0 1 2 3 4
        j++;
    }
}

现在,让我们看看第二个循环。

vector<int> v(10);
for(int i = 0; i < v.size(); i++){
    v[i] = i;
}
int j = 0;
for(int i = 0; i < 1; i++){ // second loop
    // values of j are 0 1 2 3 4
    while(v[j++] != 5){
        // values of j are 1 2 3 4 5 because it has been incremented
        cout << v[j]; //prints 1 2 3 4 5
    }
}
,

正如上面所说,j++会增加j 因此,当 j 为 3 时, v[j++] 为 4,因此 while 中的条件不会被破坏。但是因为 j++,当 cout 被执行时 j 是 4 并且 v[4] 是 5 所以 5 被打印

,

您忘记在第一个循环后重新初始化 j。如果您想在第二个循环中从 1 打印到 5,那么您应该在第二个循环运行之前将 j 重新初始化为 0。试试:

vector<int> v(10);
    for (int i = 0; i < v.size(); i++) {
        v[i] = i;
    }
    int j = 0;
    for (int i = 0; i < 1; i++) { // first loop
        while (v[j] != 5) {
            cout << v[j]; //prints 0 1 2 3 4
            j++;
        }
    }
    j = 0;
    for (int i = 0; i < 1; i++) { //second loop
        while (v[j++] != 5) {
            cout << v[j]; //prints 1 2 3 4 5
        }
    }

使用单个 for 循环将有助于更好地理解代码,因为循环的两个条件相同。

    vector<int> v(10);
    for (int i = 0; i < v.size(); i++) {
        v[i] = i;
    }
    int j = 0;
    for (int i = 0; i < 1; i++) { // first loop
        while (v[j] != 5) {
            cout << v[j]; //prints 0 1 2 3 4
            j++;
        }
// joined the end of the first loop and the start of the second loop
        j = 0;
        while (v[j++] != 5) {
            cout << v[j]; //prints 1 2 3 4 5
        }
    }

希望对回答您的问题有所帮助

我们今天的关于Windows Powershell Do While 循环powershellfor循环的分享已经告一段落,感谢您的关注,如果您想了解更多关于2-18 分别用 for、while 和 do-while 循环语句以及递归方法计算 n!,并输出算式、2-4 Java 循环语句之 while , do...while、AWS is updating minimum requirements for AWS Tools for PowerShell to Windows PowerShell 3.0 and ....、C++ while 循环 v[i++] vs i++ in while body的相关信息,请在本站查询。

本文标签: