2

i'm trying to create a program where the number that the user has input would decrease by a certain amount. something that would like this:

Update by (Increment/Decrement):decrement
Enter starting number:15
Enter update number:3
Enter end number:3
loop#1  value=15
loop#2  value=12

the end number is where the loop would stop and the update number is how much the starting number should decrement by. so far this is the code I have and I'm stuck on how to keep the loop going until the end number.

package jaba;
import java.util.Scanner;


public class loop {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    Scanner input = new Scanner(System.in);

    System.out.print("Update by (Increment/Decrement):");
    String Decrement = scan.nextLine();
    
    System.out.print("Enter starting number:");
    String number = scan.nextLine();
    
    System.out.print("Enter update number:");
    String upnumber = scan.nextLine();
    
    System.out.print("Enter end number:");
    String endnumber = scan.nextLine();
    
    
    
    int i,j;
    
    i = 15;
    j = 1;
    
    do {
        
        System.out.println("loop#" +j+ "\tvalue="+i);
        j++;
        
        
        
        }while(i<15);
            
        i = i-3;
        
        System.out.println("loop#" +j+ "\tvalue="+i);
        };
        
        
}
coderx
  • 23
  • 5
  • 2
    Hint: your `do/while` loop has a termination condition that depends on `i`, but the body of the loop doesn't change `i`... so it will either execute exactly once, or *forever*. – Jon Skeet Feb 23 '21 at 08:43

3 Answers3

0

how about something like this:

public class loop {

    public enum Operation {INCREMENT, DECREMENT, INVALID}

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.print("Update by (Increment/Decrement):");
        String operationString = scan.nextLine();

        System.out.print("Enter starting number:");
        String numberString = scan.nextLine();

        System.out.print("Enter update number:");
        String upnumberString = scan.nextLine();

        System.out.print("Enter end number:");
        String endnumberString = scan.nextLine();

        // Determine and parse stuff
        int startNumber = Integer.parseInt(numberString);
        int updateNumber = Integer.parseInt(upnumberString);
        int endNumber = Integer.parseInt(endnumberString);
        // Parse operation, but assume invalid operation
        Operation operation = Operation.INVALID;
        if (operationString.equalsIgnoreCase("increment")) {
            operation = Operation.INCREMENT;
        } else if (operationString.equalsIgnoreCase("decrement")) {
            operation = Operation.DECREMENT;
        }

        // now do the "meat" of the assignment
        int loopNumber = 0; // we'll keep the loop number as a separate counter
        switch (operation) {
            case INCREMENT:
                for (int i = startNumber; i < endNumber; i = i + updateNumber) {
                    loopNumber++;
                    performAssignmentPrinting(loopNumber, i);
                }
                break;

            case DECREMENT:
                for (int i = startNumber; i > endNumber; i = i - updateNumber) {
                    loopNumber++;
                    performAssignmentPrinting(loopNumber, i)
                }
                break;

            case INVALID:
            default:
                throw new IllegalStateException("Please enter supported operation! (increment/decrement)");
        }
    }

    private static void performAssignmentPrinting(int loopNumber, int value) {
        System.out.println("loop#" + loopNumber + "\tvalue=" + value);
    }
}

or the do/while version:

        // now do the "meat" of the assignment
        int currentNumber = startNumber;
        int loopNumber = 0; // we'll keep the loop number as a separate counter

        do {
            loopNumber++;
            switch (operation) {
                case INCREMENT:
                    currentNumber += updateNumber;
                    performAssignmentPrinting(loopNumber, currentNumber);
                    break;

                case DECREMENT:
                    currentNumber -= updateNumber;
                    performAssignmentPrinting(loopNumber, currentNumber);
                    break;

                case INVALID:
                default:
                    throw new IllegalStateException("Please enter supported operation! (increment/decrement)");
            }

        } while (currentNumber != endNumber);

Shark
  • 6,513
  • 3
  • 28
  • 50
  • while I understand this is a school assignment, consider the following input: `startNumber = 1000`, `endNumber = -1000`, `updateNumber = 100`, and see what happens when you enter INCREMENT and DECREMENT. Notice the difference in the number of loops when you increment. It should be somewhere around 21474846 if i'm not mistaken. CC @coderx – Shark Feb 23 '21 at 09:55
  • this solution might also not work for the input i stated above, as it presumes that in case of incrementing the start number will be less than the end number, and vice versa for decrementing. – Shark Feb 23 '21 at 09:57
  • @coderx added the do/while version as well which should work for the counter example i stated in my first comment. – Shark Feb 23 '21 at 10:04
  • hi! thank you so much for the additional information and for the source code :) the source code really helped me understand the logic and concept of the assignment! thank you so much again! – coderx Feb 23 '21 at 12:14
  • @coderx for bonus points, add in a input validation check to warn the user if he enters `0` for update number and prompt for input one more time. – Shark Feb 24 '21 at 09:55
0

you have i = i-3; out of loop. Move decrementation of i into loop:

    do {
        System.out.println("loop#" + j + "\tvalue=" + i);
        j++;
        i = i - 3;
    } while (i > endnumber);
Victor1125
  • 642
  • 5
  • 16
-2

For loop is the solution for your program. for(a ; b ; c) {...}
Google how a for loop works. And try to understand how the 3 parts a,b,c works.

Pseudo:

// if decrease mode
    // for (i = upperbound ; i >= lowerbound ; i-= decrement)
        // print i

// if increase mode
    // for (i = lowerbound ; i <= upperbound ; i+= increment)
        // print i

Update: This is sufficient to get you started and add more validation on your journey.

zhuhang.jasper
  • 4,120
  • 4
  • 21
  • 29