如何在Java函数中进行切换
编辑:已经完成了,谢谢各位 大家好,我只是个新手。我需要把这个带开关的计算器放在一个函数中。我需要把它转换成void函数(如果它是这样叫的话)。它已经运行了,但问题是它运行了两次。我只是添加了一些代码来缩短代码,但我也需要在其他操作中使用它。如果有人知道,请张贴。这是我的密码:如何在Java函数中进行切换,java,function,switch-statement,Java,Function,Switch Statement,编辑:已经完成了,谢谢各位 大家好,我只是个新手。我需要把这个带开关的计算器放在一个函数中。我需要把它转换成void函数(如果它是这样叫的话)。它已经运行了,但问题是它运行了两次。我只是添加了一些代码来缩短代码,但我也需要在其他操作中使用它。如果有人知道,请张贴。这是我的密码: import java.io.*; public class Cal { static char Switch() throws IOException { BufferedReader br = new
import java.io.*;
public class Cal {
static char Switch() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n\n");
System.out.println("1]Addition");
System.out.println("2]Subtraction");
System.out.println("3]Multiplication");
System.out.println("4]Division");
System.out.print("\nEnter Your Choice : ");
char r = br.readLine().charAt(0);
switch (r) {
case '1':
Add1();
Add2();
break;
default:
System.out.print("Error");
break;
}
return r;
}
static int Add1() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nAddition");
System.out.print("enter 1st # : ");
int x = Integer.parseInt(br.readLine());
return x;
}
static int Add2() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter 2nd # : ");
int y = Integer.parseInt(br.readLine());
return y;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int y;
int x;
char r;
r = Switch();
x = Add1();
y = Add2();
System.out.print("\nSum " + (x + y));
}
}
那么这个方法呢,
static void Switch()throws IOException{
然后你改变所有的返回r代码>只返回return代码>
在你的main
中,你只需要
Switch();
System.out.print("\nSum " + (x+y));
但是x和y也需要移动成为类变量代码运行两次的原因是调用Add1()和Add2()
从开关()
方法和
再次从main()
方法
开关()
:
main()
:
你的逻辑是错误的。加法应该是一种方法,而不是两种方法。
另一个问题是,您确实返回了值,但没有将返回的值存储在任何位置
更好的办法是:
switch (r) {
case '1':
addition();
break;
default: //
break;
}
// ...
private int resultOfAddition;
public void addition(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nAddition");
System.out.print("enter 1st # : ");
int x = Integer.parseInt(br.readLine());
System.out.print("enter 2nd # : ");
int y = Integer.parseInt(br.readLine());
this.resultOfAddition = (x+y);
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//int y;
// int x;
char r = 'y';
while (r!='x'){Switch();} // assuming you want the user to quit once entered x
// x = Add1();
// y = Add2();
System.out.print("\nSum " + (resultOfAddition));
}
static void…//返回r代码>到底是什么问题?已经做了,你知道如何在函数语句中放置do吗?它将很有用,已经做了,你知道如何在函数语句中放置do吗?它将很有用,它工作了,不是我唯一的问题是在函数语句中放置do。你知道如何把它放在哪里吗?我应该把它放在哪里,这样我就可以在不关闭代码的情况下重新运行代码。您正在从switch语句和main方法调用加法。你想要的,是我在编辑我的回复时添加的内容。在main方法中,我注释掉了不应该出现的内容。看看这一点,看看您是否理解为什么不需要这些行;supahraleiigh:检查主方法,有一个循环迭代。如果你想添加它,只需将while(r!='x')更改为while(r!='n'&&r!='n')或类似的内容。但是它会自动退出或恢复。如果你想继续或不想继续使用该消息>,我需要该选项。
r = Switch();
x = Add1();
y = Add2();
switch (r) {
case '1':
addition();
break;
default: //
break;
}
// ...
private int resultOfAddition;
public void addition(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nAddition");
System.out.print("enter 1st # : ");
int x = Integer.parseInt(br.readLine());
System.out.print("enter 2nd # : ");
int y = Integer.parseInt(br.readLine());
this.resultOfAddition = (x+y);
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//int y;
// int x;
char r = 'y';
while (r!='x'){Switch();} // assuming you want the user to quit once entered x
// x = Add1();
// y = Add2();
System.out.print("\nSum " + (resultOfAddition));
}