验证输入。我究竟做错了什么?

时间:2016-05-30 11:47:22

标签: java

我是Java的新手,我刚刚在一周前开始。

我正在尝试制作一个程序,要求用户输入一个' a'' b'和' c'和小时数。

我设法做了所有这些,但问题在于:

  1. 当用户输入其他字母而不是a.b和c时,程序继续运行。

  2. 如果用户输入超过' 24'作为小时的值,程序执行' if'的第一部分。声明。

  3. 下面是我的代码。

    import java.util.*;
    import javax.swing.*;
    
    
    public class PetSelectionHw
    {
        public static void main(String[] args) throws Exception
        {
    
            String user_input="";
            String NumberOfHours="";
    
            int hours= 0;
    
    
            System.out.println("a) House");
            System.out.println("b) Apartment");
            System.out.println("c) Dormitory");
    
            Scanner User_Selection = new Scanner (System.in);
            user_input = User_Selection.next();
            char aChar = user_input.charAt(0);
    
            if(aChar == 'a'){
                //System.out.println("Selection is " + user_input);
            } else if(aChar == 'b'){
                //System.out.println("Selection is " + user_input);
            } else if(aChar == 'c'){
                //System.out.println("Selection is " + user_input);
            } else if(aChar == 'd' || aChar == 'z'){
                JOptionPane.showMessageDialog(null, "Wrong Input");
            }
    
    
            NumberOfHours = JOptionPane.showInputDialog(null,"Enter number of hours spent at home");
            hours = Integer.parseInt(NumberOfHours);
    
            if(hours > 24 && hours < 0){
                JOptionPane.showMessageDialog(null, "Incorrect input of hours");
            } else if (aChar == 'a' && hours >= 18){
                JOptionPane.showMessageDialog(null, "Pot bellied pig");
            } else if (aChar == 'a' && hours >= 10 && hours <= 17){
                JOptionPane.showMessageDialog(null, "Dog");
            } else if (aChar == 'a' && hours < 10){
                JOptionPane.showMessageDialog(null, "Snake");
            } else if (aChar == 'b' && hours >= 10){
                JOptionPane.showMessageDialog(null, "Cat");
            } else if (aChar =='b' && hours < 10){
                JOptionPane.showMessageDialog(null, "Hamster");
            }else if (aChar =='c' && hours >= 6){
                JOptionPane.showMessageDialog(null, "Fish");
            }else if (aChar =='c' && hours < 6){
                JOptionPane.showMessageDialog(null, "Ant Farm");
            }
    
    
    
    
    
    
        }
    
    
    
    
    }
    

1 个答案:

答案 0 :(得分:1)

您想使用else

if(aChar == 'a') {
    System.out.println("Selection is " + user_input);
} else if(aChar == 'b') {
    System.out.println("Selection is " + user_input);
} else if(aChar == 'c') {
    System.out.println("Selection is " + user_input);
} else { // anything that is not 'a', 'b' or 'c'
    OptionPane.showMessageDialog(null, "Wrong Input");
}

您甚至可以考虑将switchdefault案例一起使用,或者更简单(imo):

if(aChar == 'a' || aChar == 'b' || aChar == 'c') { System.out.println("Selection is " + user_input); }
else { OptionPane.showMessageDialog(null, "Wrong Input"); }

至于第二个问题,你应该在这里使用||(OR)而不是&&(AND):

if(hours > 24 || hours < 0)
    JOptionPane.showMessageDialog(null, "Incorrect input of hours");