[英]How can I make my do while loop work the way I want it to?
如果用戶對“ Continue?:( y / n)”提示回答是,我試圖使代碼循環提示輸入矩形的長度和寬度。
當前正在循環“ Continue?:( y / n)”部分,而不是“ Enter Length:”和“ Enter Width:”部分。 任何幫助表示贊賞!
import java.util.Scanner;
public class Day1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Grand Circus' Room Detail Generator!");
double length = 0.0;
double width = 0.0;
double area = 0.0;
double perimeter = 0.0;
System.out.println("Enter Length: ");
length = scanner.nextInt();
System.out.println("Enter Width: ");
width = scanner.nextInt();
area = length * width;
perimeter = 2 * (length + width);
System.out.println("Area: " + area);
System.out.println("Perimeter: " + perimeter);
Scanner sc = new Scanner(System.in);
String response;
do {
System.out.print("Continue? (y/n):");
response = sc.next();
} while (response.equals("y") || response.equals("Y"));
}
}
放
Scanner sc = new Scanner(System.in);
String response;
do {
之前
System.out.println("Enter Length: ");
為什么要重復循環主體之外的任何內容( do {
和} while (...)
之間} while (...)
)?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.