【问题标题】:how do I invoke my void method in Java?如何在 Java 中调用我的 void 方法?
【发布时间】:2015-11-15 23:03:26
【问题描述】:

这个程序有效.. 我在没有使用 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");       
  }}

【问题讨论】:

    标签: java methods parameters


    【解决方案1】:

    由于您需要将对话框中的字符串转换为数字,因此需要对其进行解析。

    int option = Integer.parseInt(inputStr); // Convert the String to an "int"
    whichtable(option); // Call the void method!
    

    【讨论】:

      【解决方案2】:

      在您的 inputStr 中,您有用户的输入。您必须将其解析为整数,然后调用您的方法。你可以使用

      final Integer choice = Integer.parseInt(stringToParse);
      whichtable(choice);
      

      【讨论】:

        【解决方案3】:

        如果您知道他们在输入字符串中输入了一个整数,您可以在 main 中输入 MathTables.whichtable(Integer.parseInt(inputStr)),它会打印出其中一张表。

        如果 main 与 MathTables 在同一个类中,那么您只需要使用 whichtable(Integer.parseInt(inputStr)),但创建一个可运行的类以及您正在使用的用于测试和调试的类是一种很好的编程习惯。

        【讨论】:

          【解决方案4】:

          使用:

          whichtable(Integer.parseInt(inputStr));
          

          whichtable 方法需要 Integer,因此您必须对其进行转换。

          【讨论】:

            【解决方案5】:

            您使用 JOptionPane.showInputDialog 得到一个字符串 所以当我输入 3 inputStr 将是“3”

            您可以将其解析为整数并调用您的方法

            whichtable(String.valueOf(inputStr));
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-05-15
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多