首页 > 解决方案 > 试图阻止用户在 java 中将字符串输入我的双精度

问题描述

我尝试了一个带有 if 的 for 循环,但我不知道我应该在 if(?) 中放入什么,所以我使用了 try 和 catch 并导入了 InputMismatchException。然后我尝试制作一个数字从 0 到 9 的数组(我是菜鸟,不知道我在做什么)并使用 for 循环,如果,我知道我可以使用一段时间。请帮助我最近输入字符串而不是数字。

import java.util.Scanner;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Employee {

    // fields
    private String firstName, lastName;// creates both firstName and lastName as string and private fields
    private String sal;// creates sal a private and double field
    private int years;// creates years a private and int field
    
    // constructors
    public Employee() {// default constructor
        this("", "", 0, 0);// this sets the default values for each one of the fields

    }

    public Employee(String firstName, String lastName, double sal, int years) {// overloaded constructor
        this.firstName = firstName;// gets the values from the field firstname and then puts it in string firstname
        this.lastName = lastName;// gets the value from the field secondname and then puts it in string
                                    // secondname
        this.sal = sal;// gets the value from the field sal and then puts it in float sal
        this.years = years;// gets the value from the field years and then puts it in int years
    }

    // methods
    public String getFirstName() {// accessor for the field firstname
        return firstName;
    }

    public void setFirstName(String firstName) {// mutator for the field firstname
        this.firstName = firstName;
    }

    public String getLastName() {// accessor for the field lastname
        return lastName;
    }

    public void setLastName(String lastName) {// mutator for the field lastname
        this.lastName = lastName;
    }

    public String getSal() {// accessor for the field lastname
        return sal;
    }

    public void setSal(String sal) {// mutator for the field sal
        this.sal = sal;
    }

    public int getYears() {// accessor for the field years
        return years;
    }

    public void setYears(int years) {// mutator for the field years
        this.years = years;
    }

    public void ScannerMethod() {
        int arrInt[] = new int[10];
        arrInt[0] = 0;
        int j  = 0;
        
        boolean a = false;
        Scanner get = new Scanner(System.in);
        System.out.println("enter first name: ");
        firstName = get.next();
        System.out.println("enter second name: ");
        lastName = get.next();
        System.out.println("enter the salary: ");
        while(j<10) {
            arrInt[j] = j+1;
            j++;
        }
        
        for(int i = 0; i<1; i++){
            if(sal == arrInt[]){
                System.out.println("Pleas enter it agian: ");
                sal = get.next();
                i = 0;
            }else{
                i = 2;
            }
        }
        /*/while (!a) {
            try {
                sal = get.nextDouble();
                a = false;
            } catch (Exception e) {
                System.out.println("Invalid input please enter a numeric value: ");
                a = true;
            }

        }/*/
        System.out.println("enter the years of service: ");
        years = get.nextInt();

    }

    public String typeStats() {// this method prints out the stats for the employee
        System.out.println("Report: " + firstName.substring(0, 1).toUpperCase() + firstName.substring(1) + " "
                + lastName.substring(0, 1).toUpperCase() + lastName.substring(1) + " $" + sal + " " + years);
        return null ; 
    }

}

标签: java

解决方案


这是一种要求用户输入双精度的方法,直到他们输入有效的双精度。

package test2134235;
import java.util.Scanner;

public class ParseDouble {

    public static void main(String[] args) {

        System.out.println("This is your double: " + getDoubleFromKeyboard());
    }

    static Double getDoubleFromKeyboard() {
        Scanner scanner = new Scanner(System.in);
        for (;;) {
            System.out.println("input a Double value");
            try {
                String input = scanner.nextLine();
                Double d = Double.parseDouble(input);
                return d;
            } catch (NumberFormatException e) {
                System.out.println("Sorry, only Double values can be used.");
            }
        }
    }
}

推荐阅读