首页 > 解决方案 > 输入选项 2 时程序不会退出,而是要求输入一个正数。这里有什么问题?

问题描述

输入选项 2 时程序不会退出,而是要求输入一个正值。它应该只询问如果选择是 1,那么进入的房间数量少于 1。它应该立即退出程序,但事实并非如此。我能做些什么来解决这个问题?是因为缺少大括号还是多余的大括号。

 import java.util.Scanner;
 import java.text.DecimalFormat;

 public class Paint {
     public static void main(String[] args) {
         DecimalFormat formatter = new DecimalFormat("###0.00");
         Scanner keyboard = new Scanner(System.in);
         double numbGallons;
         double costPerGallon;
         double totalSquareFeet = 0;
         double numbHours;
         double costPerHour;
         double paintCost1;
         double squareFeet;
         int choice;
         int numRooms = 0;
         double laborCost1;
         double totalEstimate;

         do {
             displayMenu();

             choice = keyboard.nextInt();

             if (choice == 1) {
                 System.out.println("How many rooms do you want to paint?");
                 numRooms = keyboard.nextInt();
             }

             while (numRooms < 1) {
                 System.out.println("Please enter a positive value");
                 numRooms = keyboard.nextInt();
             }

             for (int counter = 1; counter <= numRooms; counter++) {
                 System.out.println("How many square feet of room " + counter +
                     " do you want to paint?");
                 squareFeet = keyboard.nextDouble();
                 totalSquareFeet = totalSquareFeet + squareFeet;
             }
             System.out.println("The total square feet is " + totalSquareFeet);

             numbGallons = numGallons(totalSquareFeet);

             numbHours = numHours(totalSquareFeet);

             System.out.println("How much is the price per hour?");
             costPerHour = keyboard.nextDouble();

             System.out.println("How much is the price per gallon?");
             costPerGallon = keyboard.nextDouble();

             laborCost1 = laborCost(costPerHour, numbHours);

             paintCost1 = paintCost(numbGallons, costPerGallon);

             System.out.println("The number of Gallons is " + numbGallons);
             System.out.println("The number of Hours is " + numbHours);
             System.out.println("The labor cost is " + laborCost1);
             System.out.println("The paint cost is " + paintCost1);

             totalEstimate = laborCost1 + paintCost1;
             System.out.println("The total estimate is " + totalEstimate);

         } while (choice != 2);


     }

     public static void displayMenu() {
         System.out.println("1)Calculate Estimate");
         System.out.println("2)Quit the program");
         System.out.println("Please make a selection");
     }

     public static double numGallons(double sqr) {
         return sqr / 115;
     }
     public static double numHours(double sqr) {
         return (sqr / 115) * 8;
     }

     public static double laborCost(double cph, double nh) {
         return cph * nh;
     }

     public static double paintCost(double ng, double cpg) {
         return ng * cpg;
     }
 }

标签: javaloopsmenuexit

解决方案


您应该在进入循环之前和循环结束时显示菜单并获得选择。

public static void main(String[] args) {
    DecimalFormat formatter = new DecimalFormat("###0.00");
    Scanner keyboard = new Scanner(System.in);
    double numbGallons;
    double costPerGallon;
    double totalSquareFeet = 0;
    double numbHours;
    double costPerHour;
    double paintCost1;
    double squareFeet;
    int choice;
    int numRooms = 0;
    double laborCost1;
    double totalEstimate;

    displayMenu();
    choice = keyboard.nextInt();

    while (choice != 2) {

        //if (choice ==1)
        //{     
        System.out.println("How many rooms do you want to paint?");
        numRooms = keyboard.nextInt();
        //} 

        while (numRooms < 1) {
            System.out.println("Please enter a positive value");
            numRooms = keyboard.nextInt();
        }

        for (int counter = 1; counter <= numRooms; counter++) {
            System.out.println("How many square feet of room " + counter +
                " do you want to paint?");
            squareFeet = keyboard.nextDouble();
            totalSquareFeet = totalSquareFeet + squareFeet;
        }
        System.out.println("The total square feet is " + totalSquareFeet);

        numbGallons = numGallons(totalSquareFeet);

        numbHours = numHours(totalSquareFeet);

        System.out.println("How much is the price per hour?");
        costPerHour = keyboard.nextDouble();

        System.out.println("How much is the price per gallon?");
        costPerGallon = keyboard.nextDouble();

        laborCost1 = laborCost(costPerHour, numbHours);

        paintCost1 = paintCost(numbGallons, costPerGallon);

        System.out.println("The number of Gallons is " + numbGallons);
        System.out.println("The number of Hours is " + numbHours);
        System.out.println("The labor cost is " + laborCost1);
        System.out.println("The paint cost is " + paintCost1);

        totalEstimate = laborCost1 + paintCost1;
        System.out.println("The total estimate is " + totalEstimate);

        displayMenu();
        choice = keyboard.nextInt();

    }
}

推荐阅读