标签: while-loop

基本While循环不打印任何内容

我正在尝试在C中创建一个while循环,它表示构建成功,但它不打印任何东西.我真的没看到什么错,它在控制台中没有显示任何内容.

int main()
{
  int w = 0;
  while (w >=100){
    printf("w = %i" , w);
    w++;
  }
  return 0; 
}
Run Code Online (Sandbox Code Playgroud)

c while-loop logical-operators

-2
推荐指数
1
解决办法
54
查看次数

减少varibale直到满足条件(在循环中无体)

我正在寻找这样的东西:

int nVal = 130;
while(nVal % 6 != 0)
  nVal--;
Run Code Online (Sandbox Code Playgroud)

在没有身体的while循环中:

int nVal = 130;
while(nVal-- % 6 != 0)
Run Code Online (Sandbox Code Playgroud)

当然这样,--操作员经常被调用一次.这里有什么建议,还是我应该选择#1?

c while-loop

-2
推荐指数
1
解决办法
76
查看次数

为什么这不是while循环正常工作?

我正在为我的课程开展一个项目.该程序运行良好,直到它达到我的最终程序我已经弄乱它​​约2小时,当我以为我已经完成.

这是它搞乱的代码.

do {
                System.out.println("Do you want to end program? (Enter n or y):");
                endProgram = Input.next();
                if(!endProgram.equals("y") || (!endProgram.equals("n"))){
                    System.out.println("Do you want to end program? (Enter n or y):");
                }
                if (endProgram.equalsIgnoreCase("n")){
                    endProgram = "n";
                    aui = true;
                } 
                if (endProgram.equalsIgnoreCase("y")){
                    endProgram = "y";
                    aui = true;
                }
            } while(aui = false);
Run Code Online (Sandbox Code Playgroud)

如果然后切换到if,我试着搞乱别人.完整的代码是

