gpt4 book ai didi

java - 不允许使用 try catch block 进行基本输入计算

转载 作者:行者123 更新时间:2023-12-01 11:24:03 29 4
gpt4 key购买 nike

如果输入ENTER,即null或空字符串,则会抛出以下错误。似乎无法解决这个问题。

Exception in thread "main" java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at com.company.Main.main(Main.java:67) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

说明

Design a program that will prompt for and read in two numbers as strings. The numbers could be decimal numbers (double or float) or whole numbers (int). Convert these numbers from String to numeric. You can either choose the correct numeric type for the number of convert directly to double since it is the largest enclosing type. If the numbers are not valid, please inform the user and prompt again. After both valid numbers are read in, read in an operator (+, -, *, /). If the operator read in is not one of the allowed operators, inform the user and prompt again. Once everything is valid perform the indicated operation using the two numbers.

Do not use a try...catch block to intercept errors. All other options are open to you.

package com.company;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

String sInput;
String nInput;
double dInput;

String sInput2;
String nInput2;
double dInput2;

boolean inputOK = false;
String operator = "";

int decCount;
String operatorSelect;
double total = 0;

Scanner scanner = new Scanner(System.in);

while (true) {
do {
System.out.print("Enter first number: ");
//decimals count validation reset.
decCount = 0;
sInput = scanner.nextLine();
if (sInput == null) {
sInput = "0";
}
//purge !numerals || !decimals.
nInput = sInput.replaceAll("[^0-9.]", "");
//decimal count validation.
for (int i = 0; i < nInput.length(); i++) {
if (nInput.charAt(i) == '.') {
decCount = decCount + 1;
}
}
//break if input valid.
if (decCount <= 1) {
break;
}
} while (true);
dInput = Double.parseDouble(nInput);

do {
System.out.print("Enter second number: ");
//decimals count validation reset.
decCount = 0;
sInput2 = scanner.nextLine();
//purge !numerals || !decimals.
nInput2 = sInput2.replaceAll("[^0-9.]", "");
//decimal count validation.
for (int i = 0; i < nInput2.length(); i++) {
if (nInput2.charAt(i) == '.') {
decCount = decCount + 1;
}
}
//break if input valid.
if (decCount <= 1) {
break;
}
} while (true);
dInput2 = Double.parseDouble(nInput2);
while (inputOK == false) {
System.out.println("Operator to select: \n1) +\n2) -\n3) *\n4) /\n");
operatorSelect = scanner.nextLine();
switch (operatorSelect) {
case "1":
total = dInput + dInput2;
inputOK = true;
operator = "+";
break;
case "2":
total = dInput - dInput2;
inputOK = true;
operator = "-";
break;
case "3":
total = dInput * dInput2;
inputOK = true;
operator = "*";
break;
case "4":
total = dInput / dInput2;
inputOK = true;
operator = "/";
break;
default:
System.out.println("Invalid input.");
}
}
System.out.println(dInput + " " + operator + " " + dInput2 + " = " + total);
}
}
}

最佳答案

如果按 Enter 键,sInput 不为 null,而是一个空字符串。

sInput = scanner.nextLine();
if (sInput.equals("")) {
sInput = "0";
}

关于java - 不允许使用 try catch block 进行基本输入计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30966738/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com