Java 如何使用if-else语句调用方法
正如标题所说:我有一个方法需要调用,但我不确定如何调用。方法如下:Java 如何使用if-else语句调用方法,java,methods,Java,Methods,正如标题所说:我有一个方法需要调用,但我不确定如何调用。方法如下: public static int wordOrder(int order, String result1, String result2){ order = result1.compareToIgnoreCase(result2); if (order == 0){ System.out.println("The words are the same."); }else if (orde
public static int wordOrder(int order, String result1, String result2){
order = result1.compareToIgnoreCase(result2);
if (order == 0){
System.out.println("The words are the same.");
}else if (order > 0){
System.out.println("The order of the words alphabetically is " +result2+ " then " +result1+ ".");
}else{
System.out.println("The order of the words alphabetically is " +result1+ " then " +result2+ ".");
}
return order;
}
在main方法中,我该怎么称呼它呢?任何帮助都会很好!谢谢 像这样:
wordOrder(1,“a”,“b”)
(第一个参数毫无意义)。如下:wordOrder(1,“a”,“b”)代码>(尽管第一个参数没有意义)。它应该是这样的
public static void main(String [] args){
int myOrder = wordOrder(1, "One word", "Second word");
}
public static int wordOrder(int order, String result1, String result2){
order = result1.compareToIgnoreCase(result2);
if (order == 0){
System.out.println("The words are the same.");
}else if (order > 0){
System.out.println("The order of the words alphabetically is " +result2+ " then " +result1+ ".");
}else{
System.out.println("The order of the words alphabetically is " +result1+ " then " +result2+ ".");
}
return order;
}
主要方法
public static void main(String[] args) {
int resultFromMethod= wordOrder(2,"result1","result2");
// your method accept argument as int, String , String and
// it is returning int value
}
这是你的方法
public static int wordOrder(int order, String result1, String result2){
order = result1.compareToIgnoreCase(result2);
if (order == 0){
System.out.println("The words are the same.");
}else if (order > 0){
System.out.println("The order of the words alphabetically is " +result2+ " then " +result1+ ".");
}else{
System.out.println("The order of the words alphabetically is " +result1+ " then " +result2+ ".");
}
return order;
}
此live可能会对您有所帮助。它应该是这样的
public static void main(String [] args){
int myOrder = wordOrder(1, "One word", "Second word");
}
public static int wordOrder(int order, String result1, String result2){
order = result1.compareToIgnoreCase(result2);
if (order == 0){
System.out.println("The words are the same.");
}else if (order > 0){
System.out.println("The order of the words alphabetically is " +result2+ " then " +result1+ ".");
}else{
System.out.println("The order of the words alphabetically is " +result1+ " then " +result2+ ".");
}
return order;
}
主要方法
public static void main(String[] args) {
int resultFromMethod= wordOrder(2,"result1","result2");
// your method accept argument as int, String , String and
// it is returning int value
}
这是你的方法
public static int wordOrder(int order, String result1, String result2){
order = result1.compareToIgnoreCase(result2);
if (order == 0){
System.out.println("The words are the same.");
}else if (order > 0){
System.out.println("The order of the words alphabetically is " +result2+ " then " +result1+ ".");
}else{
System.out.println("The order of the words alphabetically is " +result1+ " then " +result2+ ".");
}
return order;
}
此live可能会对您有所帮助。qqilihq给了您正确的答案,但您可以从函数中删除未使用的变量并生成函数:
public static int wordOrder(String result1, String result2){
int order = result1.compareToIgnoreCase(result2);
if (order == 0){
System.out.println("The words are the same.");
}else if (order > 0){
System.out.println("The order of the words alphabetically is " +result2+ " then " +result1+ ".");
}else{
System.out.println("The order of the words alphabetically is " +result1+ " then " +result2+ ".");
}
return order;
}
wordOrder("a","b");
并调用此函数:
public static int wordOrder(String result1, String result2){
int order = result1.compareToIgnoreCase(result2);
if (order == 0){
System.out.println("The words are the same.");
}else if (order > 0){
System.out.println("The order of the words alphabetically is " +result2+ " then " +result1+ ".");
}else{
System.out.println("The order of the words alphabetically is " +result1+ " then " +result2+ ".");
}
return order;
}
wordOrder("a","b");
qqilihq给出了正确的答案,但您可以从函数中删除未使用的变量并生成函数:
public static int wordOrder(String result1, String result2){
int order = result1.compareToIgnoreCase(result2);
if (order == 0){
System.out.println("The words are the same.");
}else if (order > 0){
System.out.println("The order of the words alphabetically is " +result2+ " then " +result1+ ".");
}else{
System.out.println("The order of the words alphabetically is " +result1+ " then " +result2+ ".");
}
return order;
}
wordOrder("a","b");
并调用此函数:
public static int wordOrder(String result1, String result2){
int order = result1.compareToIgnoreCase(result2);
if (order == 0){
System.out.println("The words are the same.");
}else if (order > 0){
System.out.println("The order of the words alphabetically is " +result2+ " then " +result1+ ".");
}else{
System.out.println("The order of the words alphabetically is " +result1+ " then " +result2+ ".");
}
return order;
}
wordOrder("a","b");
如果我理解,您只想从主方法调用一个方法。代码可能是这样的
public static void main(String [] args){
int myOrder = wordOrder(1, "One word", "Second word");
}
public static int wordOrder(int order, String result1, String result2){
order = result1.compareToIgnoreCase(result2);
if (order == 0){
System.out.println("The words are the same.");
}else if (order > 0){
System.out.println("The order of the words alphabetically is " +result2+ " then " +result1+ ".");
}else{
System.out.println("The order of the words alphabetically is " +result1+ " then " +result2+ ".");
}
return order;
}
另请注意:只有当方法wordOrder
设置为静态时,才能执行此操作,否则将显示无法从静态上下文引用非静态方法。如果我理解,您只想从主方法调用方法。代码可能是这样的
public static void main(String [] args){
int myOrder = wordOrder(1, "One word", "Second word");
}
public static int wordOrder(int order, String result1, String result2){
order = result1.compareToIgnoreCase(result2);
if (order == 0){
System.out.println("The words are the same.");
}else if (order > 0){
System.out.println("The order of the words alphabetically is " +result2+ " then " +result1+ ".");
}else{
System.out.println("The order of the words alphabetically is " +result1+ " then " +result2+ ".");
}
return order;
}
另请注意:仅当方法wordOrder
设置为静态时,才能执行此操作,如果不是,则显示无法从静态上下文引用非静态方法。如何设置参数?只需像给定示例中那样编写参数。@Salma与调用所有其他方法相同System.out.println(String s)
是一个方法,你知道。我如何设置参数?只需像给定示例中那样编写参数。@Salma与调用所有其他方法一样System.out.println(String s)
是一种方法,你知道。你没有理由将int-order
作为方法的参数。请参阅Oracle的Java教程。你没有理由将int-order
作为方法的参数。请参阅Oracle的Java教程。@AndrewMartin没有,他没有,他从方法签名中删除了int-order
。另外,我从函数中删除了不必要的参数;)很公平-抱歉@AndrewMartin不,他没有,他从方法签名中删除了int-order
。另外,我从函数中删除了不必要的参数;)很公平-抱歉!