标签: while-loop

使用单个字符C++评估if语句

我正在尝试评估一个字符:

bool repeat = true;
while (repeat)

//code

char x;
    cout << "Would you like to tansfer another file? Y/N ";
    cin >> x;

    if (x == 'y' || x == 'Y')
        repeat = true;
    if (x == 'n' || x == 'N')
        repeat = false;
    else
        throw "Input error";
Run Code Online (Sandbox Code Playgroud)

我一直得到输入错误作为我的控制台输出.有什么想法吗?我不能让while循环重复.

c++ boolean while-loop

-3
推荐指数
1
解决办法
217
查看次数

如何从用户的表销售中选择所有数据

示例SQL表:

ID | USER | AMOUNT_SALES | TOTAL
--------------------------------
1  | John | 1500         | 2000 
2  | John | 1400         | 3000  
3  | James| 1200         | 4000   
4  | John | 1100         | 5000   
5  | Anie | 1900         | 8000 
Run Code Online (Sandbox Code Playgroud)

所以我想

SELECT * FROM table 
Run Code Online (Sandbox Code Playgroud)

并使用while在HTML表格上显示结果,但我不想重复用户.

我要创建的示例表:

User  | Amount Sales  | Total 
-----------------------------
John  | 4000          | 10000
James | 1200          | 4000
Anie  | 1900          | 8000
Run Code Online (Sandbox Code Playgroud)

php sql while-loop

-3
推荐指数
1
解决办法
65
查看次数

我可以在While语句中使用If语句吗?

while not eof(MyFile) do
begin
  Readln(MyFile,sLine);
  iCheck := pos('*',sLine);

  if iCheck >= 0 then
    K := K + 1;
  else
    K := 1;
  sLine := sArrayParty[K];
end;
Run Code Online (Sandbox Code Playgroud)

K是我的柜台.MyFile设置为文本文件.

我试图在while语句中运行if语句来检查是否有*一行读入sLine.如果是这样,我希望我的计数器递增,从而将以下文本放入数组的下一个索引.所以基本上我有一个文本文件,信息由*分隔符分隔,我希望每条信息都填充一个新的索引.但每当我把if语句放入时,delphi就会发挥作用.

delphi if-statement delphi-7 while-loop

-3
推荐指数
1
解决办法
439
查看次数

堆栈实现中的无限循环c ++

这是我的第一个数据结构程序.我正在使用带有push,pop和initialize函数的数组实现一个简单的堆栈.我得到一个无限循环作为输出.你能告诉我为什么会这样吗?

#include<iostream>

using namespace std;
# define SIZE 6

class stack{



    public:
        void init();
        void push(int i);
        int pop();
        int top;
        int stck[SIZE];//bydefault private

};

void stack::init()
{
    top=0;
    return;
}

void stack::push(int i)
{

    if(top==SIZE)
    {
        cout<<"stack is full";
        return;
    }
    else
    {
        top=top+1;
        stck[top]= i;
        return;
    }
} 

int stack::pop()
{
    if(top==0)
    {
        cout<<"stack is empty. \n";
        return 0;
    }
    else
    {
        top = top-1;
        return(stck[top-1]);
    }
} 

