Java Homework用户输入问题 [英] Java Homework user input issue

查看:153
本文介绍了Java Homework用户输入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类分配来使用Scanner读取数据。

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");
    }
}

底部是我的测试找出每个标志是否正常运行。然而我设置的while循环似乎不想继续运行多次。它将采用一个值,然后如果我说是我有更多的值(y或Y)它将退出并打印鲣鸟

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

推荐答案

基本上发生了什么,
当您正在读取double.inxtDouble()时,您只读取double,但是当您在未读取的流上按Enter键时会出现ENDLINE字符通过scan.nextDouble()。
因此当你到达any1 = scan.nextLine()时,它会读取不等于Y或y的结束字符。结果它退出while循环。
这样修改你的代码,只需改变一行你正在阅读的内容:

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

这篇关于Java Homework用户输入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