如何在java中退出返回循环



我对编程本身并不陌生,我已经学习C#很长一段时间了,但我还没有真正自己做过很多练习,我现在刚开始学习java,因为我想做Android应用程序,并测试我新获得的知识,我想做一个基本的控制台计算器,以下是我到目前为止得到的:

package calculatorSource;
import java.util.*;
public class calculatorJava {
private static Scanner input;
public static void main(String[] args) {
System.out.println("Select an operation");
String choice = null;
input = new Scanner(System.in);
while(choice == null) {
System.out.println("Type 'a' for adition, 's' for subtraction, 'm' for multiplication," + " 'd' for division, or 'mo' for module.");
choice = input.next();
}
while(choice != null) {
if(choice.equals("a")) {
System.out.println(adition(0, 0));
choice = null;
} else if(choice.equals("s")) {
System.out.println(subtraction(0, 0));
choice = null;
} else if(choice.equals("m")) {
System.out.println(multiplication(0, 0));
choice = null;
} else if(choice.equals("d")) {
System.out.println(division(0, 0));
choice = null;
} else if(choice.equals("mo")) {
System.out.println(module(0, 0));
choice = null;
} else {
System.out.println("Option not available, please try again.");
choice = null;
}
}
}
public static float adition(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 + n2;
System.out.println("Result is: " + result);
return adition(result, result);
}
public static float subtraction(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 - n2;
System.out.println("Result is: " + result);
return subtraction(result, result);
}
public static float multiplication(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 * n2;
System.out.println("Result is: " + result);
return multiplication(result, result);
}
public static float division(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 / n2;
System.out.println("Result is: " + result);
return division(result, result);
}
public static float module(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 % n2;
System.out.println("Result is: " + result);
return module(result, result);
}
}

我知道它可能不是最好或更高效的计算器,但正如我所说,我刚开始使用java,这几乎是我迄今为止所知道的全部,该程序是有效的,因为我可以进行加法、除法或我选择的任何其他运算,但在那之后,我希望它能让我选择不同的运算,我把"choice=null"放在return后面,但它似乎不起作用,我已经尝试了几种方法,但我开始认为我可能误解了return的实际作用,所以我想最好向你们寻求帮助。

任何建议都将不胜感激,谢谢!

而不是

return adition(result, result);

adition方法中,返回result

否则,您只需要重复相同的方法,直到堆栈溢出。(其他方法也一样)。


您的while (choice != null) {循环不是必需的,因为您总是设置choice = null;。由于您也不会以其他方式更改该循环中choice的值,因此您还可以删除该循环。


将参数传递到adition(etc)方法中没有意义,因为您总是覆盖它们。只需在这些方法中将它们声明为局部变量:

public static float adition() {  // No parameters
// ...
float n1 = input.nextFloat();
// ... 
float n2 = input.nextFloat();

您正在无限递归地调用所有方法。choice=null可以。如果您更正代码,它将终止:
而不是

MethodaName(result, result);

只写

return result; 

此外,若在方法本身中接受用户的输入,则不需要传递任何参数。

您似乎误解了函数中的return语句。每当我们在java中调用函数/方法时,它都应该有一个返回类型,并且返回的值应该是函数在处理参数后实际返回的值。

因此,在您的情况下,添加功能应该像一样

public static float adition(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 + n2;
System.out.println("Result is: " + result);
return result;
}

这里的结果变量是为其创建的函数的处理值。

在你的情况下,你在return语句中调用了同一个函数,它变成了一个递归函数,而在递归函数中,我们总是需要一个break语句

你可以做两件事。

  1. 如果您只想运行一次操作,则不要运行

    return addition(result,result)
    

    使用

    return result.
    

2。假设你想运行加法循环,直到用户说,他想做一些其他操作,然后使用一个全局变量,假设你在用户输入-999时添加条件,然后返回。因此,在第二种情况下,adition循环一直运行到用户输入-999。

import java.util.*;
public class calculatorJava {
private static Scanner input;
static int flag = 1;
public static void main(String[] args) {
System.out.println("Select an operation");
String choice = null;
input = new Scanner(System.in);
while (choice == null) {
System.out.println(
"Type 'a' for adition, 's' for subtraction, 'm' for multiplication,"
+ " 'd' for division, or 'mo' for module.");
choice = input.next();
}
while (choice != null) {
if (choice.equals("a")) {
flag = 1;
System.out.println(adition(0, 0));
//choice = null;
} else if (choice.equals("s")) {
flag = 1;
System.out.println(subtraction(0, 0));
//choice = null;
} else if (choice.equals("m")) {
flag = 1;
System.out.println(multiplication(0, 0));
//choice = null;
} else if (choice.equals("d")) {
flag = 1;
System.out.println(division(0, 0));
//choice = null;
} else if (choice.equals("mo")) {
flag = 1;
System.out.println(module(0, 0));
//choice = null;
} else {
System.out.println("Option not available, please try again.");
choice = null;
}
}
}
public static float adition(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
if(n1 == -999){
flag = 0;
}
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 + n2;
System.out.println("Result is: " + result);
if(flag==0)
return result;
return adition(result, result);
}
public static float subtraction(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
if(n1 == -999){
flag = 0;
}
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 - n2;
System.out.println("Result is: " + result);
if(flag==0)
return result;
return subtraction(result, result);
}
public static float multiplication(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
if(n1 == -999){
flag = 0;
}
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 * n2;
System.out.println("Result is: " + result);
if(flag==0)
return result;
return multiplication(result, result);
}
public static float division(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
if(n1 == -999){
flag = 0;
}
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 / n2;
System.out.println("Result is: " + result);
if(flag==0)
return result;
return division(result, result);
}
public static float module(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
if(n1 == -999){
flag = 0;
}
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 % n2;
System.out.println("Result is: " + result);
if(flag==0)
return result;
return module(result, result);
}
}   

再次询问

首先,加法的while循环没有用,IF可能足够了,但没有用,因为你在之前(在第一个循环中)检查了值

这是一种在代码上循环的可能性。

boolean letDoMath = true;
while(letDoMath ) { // aka while(letDoMath == true){
while(choice == null) {
System.out.println("Type 'a' for adition, 's' for subtraction, 'm' for multiplication," + " 'd' for division, or 'mo' for module.");
choice = input.next();
}
//HERE YOU KNOW CHOISE ISN'T NULL BECAUSE OF THE WHILE LOOP, NO NEED TO CHECK AGAIN.
if(choice.equals("a")) {
System.out.println(adition(0, 0));
choice = null;
} else if(choice.equals("s")) {
System.out.println(subtraction(0, 0));
choice = null;
} else if(choice.equals("m")) {
System.out.println(multiplication(0, 0));
choice = null;
} else if(choice.equals("d")) {
System.out.println(division(0, 0));
choice = null;
} else if(choice.equals("mo")) {
System.out.println(module(0, 0));
choice = null;
} else if(choice.equlas("end"){
letDoMath = false;
}
}

布尔while使您可以再次循环,直到输入end。当然,这并不完美,但这只是一个开始。(我没有尝试,甚至没有在IDE中编写它,所以可能包含错误。

操作方法错误

由于处处精确,您的操作方法都是递归的,加法在最后调用自己。删除返回的加法(x,y),只返回您所做加法的结果。

一些改进想法:

  • 创建一个方法来获取一个数字并返回它(在每个方法中都使用full)
  • 最好是在操作之外询问数字,将值传递给方法,而不是读取方法中的扫描仪。(当然,你需要确定选择值(实际上,你可以在选择之前询问数字)。只有在操作完成后才再次询问

最新更新