質問

I must write a do loop that reads integers and computes their sum. Stop when reading a zero or the same value twice in a row. For example, if the input is 1 2 3 4 4, then the sum is 14 and the loop stops. Same thing if user input is 0. I need to know if I can use multiple parameters for the do while statement or if i need to nest another do while stamenent.

    int input = 0;
    int total = 0;
    int update = 0;

    System.out.println("Enter any number");
    total = inputDevice.nextInt();

    do
    {
        System.out.println("Enter any number");
        input = inputDevice.nextInt();

        // Do i have to nest a do-while statement here?

        total = total + input;

    }while((input != 0); // Or How would i insert a second parameter that stops the loop when a number is entered twice in a row?

                  For the above parameters I know i would do && to start the second parameter, I just cannot figure out the logic statement I would insert as the parameter.

    System.out.println("The sum of the numbers is " + total);
}

}

役に立ちましたか?

解決

You need to save the last input in a variable and then compare it with the actual input (and break the while loop if they're equal)

Try this:

    int last_input = 0;
    int input = 0;
    int total = 0;
    int update = 0;

    Scanner inputDevice = new Scanner(System.in);

    do
    {   
        System.out.println("Enter any number");
        input = inputDevice.nextInt();

        total = total + input;

        if(last_input == input)
            break;

        last_input = input;

    }while(input != 0); // Or How would i insert a second parameter that stops the loop when a number is entered twice in a row?

    System.out.println("The sum of the numbers is " + total);

他のヒント

This is fairly simple and you can do it with a "normal" while loop, e.g. by replacing the do-while statement with

while (true) {
    System.out.println("Enter any number");
    input = inputDevice.nextInt();            
    total = total + input;

    if ((input == 0) || (input == update)) {
        break; // Exit the loop
    } else {
        update = input; // Remember the last value
    }    
}

The above example reuses the update variable which does not seem to be used for anything else. If it is needed for another purpose, it can of course be replaced with another variable, declared in the same area with input.

int prevInput = 0;
bool bFirstRun = true

do
{
    System.out.println("Enter any number");

     if(!bFirstRun) 
      {
         prevInput = input
      }
    input = inputDevice.nextInt();

     bFirstRun = false;


    total = total + input;
     if(prevInput == input)
      break;

}while((input != 0); 

I would add a prevInput parameter as above and a boolean to check if its the first time through the loop. If it is then dont check the previous input, if its not the first time through then check the input to the previous (after it was added to the total) and if its the same then break from the loop.

Here... try this. It's simpler

int num1 = 0, num2 = 0, total = 0;

    do {
        num2 = num1;
        System.out.println("Enter any number");
        num1 = inputDevice.nextInt();
        total += num1;
        System.out.println("The sum of the numbers is " + total);
    } while (num1 != num2 && num1 != 0);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top