-2

我有一个使用扫描仪读取数据的课程作业。

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),它将退出并打印任一鲣鸟

4

4 回答 4

7

基本上发生的事情是,当您以 scan.nextDouble() 读取双精度时,您仅读取双精度,但是当您在 scan.nextDouble() 未读取的流上按 Enter 键时会出现 ENDLINE 字符。因此,当您到达 any1=scan.nextLine() 时,它会读取不等于 Y 或 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 :)
}
于 2012-10-03T02:07:21.850 回答
2

您应该能够通过在调试器下运行应用程序或添加一些诊断跟踪打印来自己解决这个问题;例如在适当的点上这样的事情。

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

(我敢打赌,这将表明它any不包含您期望的内容......)

我强烈建议您学习调试自己的代码,而不是请其他人帮忙。这是专业软件工程师的一项关键技能。

于 2012-10-03T02:07:44.300 回答
1

这对我来说似乎很好......

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"))会做同样的事情

于 2012-10-03T02:00:18.050 回答
1

为了获得更干净、更易读的代码,最好在这种情况下使用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"));
}

对于家庭作业,我认为这很好。对于项目中的实际工作,您应该重构许多有意义的小方法并避免大部分冗余。这可能是你的第二功课:优化它:)

于 2012-10-03T02:16:22.787 回答