如何通过用户输入结束while循环 [英] How to end a while Loop via user input
本文介绍了如何通过用户输入结束while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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("
" +"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 + "
" + "Type 'yes' or 'no' if this is correct: ");
input = keyboard.nextLine();
}
}
}
当用户说是"时程序停止,如果用户说否"则程序只是重复,我将如何做到这一点?我不知道为什么我有这么多麻烦.我已经搜索了 4 个多小时.我认为我只应该使用 while 循环.
How would I make it so when the user says yes the program stop and if the user says no then the program just repeats? I don't know why I'm having so much trouble. I've searched for well over 4 hours. I am only supposed to use a while loop, I think.
推荐答案
你必须在你的 while
循环中分配 repeat
所以它变成 false
如果用户说yes
:
You have to assign repeat
in your while
-loop so it becomes false
if the user says yes
:
repeat = !input.equalsIgnoreCase("yes");
这篇关于如何通过用户输入结束while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文