Java的作业用户输入的问题[关闭](Java Homework user input issue

2019-08-01 02:35发布

我有一个类分配使用扫描仪中读取数据。

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

底部只是有一个测试,我发现如果everythign是正常的运行。 然而while循环我有安装似乎并不想继续运行不止一次。 如果我说是的,我有更多的值(y或)将采取一个值,那么它只会退出,请打印鲣鸟

Answer 1:

基本上什么是happenning是,当你正在为scan.nextDouble(),你只是读读书双重双,但是会有ENDLINE字符,当你打的()不是由scan.nextDouble读取流进入。 所以,当你到达ANY1 = scan.nextLine(),它读取这不等于y或行尾字符。 因此,它退出while循环。 纠正你这样的代码,只要改变一条线在那里你正在阅读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 :)
}


Answer 2:

您应该能够通过运行在调试器下的应用程序或加入一些诊断跟踪打印算出这个自己; 例如,像这样在适当的点(一个或多个)。

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

(我敢打赌,这将表明, any不包含你指望它有什么...)

我强烈建议你要学会调试自己的代码,而不是要求别人的帮助。 这是一个专业的软件工程师的关键技能。



Answer 3:

这似乎为我工作得很好...

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

}

哪一种乐不可支,因为我敢肯定,有没有太大的差别

while(more==true)不是必需的while(more)说同样的事情。

if(any1.equals("y") || any1.equals("Y"))不是必需的, if (any1.equalsIgnoreCase("y"))会做同样的事情



Answer 4:

对于更清洁和更可读的代码,宁可在这种情况下do..while块因为现在不需要more的变量:

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

对于一门功课,这是相当不错的,我认为。 对于项目中的实际工作,你应该在很多小方法,使感测和避免最冗余的重构。 这可能是您的第二个作业:优化呢:)



文章来源: Java Homework user input issue [closed]