gpt4 book ai didi

java - java中出现异常后如何重复特定语句?

转载 作者:行者123 更新时间:2023-12-02 04:04:08 25 4
gpt4 key购买 nike

众所周知,当发生异常时,我们用“try and catch”捕获它,并且程序向前运行,但我希望程序从异常最初发生的位置开始运行。下面是我制作的迷你计算器的示例。当程序运行时,系统会要求用户输入整数选择号来选择程序的功能,例如加法、减法等。如果用户输入字符或字符串,则会发生异常,该异常会被捕获,程序会继续运行,用户可以重新启动该程序。但我希望当用户给出错误的输入导致异常时,程序从同一点运行,即输入您的选择号。选择功能。对于程序中的进一步输入也类似。谁能指导我吗?

package string;

import java.util.Scanner;
import java.lang.Exception;

public class Calculator2 {

double sum(double number1, double number2) {
double operation = number1 + number2;
return operation;
}

double subtract(double number1, double number2) {
double operation = number1 - number2;
return operation;
}

double multiply(double number1, double number2) {
double operation = number1 * number2;
return operation;
}

double divide(double number1, double number2) {
double operation = number1 / number2;
return operation;
}

public static void main(String[] args) {
Calculator2 f = new Calculator2();
int choice;
int z;

Scanner s1 = new Scanner(System.in);

do {
try {
System.out.println();
System.out.println("Welcome To Sebiz Caculator: Which Function Do You Want To Use");
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.println();
System.out.print("Please Enter Your Choice Number: ");
choice = s1.nextInt();

System.out.println();
System.out.print("Please Enter The First Number: ");
double numb1 = s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double numb2 = s1.nextDouble();
double result;
String operation = "";

switch (choice) {
case 1:
result = f.sum(numb1, numb2);
operation = "Sum";
break;
case 2:
result = f.subtract(numb1, numb2);
operation = "Substraction";
break;
case 3:
result = f.multiply(numb1, numb2);
operation = "Multiplication";
break;
case 4:
result = f.divide(numb1, numb2);
operation = "Divison";
break;
default:
result = 0;
System.out.println();
System.out.println("Please Enter a Valid Choice from 1 to 4");
}

System.out.println();
System.out.println("The " + operation + " Of Two Numbers is: "
+ result);
} catch (Exception e) {
System.out
.println("You Have Entered An Invalid Input. Please Enter 1 On Next Instruction To Start Again");
s1.nextLine();
}
System.out.println();
System.out.println("Do You Want To perform Another Functionality?");
System.out
.print("Press 1 to Continue and Press 2 to Terminate The Program ");
z = s1.nextInt();
} while (z == 1);

System.out.println();
System.out.println("Thank You For Using Sebiz Calculator");
}
}

最佳答案

通过分离您的业务,您可以获得很多好处。您可以从扫描仪开始:

package string;

import java.util.Scanner;
import java.lang.Exception;

public class Calculator2
{

Scanner s1 = new Scanner(System.in);

double getIntInput(String message) {
try {
System.out.print(message);
return s1.nextInt();
}catch(Exception e) {
System.out.print("There was an error in your input it is not an int, please try again");
return getIntInput(message);
}
}

double getDoubleInput(String message) {
try {
return s1.nextDouble();
}catch(Exception e) {
System.out.print("There was an error in your input it is not a double, please try again");
return getDoubleInput(message);
}
}

double sum(double number1, double number2) {
double operation = number1 + number2;
return operation;
}

double subtract(double number1, double number2) {
double operation = number1 - number2;
return operation;
}

double multiply(double number1, double number2) {
double operation = number1 * number2;
return operation;
}

double divide(double number1, double number2) {
double operation = number1 / number2;
return operation;
}


public static void main(String[] args){
Calculator2 f = new Calculator2();
int choice;
int z;

do
{
try
{
System.out.println();
System.out.println("Welcome To Sebiz Caculator: Which Function Do You Want To Use");
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.println();
choice = getIntInput("Please Enter Your Choice Number: ");

System.out.println();
double numb1 = getDoubleInput("Please Enter The First Number: ");
System.out.println();
double numb2 = getDoubleInput("Please Enter The Second Number: ");
double result;
String operation = "";

switch (choice)
{
case 1:
result = f.sum(numb1, numb2);
operation = "Sum";
break;
case 2:
result = f.subtract(numb1, numb2);
operation = "Substraction";
break;
case 3:
result = f.multiply(numb1, numb2);
operation = "Multiplication";
break;
case 4:
result = f.divide(numb1, numb2);
operation = "Divison";
break;
default:
result = 0;
System.out.println();
System.out.println("Please Enter a Valid Choice from 1 to 4");
}

System.out.println();
System.out.println("The " + operation + " Of Two Numbers is: " + result);
}
catch (Exception e)
{
System.out.println("You Have Entered An Invalid Input. Please Enter 1 On Next Instruction To Start Again");
s1.nextLine();
}
System.out.println();
System.out.println("Do You Want To perform Another Functionality?");
System.out.print("Press 1 to Continue and Press 2 to Terminate The Program ");
z = s1.nextInt();
}
while (z == 1);

System.out.println();
System.out.println("Thank You For Using Sebiz Calculator");
}
}

关于java - java中出现异常后如何重复特定语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46007138/

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