int main()
{
    stack stack1;
    stack1.init();
    int a;
    int m; …
Run Code Online (Sandbox Code Playgroud)

c++ while-loop

-3
推荐指数
1
解决办法
160
查看次数

Java - While循环(帮我理解流程)

当我运行此代码时,我得到UNREACHABLE STATEMENT错误.但是,如果我将false更改为true(在第二次),则输出为:"1 0"并继续执行.里面发生了什么?

int m=2;
while (m >0)
{
 m =m -1;
 System.out.println(m);
} while (false);
Run Code Online (Sandbox Code Playgroud)

输出:"错误:无法访问的语句"

但是,如果,

int m=2;
while (m >0)
{
 m =m -1;
 System.out.println(m);
} while (true);
Run Code Online (Sandbox Code Playgroud)

输出:"1 0"

提前致谢...

java while-loop

-3
推荐指数
1
解决办法
98
查看次数

while循环中的无限循环

while ($my_query = mysql_fetch_array(mysql_query("select table2.ctg_id from table1 INNER JOIN table2 ON table2.game_id = table1.id where table1.id = $id"))) 
{
    echo $my_query['data'];
}
Run Code Online (Sandbox Code Playgroud)

当我在navicat上尝试这个查询时,我会采取;

2
3

但是在'.php'页面中,查询变为无限循环.

php mysql while-loop

-3
推荐指数
1
解决办法
58
查看次数

虽然功能不像我想要的那样工作

有一个与while函数有关的新问题.听起来很简单,我仍然无法绕过它.

就像我上一个程序一样,这个程序在正确和错误的消息之后意外关闭.我希望在输入数字后循环,这样程序就不会停止.感谢您的帮助,如果有的话.

#include <iostream>
using namespace std;

int main()
{
    int X = 0; //setting the first variable
    int num; //setting the second

    while (X == 0) //this should happen whenever X is equal to 0
    {
          cout << "Type a number bigger than 3. "; //output
          X++; //This should increase X, so that the next while function can happen
    }
    while (X == 1) //again, since I increased x by one, (0+1=1 obviously) this should happen
    {
          cin >> …
Run Code Online (Sandbox Code Playgroud)

c++ while-loop

-3
推荐指数
1
解决办法
100
查看次数

使用Scanner时的无限循环 - java

我想询问用户是否要创建一个名为file.elt或不是的文件.我正在尝试使用Scannerclass 使用switch语句.

这是我的代码:

    System.out.println("Do you want to create the file.elt? (Y/N)");
            strOption=sc.nextLine();

        OUTER:
        while (sc.hasNext()) {
            switch (strOption) {
                case "Y":
                case "y":
                    elements.createElements(file);
                    break OUTER;
                case "N":
                case "n":
                    System.out.println("There will be no file.elt created! .");
                    break OUTER;
                default:
                    System.out.println("Please, type Y or N.");
                    strOption=sc.nextLine();
                    break;
            }                
        }
        sc.close();
Run Code Online (Sandbox Code Playgroud)

该sc对象在程序开头声明,我要求提供文件名.

该sc声明是:

    String file;

    Scanner sc = new Scanner(System.in);

    System.out.println("Type the name of the file .dat .");

    file=sc.nextLine();
Run Code Online (Sandbox Code Playgroud)

问题是while循环是无限的,我不知道为什么.

java while-loop java.util.scanner

-3
推荐指数
1
解决办法
1281
查看次数

for循环有多贵?

问题是要求比较成本for和while循环,相对于if陈述的成本.

假设if语句的相对成本为1,for和while循环的相对成本是多少?

假设在比较时将单个布尔值传递给while循环和if语句,并while允许迭代X次.假设它for也迭代X次.

java performance for-loop if-statement while-loop

-3
推荐指数
1
解决办法
2457
查看次数

C++ while循环未运行

我的while循环不会在我设置它的条件下运行.

该计划的目的是使用存款金额,利率和目标金额确定存款金额到期的年数.

程序刚刚输入目标金额后停止,除非我将while语句从<=更改为> =在这种情况下它运行循环但返回在第100轮或1000等设置的年数...

#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>

using namespace std;

int main()
{
    //declare the variables
    double rate,
           balance = 0;
    int deposit,
        target,
        years = 0;
    cout << "****Lets make you some money!****" << endl << endl;
    //input from the user
    cout << "What is your deposit amount?: " << endl;
    cin >> deposit;
    cout << "What is your interest rate?: " << endl;
    cin >> rate;
    cout << "What is you target savings …
Run Code Online (Sandbox Code Playgroud)

c++ while-loop

-3
推荐指数
1
解决办法
168
查看次数