Java Homework user input issue [closed]

霸气de小男生 提交于 2019-12-02 13:27:57
Jimmy

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 :)
}

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.

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

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 :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!