Do-While 循环打印文本以提示用户输入哨兵值,然后在接收输入之前重新启动

huangapple 未分类评论54阅读模式
英文:

Do-While loop printing text to prompt user for sentinel value, then restarting before receiving input

问题

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 = "INITIAL";

        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.equals("quit")) {
                ans = true;
            } else {
                ans = false;
            }
        } while (!ans);
    }
}
英文:

I have been trying to work around this for the last few hours and decided I should ask for some advice. I am writing a code that calculates the square footage of a running-track-shaped sign. it then calculates the price based on square footage (), and then prompts for any text and charges per character. It runs smoothly through the loop without apparent issue, but once it reaches the end there is an if/else sequence to search for a sentinel value. The problem is, my code isn't seeming to read that if/else. it prompts for the user to enter "quit" if they are done, but instead of collecting user input it jumps right back to prompting for details to calculate square footage. how do i get my Scanner to accept the users input and affect the loop?

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

答案1

得分: 0

这里有一个很好的回答:

https://stackoverflow.com/questions/23450524/java-scanner-doesnt-wait-for-user-input

问题在于 nextInt() 没有消耗掉 '\n',因此对 nextLine() 的下一次调用会消耗掉它,然后它会等待读取 y 的输入。

在调用 nextLine() 之前,你需要消耗掉 '\n'。

在你的 input.next()input.nextDouble() 后面尝试添加 input.nextLine()

英文:

There is a good answer here:

https://stackoverflow.com/questions/23450524/java-scanner-doesnt-wait-for-user-input

> The problem is that nextInt() does not consume the '\n', so the next
> call to nextLine() consumes it and then it's waiting to read the input
> for y.
>
> You need to consume the '\n' before calling nextLine().

Try adding an input.nextLine() after your input.next() or input.nextDouble()

huangapple
  • 本文由 发表于 2020年6月29日 05:45:49
  • 转载请务必保留本文链接:https://java.coder-hub.com/62628562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定