gpt4 book ai didi

java - 调用Java方法

转载 作者:行者123 更新时间:2023-12-01 18:10:05 25 4
gpt4 key购买 nike

我试图调用一个java方法来确定用户输入的字母是元音还是辅音。不确定如何正确调用该方法。

import java.util.*;
public class HW8Problem1{
public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter a letter: ");

String letter = input.nextLine();

}

public static void vowel(String a){

if (a.charAt(0) == 'a' || a.charAt(0) == 'A'){
System.out.println(" is a vowel");

if (a.charAt(0) == 'e' || a.charAt(0) == 'E')
System.out.println(" is a vowel");

if (a.charAt(0) == 'i' || a.charAt(0) == 'I')
System.out.println(" is a vowel");

if (a.charAt(0) == 'o' || a.charAt(0) == 'O')
System.out.println(" is a vowel");

if (a.charAt(0) == 'u' || a.charAt(0) == 'U')
System.out.println(" is a vowel");

else
System.out.println(" is a consonant");

}
}
}

最佳答案

您可以通过指定其名称后跟括号 (()) 来调用方法,其中包含要传递给该方法的参数(如果有)。就您而言,例如:

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a letter: ");
String letter = input.nextLine();
vowel(letter); // Here!
}

关于java - 调用Java方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33701532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com