gpt4 book ai didi

java - 如何通过用户输入结束 while 循环

转载 作者:行者123 更新时间:2023-12-03 22:52:38 27 4
gpt4 key购买 nike

package cst150zzhw4_worst;

import java.util.Scanner;

public class CST150zzHW4_worst {

public static void main(String[] args) {
//Initialize Variables
double length; // length of room
double width; // Width of room
double price_per_sqyd; // Total carpet needed price
double price_for_padding; // Price for padding
double price_for_installation; // Price for installation
String input; // User's input to stop or reset program
double final_price; // The actual final price
boolean repeat = true;

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

while (repeat)
{
//User Input

System.out.println("\n" +"What is the length of the room?: ");
length = keyboard.nextInt();

System.out.println("What is the width of the room?: ");
width = keyboard.nextInt();

System.out.println("What is the price of the carpet per square yard?: ");
price_per_sqyd = keyboard.nextDouble();

System.out.println("What is the price for the padding?: ");
price_for_padding = keyboard.nextDouble();

System.out.println("What is the price of the installation?: ");
price_for_installation = keyboard.nextDouble();

final_price = (price_for_padding + price_for_installation + price_per_sqyd)*((width*length)/9);

keyboard.nextLine(); //Skip the newline

System.out.println("The possible total price to install the carpet will be $" + final_price + "\n" + "Type 'yes' or 'no' if this is correct: ");
input = keyboard.nextLine();

}
}
}

当用户说"is"时程序停止,如果用户说“否”则程序只是重复?我不知道为什么我有这么多麻烦。我已经搜索了 4 个多小时。我认为我只应该使用 while 循环。

最佳答案

您必须在 while 循环中分配 repeat,以便在用户说 yes 时它变为 false :

repeat = !input.equalsIgnoreCase("yes"); 

关于java - 如何通过用户输入结束 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18975208/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com