下面是代碼。我仍然是一名學生,有一個問題可以完成這個作業。我需要在「static void main」中完成用戶的輸入,將該字符串傳遞給我的方法「PluralForm」,並將返回傳回到「static void main」以打印出結果。我被困在如何將字符串傳遞給該方法並將結果傳回。以下是代碼。從main輸入並將其傳遞給java中的方法
import java.util.*;
public class Plural
{
public static String PluralForm(String word)
{
String last = word.substring(word.length() - 1);
String sub_last = word.substring(word.length() - 2, word.length() - 1);
if(last.equals("s") || last.equals("x") || last.equals("z"))
return word + "es";
else if(last.equals("h") && sub_last.equals("s") || sub_last.equals("c"))
return word + "es";
while(last.equals("y"))
{
if(sub_last.equals("a")) return word + "s";
if(sub_last.equals("e")) return word + "s";
if(sub_last.equals("i")) return word + "s";
if(sub_last.equals("o")) return word + "s";
if(sub_last.equals("u")) return word + "s";
return word.substring(0, word.length() - 1) + "ies";
}
while(last.equals("o"))
{
if(sub_last.equals("a")) return word + "s";
if(sub_last.equals("e")) return word + "s";
if(sub_last.equals("i")) return word + "s";
if(sub_last.equals("o")) return word + "s";
if(sub_last.equals("u")) return word + "s";
return word + "es";
}
return word + "s";
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("This program converts an English word to its plural form.\n");
System.out.print("English word: ");
?? ?? ?? = sc.next(???); // how to input and pass the value to the method?
System.out.println("Plural form: " + ???); //how to print out the return?
}
}
你使用'while',但返回一個值嗎?在那種情況下,'while'是相當無用的? – 2014-10-06 03:20:18