标签: while-loop

Powershell while循环

下面的代码在到达时会一直陷入循环

$choice = read-host "Are you sure you want to disable the following user? (Y/N)" "$name"
Run Code Online (Sandbox Code Playgroud)

我键入y并继续循环该问题.

我尝试了一段时间和单一的IF语句.关于如何阻止这个循环的任何想法?

$choice = ""
$User = read-host "enter username"
$Name = Get-ADuser -Identity $Username | Select-Object Name

while ($choice -notmatch "y/n"){
    $choice = read-host "Are you sure you want to disable the following user? (Y/N)" "$name"

    If ($Choice -eq"Y"){
        Disable-ADAccount $Username
    } 
}
Run Code Online (Sandbox Code Playgroud)

powershell if-statement while-loop

0
推荐指数
1
解决办法
378
查看次数

如果用户输入多行/空格,如何计算单词数?

我有一个char数组,其中包含用户输入的内容.我怎么算数字呢?允许用户做一些疯狂的事情:

hello this
               is a test
   how are
you today?
Run Code Online (Sandbox Code Playgroud)

所以这里的单词数量应为9,但我的程序告诉我23.为什么这不起作用?它计算空间,但我考虑到了这一点sentence_entered[i + 1] != ' '

我的代码:

int i = 0;    
while (sentence_entered[i] != '\0') {

        if (
            (sentence_entered[i] == ' ' ||
            sentence_entered[i] == '\n') &&
            (sentence_entered[i + 1] != ' ' ||
            sentence_entered[i + 1] != '\n')
           ) {
            words += 1;
    }
i++
}
Run Code Online (Sandbox Code Playgroud)

c arrays if-statement while-loop

0
推荐指数
1
解决办法
41
查看次数

很难理解这个循环

我在循环时遇到一些麻烦,它的结果是"TT",但对我来说,如果它打印出"TP"是有意义的.谁能帮我吗?我将展示为什么我认为它应该打印"TP",并需要解释为什么它打印"TT"(这是正确的).

#include <iostream>
using namespace std;
char txt[] = "ETERNITTPLATENE-OG-TAKPAPPEN-BLE-KASTET";

void funk(char* t1, char* t2, int n)
{
    while (t1 < t2) { ++t1; t2 -= n; } cout << *t1 << *t2;
}

