【发布时间】: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);
}
}
【问题讨论】: