Java Homework user input issue [closed]

2019-03-07 08:09发布

问题:

I have a class assignment to read in data using Scanner.

import java.util.Scanner;

public class Project23
{
    public static void main(String[] args)
    {
        // Declarations and instantiations.
        Scanner scan = new Scanner(System.in);
        String any = "";
        boolean more = false;
        double purchase = 0.0;

        // Ask if user would like to run program?
        System.out.print("Do you have any purchases? Y/N?: ");

        // Ready value into string.
        any = scan.nextLine();
        System.out.println();

        // If any is equal to y or Y it will set the value of more to true
        // this runs the while statement below.
        more = any.toUpperCase().equals("Y");

        // While more is still true continue to run the code within the brackets.
        while (more)
        {
            System.out.print("Please input purchase amount: ");
            purchase += scan.nextDouble();
            System.out.println();

            System.out.print("Do you have any more purchases Y/N?: ");
            any = scan.nextLine();
            System.out.println();

            more = any.toUpperCase().equals("Y");
        }

        if (purchase >= 2500)
            System.out.println("Purchase >= 2500");
        else
            System.out.println("Purchase < 2500");
    }
}

The bottom part is just there as a test for me to find out if everythign is running alright. However the while loop i have setup doesn't seem to want to continue running more than once. It will take in one value, then if I say yes I have more values (y or Y) it will just exit and print either boobies

回答1:

Basically what is happenning is , While you are reading double as scan.nextDouble(), you are reading only the double, however there would be ENDLINE character when you hit enter on the stream that is not read by scan.nextDouble(). So when you reach the any1=scan.nextLine(), it reads the endline character which does not equal to Y or y. As a result it exits the while loop. correct your code like this, just make change one line where you are reading doubel :

while(more){

    System.out.print("Please input purchase amount: ");
    purchase+=Double.parseDouble(scan.nextLine());
    System.out.println();
    System.out.print("Do you have any more purchases Y/N?: ");
    // scan.nextLine();
    String any1=scan.nextLine();
    System.out.println();       
    more = any1.equals("y") || any1.equals("Y");//Shortened :)
}


回答2:

You should be able to figure this out yourself by EITHER running the application under a debugger OR adding some diagnostic trace prints; e.g. something like this at the appropriate point(s).

    System.out.println("any is ==>'" + any + "'<==");

(I bet that this will show that any doesn't contain what you expect it to ...)

I strongly recommend that you learn to debug your own code rather than asking other people to help. This is a critical skill for a professional software engineer.



回答3:

This seems to work fine for me...

boolean more = true;
while (more) {

    System.out.print("Do you have any more purchases Y/N?: ");
    String any1 = scan.nextLine();
    System.out.println();
    if (any1.equalsIgnoreCase("y")) {
        more = true;
    } else {
        more = false;
    }

    System.out.println(more);

}

Which kind of freaks me out cause I'm sure there's not that much difference

while(more==true) is not required while(more) says the same thing.

if(any1.equals("y") || any1.equals("Y")) is not required, if (any1.equalsIgnoreCase("y")) will do the same thing



回答4:

For a cleaner and more readable code, prefer in this case do..while block since now no need for more variable:

public static void main(String[] args) {
    double purchase = 0.0;
    Scanner scan = new Scanner(System.in);
    System.out.print("Do you have any more purchases Y/N?: ");
    if (!scan.nextLine().equalsIgnoreCase("y")) {
        return;
    }
    do {
        System.out.print("Please input purchase amount: ");
        purchase += Double.parseDouble(scan.nextLine());
        System.out.print("Do you have any more purchases Y/N?: ");
    }
    while (scan.nextLine().equalsIgnoreCase("y"));
    System.out.println("Your purchase amount is: " + purchase + " and so is " + (purchase >= 2500 ? ">= 2500" : "< 2500"));
}

For a homework, that's pretty good I think. For a real work in a project, you should refactor in many small methods that make sense and avoid most of redundancy. That could be your secondary homework: optimize it :)