如何在 Java 中调用我的 void 方法?
how do I invoke my void method in Java?
这个程序有效。我在没有使用 switch 的情况下测试了它。我唯一的问题是不知道如何调用 whichtable(int number) 方法。
package switchmathtables;
import javax.swing.JOptionPane;
public class MathTables {
public static void printAddition(int x){
int i = 1;
while (x <= 12){
System.out.print (0 + x + " ");
x = x + 1;
}
System.out.println ("");
}
public static void printSinTable(){
double x, y, z;
System.out.print("Angle 30 degree ");
x = 30 * Math.PI/180;
System.out.println ("sin(" + x + ") is " + Math.asin(x));
System.out.print("Angle 60 degree ");
y = 30 * Math.PI/180;
}
public static void printCosTable(){
double x, y, z;
System.out.print("Angle 30 degree ");
x = 30 * Math.PI/180;
System.out.println ("cos(" + x + ") is " + Math.acos(x));
System.out.print("Angle 60 degree ");
y = 30 * Math.PI/180;
}
public static void printTanTable(){
double x, y, z;
System.out.print("Angle 30 degree ");
x = 30 * Math.PI/180;
System.out.println("tan(" + x + ") is " + Math.atan(x));
System.out.print("Angle 60 degree ");
y = 30 * Math.PI/180;
}
public static void printCommonLog(){
double x = 0.5;
while(x < 10) {
System.out.println( x + " " + Math.log(x) / Math.log(10));
x = x + 0.05;
}
}
这是我想从我的 main 调用的 void switch 方法。我也不知道如何
public static void printAddition(){
int x = 1;
while (x <= 12){
System.out.print (0 + x + " ");
x = x + 1;
}
System.out.println ("");
}
public static void whichtable(int number){
switch (number){
case 1:
printCommonLog();
break;
case 2:
printAddition();
break;
case 3:
printTanTable();
break;
case 4:
printCosTable();
break;
case 5:
printSinTable();
break;
}
这是我的主程序,我想在其中调用 whichtable(int number) 方法。
}
public static void main(String[] args) {
int number;
String inputStr = JOptionPane.showInputDialog ("Type 1 for CommonLog table"
+ "Type 2 for Addition table"
+ "Type 3 for the tangent table"
+ "Type the 4 for the cosine table"
+ "Type the 5 for the sine");
}}
如果您知道他们在输入字符串中输入了一个整数,您可以在 main 中输入 MathTables.whichtable(Integer.parseInt(inputStr))
,它会打印出其中一个表格。
如果 main 与 MathTables 在同一个 class 中,那么您只需要使用 whichtable(Integer.parseInt(inputStr))
,但是制作一个可运行的 class 以及 class 您正在与您一起进行测试和调试。
由于您需要将对话框中的字符串转换为数字,因此需要对其进行解析。
int option = Integer.parseInt(inputStr); // Convert the String to an "int"
whichtable(option); // Call the void method!
使用:
whichtable(Integer.parseInt(inputStr));
whichtable
方法需要一个 Integer
,因此您必须转换它。
在您的 inputStr 中,您有用户的输入。您必须将其解析为整数,然后调用您的方法。您可以使用
final Integer choice = Integer.parseInt(stringToParse);
whichtable(choice);
你使用 JOptionPane.showInputDialog 得到一个字符串
所以当我输入 3 时,inputStr 将是“3”
您可以将其解析为整数并调用您的方法
whichtable(String.valueOf(inputStr));
这个程序有效。我在没有使用 switch 的情况下测试了它。我唯一的问题是不知道如何调用 whichtable(int number) 方法。
package switchmathtables;
import javax.swing.JOptionPane;
public class MathTables {
public static void printAddition(int x){
int i = 1;
while (x <= 12){
System.out.print (0 + x + " ");
x = x + 1;
}
System.out.println ("");
}
public static void printSinTable(){
double x, y, z;
System.out.print("Angle 30 degree ");
x = 30 * Math.PI/180;
System.out.println ("sin(" + x + ") is " + Math.asin(x));
System.out.print("Angle 60 degree ");
y = 30 * Math.PI/180;
}
public static void printCosTable(){
double x, y, z;
System.out.print("Angle 30 degree ");
x = 30 * Math.PI/180;
System.out.println ("cos(" + x + ") is " + Math.acos(x));
System.out.print("Angle 60 degree ");
y = 30 * Math.PI/180;
}
public static void printTanTable(){
double x, y, z;
System.out.print("Angle 30 degree ");
x = 30 * Math.PI/180;
System.out.println("tan(" + x + ") is " + Math.atan(x));
System.out.print("Angle 60 degree ");
y = 30 * Math.PI/180;
}
public static void printCommonLog(){
double x = 0.5;
while(x < 10) {
System.out.println( x + " " + Math.log(x) / Math.log(10));
x = x + 0.05;
}
}
这是我想从我的 main 调用的 void switch 方法。我也不知道如何
public static void printAddition(){
int x = 1;
while (x <= 12){
System.out.print (0 + x + " ");
x = x + 1;
}
System.out.println ("");
}
public static void whichtable(int number){
switch (number){
case 1:
printCommonLog();
break;
case 2:
printAddition();
break;
case 3:
printTanTable();
break;
case 4:
printCosTable();
break;
case 5:
printSinTable();
break;
}
这是我的主程序,我想在其中调用 whichtable(int number) 方法。
}
public static void main(String[] args) {
int number;
String inputStr = JOptionPane.showInputDialog ("Type 1 for CommonLog table"
+ "Type 2 for Addition table"
+ "Type 3 for the tangent table"
+ "Type the 4 for the cosine table"
+ "Type the 5 for the sine");
}}
如果您知道他们在输入字符串中输入了一个整数,您可以在 main 中输入 MathTables.whichtable(Integer.parseInt(inputStr))
,它会打印出其中一个表格。
如果 main 与 MathTables 在同一个 class 中,那么您只需要使用 whichtable(Integer.parseInt(inputStr))
,但是制作一个可运行的 class 以及 class 您正在与您一起进行测试和调试。
由于您需要将对话框中的字符串转换为数字,因此需要对其进行解析。
int option = Integer.parseInt(inputStr); // Convert the String to an "int"
whichtable(option); // Call the void method!
使用:
whichtable(Integer.parseInt(inputStr));
whichtable
方法需要一个 Integer
,因此您必须转换它。
在您的 inputStr 中,您有用户的输入。您必须将其解析为整数,然后调用您的方法。您可以使用
final Integer choice = Integer.parseInt(stringToParse);
whichtable(choice);
你使用 JOptionPane.showInputDialog 得到一个字符串 所以当我输入 3 时,inputStr 将是“3”
您可以将其解析为整数并调用您的方法
whichtable(String.valueOf(inputStr));