-2

I need help getting this loop to work. It may be because I don't really know how to use loops or the if statements. But I think I needed to use the If statements when it comes to receiving a free yogurt after 10.

import java.util.Scanner;

public class Foothill {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        int numPurchases, totalPurchased;
        boolean correct;
        String menu;
        numPurchases = 0;
        totalPurchased = 0;
        correct = false;
        while(!correct) 
        {
            System.out.println("Menu:");
            System.out.println("  P (Process Purchase)");
            System.out.println("  S (Shut down)");
            menu = scanner.nextLine();
            if (menu.charAt(0)=='S' || menu.charAt(0)=='s'){
                System.exit(0);
            } else if (menu.charAt(0)=='P' || menu.charAt(0)=='p'){
                continue;
            }
            System.out.println("Your choice: " + menu.charAt(0));
            System.out.println("How many yougurts would you like to buy?");
            numPurchases = scanner.nextInt();
            totalPurchased += numPurchases;
            if (totalPurchased > 0 || totalPurchased < 10){
                System.out.println("you have purchased " + totalPurchased + " yogurts");
            } else if (totalPurchased >= 10){
                System.out.println("Congradulations you have recieve a free Yogurt");
                System.out.println("Remainding yogurt credits: " + (totalPurchased-10));
            }
        }
    }
}

I don't really know how to use loops that well so that may be my problem because I have never made a loop using false and true

RacerNerd
  • 1,551
  • 1
  • 13
  • 30
mwaymouth
  • 13
  • 4

3 Answers3

0

Just remove the the following statement, then the loop will work:

else if (menu.charAt(0)=='P' || menu.charAt(0)=='p'){
                continue;
            }
Weibo Li
  • 3,277
  • 2
  • 22
  • 34
0

You had a number of problems with your code. Here is the solution:

import java.util.Scanner;

public class Foothill {
    public static void main(String args[]){
          Scanner scanner = new Scanner(System.in);
          int numPurchases, totalPurchased;
          boolean correct;
          String menu;
          numPurchases = 0;
          totalPurchased = 0;
          correct = false;
          while(!correct){
              System.out.println("Menu:");
              System.out.println("  P (Process Purchase)");
              System.out.println("  S (Shut down)");
              menu = scanner.next();
              if (menu.charAt(0)=='S' || menu.charAt(0)=='s'){
                System.exit(0);
              }
              else if (menu.charAt(0)=='P' || menu.charAt(0)=='p'){
                  System.out.println("Your choice: " + menu.charAt(0));
                  System.out.println("How many yougurts would you like to buy?");
                  numPurchases = scanner.nextInt();
                  totalPurchased += numPurchases;
              }

              if (totalPurchased > 0 && totalPurchased < 10){
                 System.out.println("you have purchased " + totalPurchased + " yogurts");
              }
              else if (totalPurchased >= 10){
                 System.out.println("Congradulations you have recieve a free Yogurt");
                 System.out.println("Remainding yogurt credits: " + (totalPurchased-10));

              }
          }
          scanner.close();
      }
  }

Like mentioned above, you probably need some way to break out of the while loop by setting correct to true.

Alvin Bunk
  • 7,106
  • 3
  • 26
  • 40
  • Oh, also make sure you close Scanners to prevent resource leaks. Another side point, your code doesn't check for invalid input... – Alvin Bunk Feb 08 '14 at 06:27
0

As user2357112 has stated in the comments, the || operator is inappropriate for testing if a number is in a given range since that condition would be true for any number.

You could change the condition to use && instead but I don't know why you'd only display that message on a certain condition. You could print it at the end of each purchase and then print an extra message if the user has received a freebie.

System.out.println("you have purchased " + totalPurchased + " yogurts");

if (totalPurchased >= 10){
    System.out.println("Congradulations you have recieve a free Yogurt");
    System.out.println("Remainding yogurt credits: " + (totalPurchased-10));
}

You also need to know that Scanner#nextInt() does NOT consume the trailing new-line character from user input. Scanner#nextLine() however, trims this character from the read input. As a result you'll get a StringIndexOutOfBoundsException after making a purchase since the Scanner#nextLine() to read in the user's choice at the menu will eat up the trailing new-line character and your program will try to get the first character of an empty String.

As stated in the linked thread you can fix this by calling Scanner#nextLine() again after reading integer input to remove the new-line character from the buffer like so:

numPurchases = scanner.nextInt();
scanner.nextLine();
Community
  • 1
  • 1
PakkuDon
  • 1,631
  • 4
  • 21
  • 21