标签: do-while

做的同时永远循环

我遇到了这种方法的问题,我永远坚持"抱歉你必须指定一个Deeppan或瘦基地,再试一次:"即使我输入jimmy,harry,deeppan,thin,Thin ..等我输入之后deeppan或thin,我希望String存储在变量"size"中并返回

我缺少什么想法?

 public String Input(){


    String size;

    Scanner sc = new Scanner(System.in);
    System.out.println("thin or thick: ");

    do {
        size = scan.next();
        if ( !size.equalsIgnoreCase( "thick") || !size.equalsIgnoreCase( "thin" )) {
            System.out.print("Sorry you must specify a thick or thin base, try again: ");
        } else {
            break;
        }
    } while ( true );

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

java while-loop do-while

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

只识别的int输入为1

无论我输入什么,此函数中唯一识别的int是1.如果选择了其他任何内容,则重复执行do-while循环.没有任何OR运算符,代码也工作正常,例如"while(input!= 0)"

void menu()
{
    int input = -1;
    do
    {
    cout << "         ---------------" << endl << "         -     OU6     -" << endl << "         ---------------" << endl;
    cout << "1. Read a transaction from the keyboard." << endl;
    cout << "2. Print all transactions to console." << endl;
    cout << "3. Calculate the total cost." << endl;
    cout << "4. Get debt of a single person." << endl;
    cout << "5. Get unreliable gold of a single …
Run Code Online (Sandbox Code Playgroud)

c++ do-while

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

使用字符输入进行 while 循环

我写了这个简单的程序,它应该计算用户输入的数字的阶乘。程序应该要求用户停止或继续程序以找到新数的阶乘。

由于大多数时候用户不注意 CapsLock,程序应该接受 Y 或 y 作为是的答案。但是每次我运行这个程序时,即使我输入 Y/y ,它也会被终止!

我用谷歌搜索并发现问题可能是由于new line字符被我的字符输入所接受,所以我修改了 scanf 代码从scanf("%c", &choice);toscanf("%c ", &choice);以适应新行字符,但我的程序在接受 Y/y 作为输入后仍然被终止.

这是代码。如果可能,请让我知道处理此类问题的最佳实践和方法以及所需的更正。

#include<stdio.h>
#include"Disablewarning.h" // header file to disable s_secure warning in visual studio contains #pragma warning (disable : 4996) 

void main() {
    int factorial=1;//Stores the factorial value
    int i; //Counter
    char choice;//stores user choice to continue or terminte the program

        do {//Makes sure the loop isn't terminated until the user decides
            do{
                printf("Enter the no whose …
Run Code Online (Sandbox Code Playgroud)

c char do-while

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

C++:对于具有 do-while 循环的两个不同函数,为什么 x+=y 在一个函数中给出与 x=x+y 相同的结果,而在另一个函数中却不然?

对于下面的函数,与使用相比,A我使用时得到了不同的结果。前者给出的结果为,后者给出的结果为。est += XXXest = est + XXX1.33227e-158.88178e-16

另一方面,对于B下面的函数,无论我是否使用est += XXX或 ,我都会得到相同的结果est = est + XXX

有人能解释为什么x+=y相当于x=x+yin 函数B而不是 in 吗A

功能A

double A(int nTerm){
    const double PI = 3.141592653589793238463;    
    double est = 0;
    double counter = 0;

    do {
        est += ( 16 * pow(-1,counter) )/ (2*counter+1) * pow((double)1/5, 2*counter+1) 
             - ( 4 * pow(-1,counter) )/ (2*counter+1) * pow((double)1/239, 2*counter+1);
        counter++; …
Run Code Online (Sandbox Code Playgroud)

c++ operators do-while

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

为什么即使条件为假,while 循环也会运行?

我是 JavaScript 新手,现在正在学习它的基础知识。我遇到 do...while 循环,它解释了“do”命令将运行一次,然后检查条件。现在这是我的代码: // 设置

const myArray = [];
let i = 10;


do {

}
while (i < 5) 
{
  myArray.push(i);
  i++;
}

console.log(myArray);
Run Code Online (Sandbox Code Playgroud)

由于 do 中没有元素,因此它不应该在 myArray 中添加任何内容。然而,最终结果在 myArray 中显示 10,我很困惑为什么会发生这种情况?请协助。

javascript loops do-while

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

C:似乎无法找到错误[Do-While循环]

该计划很简单:

1)它接受用户的名字2)询问他购买了多少物品以及花费多少3)如果输入任何> 10项,则表示"请输入大于0(零)且小于10(10)的数字你想继续生成另一个账单吗?(是/否)"4)用户输入'Y',它应该把他带到do循环的开头并重新开始.

介于两者之间的一切都很好.这是代码:

int main()
{
    char item[10][10], answer = 'null', name[10];
    int price[10], total_item, total_price = 0, i, j = 0, a;
    float service_tax = 0, vat = 0,total_bill = 0,  bill = 0;

    printf ("Please enter your name: ");
    scanf ("%s", name);

    do
    {
        printf ("\n Enter the total items purchased (must be more than 0 (zero) and less than 10 (ten): ");
        scanf (" %d", &total_item);

        if(total_item > 0 && total_item <= 10)
    {
        for(i = …
Run Code Online (Sandbox Code Playgroud)

c loops do-while

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

为什么这不是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
查看次数

java do while循环无法访问变量

我在java中编写一个简单的文件I/O程序,我的程序允许用户输入他们想要编辑的文件(这将是我使用我的FileReader读取的文件).但是,我想继续提示用户输入文件名,直到他们输入有效文件(已存在的文件).出于某种原因,我的编译器(Eclipse)告诉我在任何地方都没有使用"布尔变量loopFlag".

当我运行程序时,如果用户输入错误的输入(不存在的文件),程序将正确运行并再次提示用户输入文件名.但是,如果用户输入了一个好的输入(已存在的文件),程序将无限循环并仍然提示用户输入文件名.这是我的代码:

String fileName;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // read in user input
boolean loopFlag; // compiler says this is not used anywhere, but I use it in my do-while loop?!?!

do
{
     loopFlag = false;
     System.out.println("Please enter the file name you wish to edit (including extension):");
     fileName = br.readLine(); // set fileName equal to user input
     // this try catch block is to make sure that the file exists
     try
     {
          FileReader fr = new FileReader(ManipulateText.readFileName);
          fr.close(); …
Run Code Online (Sandbox Code Playgroud)

java variables file-io filereader do-while

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

做while while循环没有退出

关于C#的快速问题.我已经在下面显示了我的代码,我不知道为什么循环永远不会关闭,Do While当用户输入"A"或"B"等时循环应该关闭,听起来像是一个愚蠢的问题我刚刚开始C#

do
{
    Console.WriteLine("What one would you like to buy? A B C or D");
    a = Convert.ToChar(Console.ReadLine());
    Console.WriteLine(a);
} while (a != 'A' || a != 'B' || a != 'C' || a != 'D');
Run Code Online (Sandbox Code Playgroud)

c# do-while

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

标签 统计

do-while ×10

c ×3

java ×3

while-loop ×3

c++ ×2

loops ×2

c# ×1

char ×1

file-io ×1

filereader ×1

javascript ×1

operators ×1

variables ×1