java程序循环如何退出_如何在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 adition(result, result);

在你的adition方法中,返回结果.

否则你只是重复相同的方法,直到堆栈溢出. (同样适用于其他方法).

你的while(选择!= null){loop是没有必要的,因为你总是设置choice = null;.既然您也没有在该循环中更改选择的值,您也可以删除循环.

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

public static float adition() { // No parameters

// ...

float n1 = input.nextFloat();

// ...

float n2 = input.nextFloat();

标签:calculator,android,java,return

来源: https://codeday.me/bug/20190724/1525149.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值