java scanner 结束输入_java-如何通过用户输入结束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("\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-loop,if-statement

来源: https://codeday.me/bug/20191012/1899886.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值