标签: while-loop

而循环和cin

下面的代码应该询问用户他的体重,如果用户输入> 15,它应该执行一个while循环将其增加到50代码编译成功,但无论你输入> 15它都不会执行while循环.

#include <iostream>
using namespace std;

int main()
{


int weight;

cout << "Current weight?" << endl;
cin >> weight;
cout << "Your weight:" << weight << endl;
if (weight > 15) {
        while (weight == 50) {
            weight = weight + 1;
            cout << "Weight:" << weight << endl;
        }
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ while-loop

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

Java,Do-While循环问题的新手

我对Java比较新,我在运行这个do-while循环时遇到了一些麻烦.

问题1

编写一个程序,允许用户将度数从摄氏度到华氏度或华氏度转换为摄氏度.使用以下公式:Degrees_C = 5(Degrees_F - 32)/ 9 Degrees_F =(9(Degrees_C)/ 5)+ 32提示用户输入温度,C或c表示摄氏度或F或f表示华氏度.如果输入摄氏度,则将温度转换为华氏温度;如果输入华氏温度,则将温度转换为摄氏温度.以可读格式显示结果.如果输入C,c,F或f以外的任何内容,则打印错误消息并停止.

问题2

让我们继续问题1,但使用循环以便用户可以转换其他温度.如果用户在温度之后输入大写或小写的C或F以外的字母,则打印错误消息并要求用户重新输入有效选择.每次转换后,要求用户键入Q或q退出或按任何其他键重复循环并执行另一次转换

public class Lab_4 {

    public static void main(String arg[]) {
        Scanner input = new Scanner(System.in);
        double F; //Fahrenheit
        double C; //Celsius
        String method; 
        boolean keepGoing = true; 
        do {            
            System.out.println("Choose a method:  ");
            System.out.println("(F)  Fahrenheit to Celsius.  ");
            System.out.println("(C)  Celsius to Fahrenheit.  ");
            System.out.println("(Q)  Exit the loop.  ");
            method = input.nextLine();
            if (method.charAt(0) == 'f' || method.charAt(0) == 'F') {
                System.out.println("Method F");
                System.out.println("Enter the temperature in …
Run Code Online (Sandbox Code Playgroud)

java loops boolean while-loop

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

包括C中循环的第一次迭代

我编写了这个程序,为用户提供所有急性三角形解决方案,当它们给出最小值和最大值时.它正常工作,除了似乎解决方案不包括循环的第一次迭代(例如,min = 1和max = 45,(1,1,1)不包括在解决方案中).怎么解决这个问题?我认为问题在于我的while循环的a值,但是当我将其更改为for循环时,问题仍然存在.我怎么能包含这些第一个值?

if (c >= dmin) {
    c = dmin;
    do {
        ++c;
        for (b = dmin; b < dmax; b++) {
            a = dmin;
            while (a < dmax) {
                a = a + 1;
                if (a * a + b * b - c == c * c) {
                    if (((a + b) > c) && ((b + c) > a) && ((c + a) > b)) {
                        printf("(%d, %d, %d)", a, b, c);
                    }
                } …
Run Code Online (Sandbox Code Playgroud)

c for-loop while-loop do-while

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

BigInteger.intValue()> 1给出错误的布尔值

我正在尝试将BigInteger数转换为二进制数.我使用while循环来减少BigInteger,直到它等于1,在循环运行时取余数.

循环的条件是:(decimalNum.intValue()> 1).

但程序只进行一次循环然后认为BigInteger小于/等于1,而实际上它大约是55193474935748.为什么会发生这种情况?

("inBinary"是一个ArrayList,用于保存循环中的余数.)

这是while循环:

while (decimalNum.intValue()>1){
      inBinary.add(0, decimalNum.mod(new BigInteger("2")).intValue()); //Get remainder (0 or 1)
      decimalNum = decimalNum.divide(new BigInteger("2")); //Reduce decimalNum
}
Run Code Online (Sandbox Code Playgroud)

java math conditional biginteger while-loop

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

C中的简单循环代码

这个程序中的循环不能像我期望的那样工作:

int main() {
    char userresponse;
    char x;

    do {
        printf("are you human");
        scanf("%c", &userresponse);
        if (userresponse == 'y') {
            printf("welcome");
        }
        if (userresponse == 'y') {
            printf("please leave");
        }
        printf("type z to repeat");
        scanf("%c", &x);
    } while (x == 'z');

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

它得到"键入z重复",然后它完成.当我尝试定义char x='z';程序时第一次运行正常,但最后它循环播放2或4次,在语句中显示所有printf消息,甚至是else那些消息if.

我希望程序等待循环底部的输入,并打破循环或继续基于该输入循环.为什么不这样做?

c loops while-loop

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

Java:在Byte Stream中循环

有人可以解释为什么循环检查条件是否不同于-1?

while ((c = in.read()) != -1) {
    out.write(c);
}
Run Code Online (Sandbox Code Playgroud)

java file-io while-loop

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

在java中除以while循环

我正在尝试做的是使用while循环来检查userNum%2> 1一旦它击中一个我希望它停止并打印出除法的所有值,例如

如果20是用户num,它将生成.20/2和10/2和5/2然后2/2导致1然后停止(整数除法)

import java.util.Scanner;

public class DivideByTwoLoop {
   public static void main (String [] args) {
      int userNum = 0;

      userNum = 20;

      while ( (userNum % 2) > 1){
         userNum = userNum / 2;
         System.out.println(userNum );
      }

      System.out.println("");

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

这就是我到目前为止所拥有的.任何帮助是极大的赞赏.

java while-loop integer-division

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

如何在C++中将while循环转换为for循环?

while (counter < total)
{
inFile >> grade;
sum += grade;
counter++;
}
Run Code Online (Sandbox Code Playgroud)

上面是我原始程序的while循环,下面是我尝试将其转换为for循环.

for (counter = 0; counter < total; counter++)
{
    inFile >> grade;        
    cout << "The value read is " << grade << endl;
    total = total + grade;  
}
Run Code Online (Sandbox Code Playgroud)

这是一个简单的程序来获得平均成绩.这是整个计划:

#include <iostream>
#include <fstream>
using namespace std;
int average (int a, int b);
int main()
{
// Declare variable inFile
    ifstream inFile;
// Declare variables
    int grade, counter, total;
// Declare and initialize sum
    int sum …
Run Code Online (Sandbox Code Playgroud)

c++ for-loop while-loop

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

向用户询问H.

我想问用户(使用Scanner)他希望程序循环多少次.我需要使用do-while循环语句来执行此操作,但每当我输入我希望它循环的次数时,它只会从我放入的值中减去它,然后重复该数量.

例如,我输入4,程序重复16次而不是4次.

这是我的计划:

System.out.println("Enter range");
int y = input.nextInt();
int x = 10;
do {
    System.out.print("value of x : " + x );
    x++;
    y++;
    System.out.print("\n");
} while(y < 20);
Run Code Online (Sandbox Code Playgroud)

我的问题在哪里?

java loops while-loop do-while java.util.scanner

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

这段C代码是否可以编译?

我的C编程老师声称这是C中while循环的正确声明.

int main(int argc, char *argv[])
{
    while()
    {
        fork();
    }
}
Run Code Online (Sandbox Code Playgroud)

据他说,它与写作完全相同:

int main(int argc, char *argv[])
{
    while(1)
    {
        fork();
    }
}
Run Code Online (Sandbox Code Playgroud)

'while()'代码是否可以在任何C标准中编译?

c while-loop

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