简体   繁体   English

如何在Java中调用我的void方法?

[英]how do I invoke my void method in Java?

this program works.. i tested it without using switch.. my only issue is not knowing how to invoke the whichtable(int number) method. 这个程序工作..我没有使用开关进行测试..我唯一的问题是不知道如何调用whittable(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;
}

}

this is the void switch method i want to call from my main. 这是我想从主调用的void切换方法。 I am not sure how too 我也不确定

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;
 }  

This is my main and I want to call the whichtable(int number) method into it.. 这是我的主要方法,我想调用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");       
  }}

If you know that they typed in an integer on the input string you could say MathTables.whichtable(Integer.parseInt(inputStr)) in main and it would print out one of the tables. 如果您知道它们在输入字符串中键入了整数,则可以在main中说MathTables.whichtable(Integer.parseInt(inputStr)) ,它会打印出其中一张表。

If main is in the same class as MathTables then you would only need to use whichtable(Integer.parseInt(inputStr)) , but it's good programming practice to make a runnable class along with the class you're working with for testing and debugging. 如果main与MathTables在同一个类中,则只需要使用whichtable(Integer.parseInt(inputStr)) ,但是将可运行类与正在使用的类一起进行测试和调试是一种很好的编程习惯。

Since you need to convert a String to a number from your dialog, you need to parse it. 由于需要从对话框将字符串转换为数字,因此需要对其进行解析。

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

In your inputStr you have the input of the user. 在inputStr中,您具有用户的输入。 You have to parse that to integer and then call your method. 您必须将其解析为整数,然后调用您的方法。 You can use 您可以使用

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

Use : 采用 :

whichtable(Integer.parseInt(inputStr));

the whichtable method expects an Integer , so you must convert it. whichtable方法需要一个Integer ,因此您必须对其进行转换。

you get a string with using JOptionPane.showInputDialog so when i type 3 inputStr will be "3" 您将使用JOptionPane.showInputDialog获得一个字符串,因此当我键入3时,inputStr将为“ 3”

you can parse it to integer and call your method 您可以将其解析为整数并调用您的方法

whichtable(String.valueOf(inputStr));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM