【问题标题】:Do-While loop printing text to prompt user for sentinel value, then restarting before receiving inputDo-While 循环打印文本以提示用户输入标记值,然后在接收输入之前重新启动
【发布时间】:2020-06-28 21:45:49
【问题描述】:

在过去的几个小时里,我一直在努力解决这个问题,并决定我应该寻求一些建议。我正在编写一个代码来计算跑道形标志的平方英尺。然后它根据平方英尺()计算价格,然后提示输入任何文本并按字符收费。它在循环中平稳运行,没有明显问题,但是一旦到达末尾,就会有一个 if/else 序列来搜索哨兵值。问题是,我的代码似乎没有读取 if/else。如果完成,它会提示用户输入“退出”,但它不会收集用户输入,而是直接跳回提示输入计算平方英尺的详细信息。如何让我的 Scanner 接受用户输入并影响循环?

    import java.util.Scanner;

public class DonutSign {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
            //initiate all variables
        boolean ans = false;
        double r = 0;
        double sideA = 0;
        double sideB = 0;
        double angle = 0;
        String text = "INTIAL";
        
        double circleArea = 0;
        double parArea = 0;
        double finalArea = 0;
        double sizePrice = 0;
        double textPrice = 0;
        double finalPrice = 0;
        String response = "quit";
    
            //do-while price calculation
        do{
                //have all of the variables entered
            System.out.println("Enter the radius of the circles:");
            r = input.nextDouble();
            System.out.println("Enter the first side of the parallelogram:");
            sideA = input.nextDouble();
            System.out.println("Enter the second side of the parallelogram:");
            sideB = input.nextDouble();
            System.out.println("Enter the angle of the parallelogram:");
            angle = input.nextDouble();
            System.out.println("Enter the string you would like on your sign:");
            text = input.next();
            
                //calculate area of the circles
            circleArea = Math.PI * Math.pow(r, 2);
                //calculate area of parallelogram
            parArea = sideA * sideB * (Math.sin(Math.toRadians(angle)));
                //add surface area values together and calculate price
            finalArea = circleArea + parArea;
            sizePrice = finalArea * 2.95;
                //calculate price of text and add values
            textPrice = ((text.length() + 1)    * 1.45);
            finalPrice = textPrice + sizePrice;
            System.out.print("$");
            System.out.printf("%.2f", finalPrice);
            System.out.println();
            
            
                //If-else to exit the loop
            System.out.println("Would you like to create another sign? Enter quit to exit.");
            response = input.nextLine();
            if(response == "quit"){
                ans = true;
            }
            else{
                ans = false;
            }
        } while (ans != true);      
    }
}

【问题讨论】:

    标签: java windows window


    【解决方案1】:

    这里有一个很好的答案:

    Java Scanner doesn't wait for user input

    问题是nextInt()不消耗'\n',所以next 调用 nextLine() 消耗它,然后它正在等待读取输入 为你。

    您需要在调用 nextLine() 之前使用 '\n'。

    尝试在input.next()input.nextDouble() 之后添加input.nextLine()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2016-09-26
      • 2019-10-30
      • 1970-01-01
      • 2017-04-03
      • 2016-07-19
      • 1970-01-01
      相关资源
      最近更新 更多