public static String endProgram = null;
public static void main(String[] args) {


    String MUR = "--------------Monthly Use Report--------------";
    int minutesAllowed;
    int minutesUsed = 0;
    int minutesOver; …
Run Code Online (Sandbox Code Playgroud)

java while-loop do-while

-2
推荐指数
1
解决办法
105
查看次数

虽然循环只发生一次

我的while循环只发生一次,而我需要多次检查以确保小时输入为24小时.我做错了什么?我知道可能是某些东西,但我一直在玩它,我仍然只有一次while循环.

#include <stdio.h>
#include <time.h>

int main(int argc, char *argv[])
{
    int h=0;
    int m=0;
    int d=0;
    int ht=0;
    int t=0;

    printf("Starting Hour: ");

    do
    {
        scanf("%d", &h);
    }
    while (h > 0 && h < 24);

    printf ("Invalid input. Please use 24hr format\n");
    printf ("Starting Hour: ");
    scanf("%d",&h);

    printf("Starting Minute: ");
    scanf("%d",&m);
    printf("Starting Time is %d:%d, what is the duration? ", h, m);
    scanf("%d",&d);
    t=(m+d);
    ht=t/60;
    h=(h+ht)%24;
    m=t%60;

    printf("Ending Time: %d:%d",h,m);
    printf("\n");

    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c while-loop do-while

-2
推荐指数
1
解决办法
163
查看次数

C++:在一个while循环中打开括号之前放置半冒号

while(condition);{ 
    statement;
}
Run Code Online (Sandbox Code Playgroud)

while循环的开括号之前的分号不应该在那里.我遇到的问题是程序在运行时遇到while循环的原因.据我所知,半冒号是一个终止标点符号,让编译器知道它已经到了语句的末尾.既然它没有读取while循环的开括号,那么它不应该移动到下一行并执行动作?如果您运行调试器并逐步移动,它将停止在while语句处移动,并且不会更进一步.我只是在寻找一些有关为何情况的见解.如果是这样的情况,while必须后跟一个条件然后开始括号,为什么在分号之间进行编译时会编译?不应该抛出某种错误吗?

正在使用的IDE是Dev c ++.我不确定这是否是IDE特定问题,因为我没有在其他IDE中测试过这个问题.

c++ syntax while-loop

-2
推荐指数
1
解决办法
139
查看次数

while(scanf("%d",&n),n)是什么意思?

给出以下代码:

// this is a solution of uva 12279    
#include<stdio.h>

int main()
{
    int arr[10000],i,n,a,d=0,e=75;
    while(scanf("%d",&n),n)// what's that means?
    {
        d++;
        int c=0,b=0;

        if(n==0)
            return 0;

        for(i=0;i<n;i++)
            scanf("%d",&arr[i]);

        for(i=0;i<n;i++)//this is for loop
        {
            if(arr[i]==0)
                c++;
            else
                b++;
        }
        a=b-c;

       printf("Case %d: %d\n",d,a);
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

是什么意思while(scanf("%d",&n),n)?

c operators while-loop conditional-statements onlinejudge

-2
推荐指数
1
解决办法
5211
查看次数

而真正的循环功能

据我所知,while循环可以这样做:

while (true) {
    // run this code
    if (condition satisfies)
        break; // return;
}
Run Code Online (Sandbox Code Playgroud)

所以我接受了它并且我在下面输入了以下主要功能,我的问题是当我在满足条件(5或更多)后放置一个printf语句时,它会停止并且不显示任何内容,如果我什么也没放,它工作的break语句循环通过5个不同的客户!我错过了关于while循环的任何内容,或者这是它的工作方式吗?

int main ()
{
    displayMenu();
    while (true)
    {
        int gasType,gallonsAmount,carWashOption,customerAmount ;
        float gasPrices[SIZE]={2.99,3.099,3.199,3.299}, washPrices[SIZE]=
        {3.50,3.00,3.00,2.50}, perGallonRate,perWashRate,total;
        float totalSum,totalGallonSum;
        gasType = getGasType();
        gallonsAmount = getGallons();
        carWashOption = getCarWash();
        perGallonRate = getGallonRate(gasPrices, gasType);
        if (carWashOption == 'Y')
        {
            perWashRate = getWashRate( washPrices, gasType );
            total = calcAmount(gallonsAmount, perGallonRate, perWashRate,&totalSum, &totalGallonSum);
        }
        else 
        {
            perWashRate = 0;
            total = calcAmount(gallonsAmount, perGallonRate, perWashRate,&totalSum, &totalGallonSum);
        }
        customerAmount++;// once we …
Run Code Online (Sandbox Code Playgroud)

c c++ while-loop

-2
推荐指数
1
解决办法
568
查看次数

python,while循环打印中间值

我很抱歉,如果这是非常基本的,我是编程的新手.当我运行我的代码时,只打印最后一个值.如何修改我的代码,以便使用while循环显示范围N中的整个列表?

x0 = 100 
p = 5 
N = 4 
i = 0

x = []
while i <=N:
    i += 1
    xn = ((1 + (p/100))**i)*x0
x.append(xn)
print(x)
Run Code Online (Sandbox Code Playgroud)

python loops while-loop

-2
推荐指数
1
解决办法
291
查看次数

C++ While循环终止,即使条件不满足

我希望程序继续要求值进行计算,直到标志为真,然后程序必须停止,但它只执行一次.我不知道我是否以正确的方式使用char,或者是否有更好的方法来执行带有标志的这些类型的while循环.任何帮助都会很棒.[在此处输入图片说明] [1]

int main()
{
    displayMenu();
    bool flag = false;
    while(!flag)
    {
        int choice = 0; int val1 = 0; int val2 = 0; int ans = 0;
        cout << "Enter your choice (1-5): ";
        cin >> choice;
        cout << "\nEnter integer 1: ";
        cin >> val1;
        cout << "\nEnter integer 2: ";
        cin >> val2;
        if(choice < 0 || choice > 5)
        {
            cout << "\nEnter a choice between 1-5: ";
            cin >> choice;
        }
        if (choice == 1) …
Run Code Online (Sandbox Code Playgroud)

c++ while-loop

-2
推荐指数
1
解决办法
126
查看次数

如何使用C#中的程序来响应用户输入

以下是我的代码.当我点击5时,我必须按Enter键5次才能让程序写入该行,而按6则根本不起作用.任何解决方案将不胜感激.

            Boolean keepRunning = true;
            while (keepRunning = true)
            {
                if (Console.ReadLine() == "1")
                {
                    Console.WriteLine("Still running");
                }
                else if (Console.ReadLine() == "2")
                {
                    Console.WriteLine("Still running2");
                }
                else if (Console.ReadLine() == "3")
                {
                    Console.WriteLine("Still running3");
                }
                else if (Console.ReadLine() == "4")
                {
                    Console.WriteLine("Still running4");
                }
                else if (Console.ReadLine() == "5")
                {
                    Console.WriteLine("Still running5");
                }
                else if (Console.ReadLine() == "6")
                {
                    keepRunning = false;
                }
            }
Run Code Online (Sandbox Code Playgroud)

c# readline while-loop

-2
推荐指数
1
解决办法
91
查看次数