java-如何通过用户输入结束while循环
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何通过用户输入结束while循环,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1666字,纯文字阅读大概需要3分钟。
内容图文
![java-如何通过用户输入结束while循环](/upload/InfoBanner/zyjiaocheng/692/6018fecd06f345999a31f35bba81db37.jpg)
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();
}
}
}
当用户说“是”时,程序将停止,如果用户说“否”,则该程序会重复执行,我该怎么做?我不知道为什么我遇到这么多麻烦.我搜索了4个多小时.我认为我只能使用while循环.
解决方法:
您必须在while循环中分配重复,因此,如果用户说是,则重复为假:
repeat = !input.equalsIgnoreCase("yes");
内容总结
以上是互联网集市为您收集整理的java-如何通过用户输入结束while循环全部内容,希望文章能够帮你解决java-如何通过用户输入结束while循环所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。