int main() {
    funk(txt, txt + 27, 3); cout << '\n';
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以它第一次运行func(E,N,3)所以我们得到 E < N哪个是真的,因此它使得E到T和N到P(因为27-3是24,并且在那个数组中P是24).

那么我们得到T < P,这是假的,因此它应该停止while循环和显示TP,它不会.我在想什么?

c++ loops while-loop

0
推荐指数
1
解决办法
94
查看次数

如何输出到代码9?

#include <stdio.h>
int main()
{
    int i=0;
    while(i++,i<=8);
    printf("%d\n",i);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么i在每个测试用例中比较后完成增量?

c while-loop

0
推荐指数
1
解决办法
89
查看次数

PHP - while语句中的多个条件

由于某种原因,此代码不会在我的编译器中运行.意图(作为更大项目的一部分)是对特定主机执行3次ping操作,或直到成功为止,以先到者为准.它没有产生任何错误,只是终止.如果我从while语句中删除第二个条件,它的工作正常,但是我需要更复杂的东西才能在成功的ping上终止循环.我有一段时间没有碰过PHP,所以我可能会错过一些愚蠢的东西.

<?php
function pingAddress($ip) {
//Set variable to limit loops and end if live
$pass = 0;
$result = 0;
//Create conditions for while loop
while( ( $pass < 3 ) && ( $result = 0 ) ) {
    //Count loops
    $pass++;
    //Execute ping
    $output=shell_exec('ping -n 1 '.$ip);
    //Display ping results for testing purposes
    echo "<pre>$output</pre>";
    //Check for "TTL" presence
    if(strpos($output, 'TTL') !== false)
    {
        //Notate positive result
        $result++;
        //Display for testing
        echo "Alive";
    }
    //Display negative result for testing …
Run Code Online (Sandbox Code Playgroud)

php while-loop

0
推荐指数
1
解决办法
946
查看次数

猜测数字的程序

大家好我正在写一个与用户玩猜谜游戏的程序.你需要考虑一个数字,程序会猜到它.这是我的代码:

 public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Guess a number between 1 do 100 and i'll guess it");
        int min = 0;
        int max = 100;
        int guess = (max-min)/2 + min;
        boolean end = false;
        while(!end){

            System.out.println("zgaduje " + guess);
            String userInput = scan.next();
            if(userInput.equalsIgnoreCase("too much")){
                max = guess;
            }
            else if(userInput.equalsIgnoreCase("too small")){
                min=guess;
            }
            else if(userInput.equalsIgnoreCase("correct")){
                end = true;
            }
             guess = (max-min)/2 + min;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以程序猜测一个数字,然后根据用户输入(太小或太多)再次猜测.它没有按预期工作,它只显示第一次猜测.你知道这里可能有什么问题吗?

java while-loop

0
推荐指数
1
解决办法
80
查看次数

写一个while循环/ for循环的成分.

这是我正在研究的教科书.

var ingredients = ["eggs", "milk", "flour", "sugar", "baking soda", "baking powder", "chocolate chips", "bananas"];

// Write a while loop that prints out the contents of ingredients:
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

var i = 0;

while (i < ingredients.length) {
  i++
  console.log(ingredients[i]);
} 
Run Code Online (Sandbox Code Playgroud)

出来的是什么:

milk
flour
sugar
baking soda
baking powder
chocolate chips
bananas
undefined
Run Code Online (Sandbox Code Playgroud)

第二个问题:

写一个打印出成分内容的for循环:

我的代码:

for (i = 0; i < ingredients.length; i++) {
  console.log(ingredients[i]);
}
Run Code Online (Sandbox Code Playgroud)

这打印出所需的一切.

为什么while循环不打印出"eggs"并在其末尾打印"undefined" - 而for循环很好?

是不是他们都打算打印出同样的东西?

谢谢.

javascript for-loop while-loop

0
推荐指数
1
解决办法
68
查看次数

C#为什么我的计算没有正确添加?

我正在制作一个计算预订日期的表格.在周五和周六,一个房间的费用为150美元,而在剩余的日子里,费用为120美元.我已经使用了一个while循环来设置它,但由于某种原因,它一直在计算错误的价格.

应该是什么样的:

在此输入图像描述

它看起来像什么:

在此输入图像描述

这是我的代码:

int nights = 0;
int price = 0;

private void btnCalculate_Click(object sender, EventArgs e)
    {
        DateTime arrivalDate = DateTime.Parse(txtArrivalDate.Text);
        DateTime departureDate = DateTime.Parse(txtDepartureDate.Text);
        TimeSpan ts = departureDate.Subtract(arrivalDate);
        nights = ts.Days;

        while (arrivalDate != departureDate)
        {
            DayOfWeek dow = arrivalDate.DayOfWeek;
            if (dow == DayOfWeek.Friday || 
                dow == DayOfWeek.Saturday)
            {
                price = 150;
            }
            else
            {
                price = 120;
            }

            arrivalDate = arrivalDate.AddDays(1);

        }

        txtNights.Text = nights.ToString();
        int totalPrice = price * nights;
        txtTotalPrice.Text = totalPrice.ToString();
        int average …
Run Code Online (Sandbox Code Playgroud)

c# datetime while-loop

0
推荐指数
1
解决办法
104
查看次数

如何在while循环中找到max和min - c

我知道这对你来说非常简单,但对我来说,作为初学者非常努力.为了得到的东西.我必须创建程序,它将获取我想要的数量,然后从它们写入最大值和最小值.数字可以有小数,所以这就是为什么加倍.就我的故障而言,一切都很顺利,直到周期开始.最小和最大值的输出总是为0.谁知道为什么?码:

 main() {
  int a = 0, b = 0, min = 1000, max = 0;
  printf("How many numbers do you want to enter\n");
  scanf("%d", & a);
  b = a;
  double n[b];
  printf("Write those numbers\n");
  for (b = 0; a > b; b++) {
    scanf("%lf", & n[b]);
  }
  b = 0;
  while (1) {
    if (n[b] < n[b + 1])
      max = n[b + 1];
    if (min > n[b])
      n[b] = min;
    b++;
    if (b == a)
      break;
  }
  printf("Minimum: …
Run Code Online (Sandbox Code Playgroud)

c double max minimum while-loop

0
推荐指数
1
解决办法
278
查看次数

主{}括号上的语法错误,"期望的时间"?

我完全难过了.无论我做什么,我的编译器都认为我需要在上一次printf中使用while循环?它只是一直在寻找没有意义的事情.当我将它们添加到其中时只是告诉我它不应该存在,当我删除它时我被告知我需要它.

int main() {
float userInput; //The grades that the user inputs.
int passingGrade = 0; //Number of passing grades
int failingGrade = 0; //Failing grades
int invalidGrade = 0; //Invalid grades

//This do for loop is simply counting the number of grades of each catagory posted.
do {
    printf("Enter a grade (Enter -1 to quit):");
    scanf("%f", &userInput);
    printf("You entered: %.1f\n", userInput);
    if ((userInput > 100) || (userInput < 0)) { //Over 100 is impossible.
        invalidGrade = invalidGrade + 1; …
Run Code Online (Sandbox Code Playgroud)

c printf while-loop do-while

0
推荐指数
1
解决办法
71
查看次数

标签 统计

while-loop ×10

c ×4

if-statement ×2

arrays ×1

c# ×1

c++ ×1

datetime ×1

do-while ×1

double ×1

for-loop ×1

java ×1

javascript ×1

loops ×1

max ×1

minimum ×1

php ×1

powershell ×1

printf ×1