【问题标题】:My code wont continue after null shows upnull 出现后我的代码不会继续
【发布时间】:2025-12-24 14:15:11
【问题描述】:

这是我的代码

import java.util.Scanner;

public class Paint1 {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        double wallHeight = 0.0;
        double wallWidth = 0.0;
        double wallArea = 0.0;
        double gallonsPaintNeeded = 0.0;
        
        final double squareFeetPerGallons = 350.0;
        
        
            // Implement a do-while loop to ensure input is valid
            // Prompt user to input wall's height
        try {
            System.out.println("Enter wall height (feet): ");
            wallHeight = scnr.nextDouble();
            
            if (wallHeight <= 0){
                throw new Exception("Invalid number" );
            }
            
            
            
            // Implement a do-while loop to ensure input is valid
            // Prompt user to input wall's width
      
            System.out.println("Enter wall width (feet): ");
            wallWidth = scnr.nextDouble();
            
            if (wallWidth <= 0) {
                throw new Exception("invalid number");
            }
                
            // Calculate and output wall area
            wallArea = wallHeight * wallWidth;
            System.out.println("Wall area: " + wallArea + " square feet");
        }
        catch (Exception excpt) {
            System.out.println(excpt.getMessage());
            System.out.println("Cannot compute wall area");
        }
      
       // Calculate and output the amount of paint (in gallons) needed to paint the wall
       gallonsPaintNeeded = wallArea/squareFeetPerGallons;
       System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");

我的代码适用于第一组输入,即 30 和 25。我不知道如何让我的代码在下一个输入为“30”和 25 后继续运行。程序在给我 null 之后就停止了,但是它需要在“三十”之后继续。有没有人知道如何帮助我。

【问题讨论】:

    标签: java eclipse while-loop try-catch


    【解决方案1】:

    在调用nextDouble 之前,您需要检查扫描仪是否有适当的令牌hasNextDouble 并吞下这样的错误输入:

    while (!scnr.hasNextDouble()) {
        scnr.next(); // skip "not a number" token
    }
    wallHeight = scnr.nextDouble();
    

    然后无效的“非双精度”输入将被悄悄丢弃,直到输入一个数字,然后您验证它是否正确(大于 0)。

    类似地,您等到输入有效数字后,才输出参数无效的消息。

    一旦两个输入值都有效,您计算必要的输出并退出而不处理异常(如果发生任何异常,将被重新抛出)。

    完整代码:

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        double wallHeight = 0.0;
        double wallWidth = 0.0;
        double wallArea = 0.0;
        double gallonsPaintNeeded = 0.0;
        
        final double squareFeetPerGallons = 350.0;
        // loop for height
        while (wallHeight <= 0.0) {
            System.out.print("Enter wall height (feet): ");
            while (!scnr.hasNextDouble()) {
                scnr.next();
            }
            wallHeight = scnr.nextDouble();
            
            if (wallHeight <= 0){
                System.out.println("Invalid wallHeight");
            }
        }
        // loop for width
        while (wallWidth <= 0) {  
            System.out.print("Enter wall width (feet): ");
            while (!scnr.hasNextDouble()) {
                scnr.next();
            }
            wallWidth = scnr.nextDouble();
            
            if (wallWidth <= 0) {
                System.out.println("invalid wallWidth");
            }
        }      
        wallArea = wallHeight * wallWidth;
        System.out.println("Wall area: " + wallArea + " square feet");
            
        // Calculate and output the amount of paint (in gallons) needed to paint the wall
        gallonsPaintNeeded = wallArea/squareFeetPerGallons;
        System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");
    }
    

    示例输出

    Enter wall height (feet): twelve
    -20
    Invalid wallHeight
    Enter wall height (feet): 20
    Enter wall width (feet): no
    0
    invalid wallWidth
    Enter wall width (feet): 15
    Wall area: 300.0 square feet
    Paint needed: 0.8571428571428571 gallons
    

    【讨论】:

      【解决方案2】:

      我假设您希望您的应用程序继续运行并再次提示用户输入相同的输入?这可以通过一个 while 循环来完成,该循环只是继续评估您已经编写的代码。应该这样做:

      import java.util.Scanner;
      
      public class Stack {
      
        static Scanner scnr = new Scanner(System.in);
      
        public static void main(String[] args) {
          while(true) {
            computePaint();
          }
        }
      
        private static void computePaint() {
            double wallHeight = 0.0;
            double wallWidth = 0.0;
            double wallArea = 0.0;
            double gallonsPaintNeeded = 0.0;
      
            final double squareFeetPerGallons = 350.0;
      
            // Implement a do-while loop to ensure input is valid
            // Prompt user to input wall's height
            try {
              System.out.println("Enter wall height (feet): ");
              wallHeight = scnr.nextDouble();
      
              if (wallHeight <= 0) {
                throw new Exception("Invalid number");
              }
      
              // Implement a do-while loop to ensure input is valid
              // Prompt user to input wall's width
      
              System.out.println("Enter wall width (feet): ");
              wallWidth = scnr.nextDouble();
      
              if (wallWidth <= 0) {
                throw new Exception("invalid number");
              }
      
              // Calculate and output wall area
              wallArea = wallHeight * wallWidth;
              System.out.println("Wall area: " + wallArea + " square feet");
            } catch (Exception excpt) {
              System.out.println(excpt.getMessage());
              System.out.println("Cannot compute wall area");
            }
      
            // Calculate and output the amount of paint (in gallons) needed to paint the wall
            gallonsPaintNeeded = wallArea / squareFeetPerGallons;
            System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");
      
          }
        }
      

      如果有必要,确实应该有一些东西可以中断这个执行,但我认为while(true) 说明了这一点。

      【讨论】:

        【解决方案3】:
        You are providing a string value to a double.
        So thats the reason you are getting the error.
        You can modified the code to -
        
        public static void main(String[] args) {
                Scanner scnr = new Scanner(System.in);
                double wallHeight = 0.0;
                double wallWidth = 0.0;
                double wallArea = 0.0;
                double gallonsPaintNeeded = 0.0;
        
                final double squareFeetPerGallons = 350.0;
        
        
                // Implement a do-while loop to ensure input is valid
                // Prompt user to input wall's height
                try {
                    while (wallHeight <= 0)
                        wallHeight = getNumber(scnr, "Enter wall height (feet): ");
                    while (wallWidth <= 0)
                        wallWidth = getNumber(scnr, "Enter wall width (feet):  ");
                    // Calculate and output wall area
                    wallArea = wallHeight * wallWidth;
                    System.out.println("Wall area: " + wallArea + " square feet");
                } catch (Exception excpt) {
                    System.out.println(excpt.getMessage());
                    System.out.println("Cannot compute wall area");
                }
        
                // Calculate and output the amount of paint (in gallons) needed to paint the wall
                gallonsPaintNeeded = wallArea / squareFeetPerGallons;
                System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons");
            }
        
        
            private static double getNumber(Scanner scnr, String message) throws Exception {
                double number = 0.0;
                try {
                    boolean isValidNumber = false;
                    while (!isValidNumber) {
                        System.out.println(message);
                        String value = scnr.next();
                        number = Double.parseDouble(value);
                        isValidNumber = true;
                    }
                } catch (Exception e) {
                    System.out.println("Value entered is not correct.");
                    return -1;
                }
                return number;
            }
        

        【讨论】:

          最近更新 更多