java - 试图阻止用户在 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 ;
}
}
解决方案
这是一种要求用户输入双精度的方法,直到他们输入有效的双精度。
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.");
}
}
}
}
推荐阅读
- machine-learning - 当所有逻辑都用另一种语言实现时,如何在 Python (sklearn) 中使用现成的 ML 模型?
- php - PHP 自定义函数与 as 语法,函数 ($x as $y)
- html - 我没有让汉堡菜单栏工作
- sql-server - 如何在 SQL Server 中的一次选择中编写多个联合选择
- neo4j - Neo4j 获取与节点列表相关的所有节点
- c# - 鼠标外观脚本中的 X 轴错误,但 y 轴正常吗?
- multithreading - 尝试在 2 个线程中运行 asio 时在 (ntdll.dll) 访问冲突读取位置引发异常
- python-3.x - 有没有办法从具有递减范围的 Python 'for' 循环中获取递增计数器?
- java - Spring WebFlux WebClient - 如何解决 400 错误请求
- reactjs - 将对象传递给子对象,然后访问对象方法