Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我刚开始使用方法。这是一个模板和I';我对如何做这些方法感到困惑。有人能告诉我该怎么做吗?_Java_String_Methods_Static_Main - Fatal编程技术网

Java 我刚开始使用方法。这是一个模板和I';我对如何做这些方法感到困惑。有人能告诉我该怎么做吗?

Java 我刚开始使用方法。这是一个模板和I';我对如何做这些方法感到困惑。有人能告诉我该怎么做吗?,java,string,methods,static,main,Java,String,Methods,Static,Main,我不知道如何添加所需的方法,因为这是我的第一个程序,没有在主程序中完成所有操作。这让我非常困惑。我不知道如何制作一个方法,只是模糊地知道它需要什么或如何返回。有人能帮我解决这个问题吗 编辑:: 由于提供的提示,我完成了代码。然而,我似乎无法让最后两种方法发挥作用。我觉得shift char似乎可以与字符串shift一起使用,但我不知道如何使用。我在这里编译并运行的最后一个输出是“字符串移位一个字母是‘nztizhqtz’”,当我输入害羞的吉普赛人时,它正确地将字母表中的每个字母向前移位一个字母,

我不知道如何添加所需的方法,因为这是我的第一个程序,没有在主程序中完成所有操作。这让我非常困惑。我不知道如何制作一个方法,只是模糊地知道它需要什么或如何返回。有人能帮我解决这个问题吗

编辑:: 由于提供的提示,我完成了代码。然而,我似乎无法让最后两种方法发挥作用。我觉得shift char似乎可以与字符串shift一起使用,但我不知道如何使用。我在这里编译并运行的最后一个输出是“字符串移位一个字母是‘nztizhqtz’”,当我输入害羞的吉普赛人时,它正确地将字母表中的每个字母向前移位一个字母,但将空格去掉了,为什么

import java.util.Scanner;

public class StringMethods {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Enter a sentence");
        String s = input.nextLine();

        System.out.println("\nNumber of Spaces in the string = " + countSpaces(s) + "\n");
        System.out.println("Number of E's in the string = " + countChar(s, "E") + "\n");
        System.out.println("Number of Stars in the string = " + countChar(s, "*") + "\n");
        System.out.println("Does string contain vowels? " + anyVowels(s) + "\n");
        System.out.println("String reversed = " + reverse(s) + "\n");
        System.out.println("String shifted by one letter is " + shift(s)  + "\n");
    }

    // method to return the number of spaces in a String
    public static int countSpaces(String input) {
        int spaces = 0;
        int i;
        for (i = 0;i<input.length();i++){
            if(input.charAt(i) == ' '){
                spaces++;
            }
        }
         return spaces;
    }

    // method to count the number of instances of
    // a particular character in a String
    // the method will return the total of upper and lower
    // case instances if it is a letter
    public static int countChar(String input,String findStr) {
        input = input.toLowerCase();
        findStr = findStr.toLowerCase();
        int num = 0;
        int i;
        for (i = 0;i<input.length();i++){
            if(input.charAt(i) == findStr.charAt(0)){
                num++;
            }
        }
        return num;

    }

    // method to detect whether a String contains a vowel or not
    // must include call(s) to countChar
    // returns true or false
    public static boolean anyVowels(String input) {
        int num = 0;
        num = countChar(input, "a")+countChar(input, "e")+countChar(input, "i")+countChar(input, "o")+countChar(input, "u");
        if(num >  0) {
            return true;
        }
        return false;
    }


    // method to return the reverse of a String
    public static String reverse(String input) {
        String reverse = "";
        for (int i = 0;i<input.length();i++) {
            reverse = input.charAt(i) + reverse;
        }
        return reverse;    
    }

    // method to return a character shifted one place in the alphaber
    // a becomes b, b become c etc. z becomes a
    // A becomes B, B becomes C etc. Z becomes A
    // all other characters are left unchanged
    public static char shift(char input) {

        char shiftedChar = 'a';
        shiftedChar++;
        //for (int i = 0; i < input.length();i++) {
        //    shiftedChar = input.charAt(i);
        //  
        //}



        return shiftedChar;
    }

    // method to return a String with each letter shifted one place in the alphabet
    // method must include call(s) to previous method
    public static String shift(String input) {

        String shiftedString = "";
        for (int i = 0; i < input.length();i++) {
            char a = input.charAt(i);
            if (a != ' ') {
            a++;
            shiftedString = shiftedString + a;
            }
        }







        return shiftedString;
    }

}
import java.util.Scanner;
公共类方法{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入句子”);
字符串s=input.nextLine();
System.out.println(“\n字符串中的空格数=“+countSpaces)+”\n”);
System.out.println(“字符串中的E数=“+countChar(s,“E”)+”\n”);
System.out.println(“字符串中的星号=”+countChar(“*”)+“\n”);
System.out.println(“字符串是否包含元音?”+任何元音)+“\n”);
System.out.println(“字符串反转=”+反转+“\n”);
System.out.println(“被一个字母移位的字符串是”+shift(s)+“\n”);
}
//方法返回字符串中的空格数
公共静态int countSpaces(字符串输入){
int空间=0;
int i;

因为(i=0;i我知道老师已经给你留下了方法的框架,所以我将帮助你解决第一个问题

public static ________ countSpaces(________) {


    }
// method to return the number of spaces in a String
public static ________ countSpaces(________) {

}
这里第一个带下划线的部分是您希望方法返回的内容(即,您希望它返回文本(String)还是整数(int))。查找Java的数据类型。提示:它应该返回的内容在方法上方的注释中

第二个带下划线的部分(括号中)是您希望在使用该方法时传递给该方法的部分。因此,如果该方法需要其中的数字10来进行某种计算,并返回某种文本,则示例如下:

public static String countSpaces(int 10) {
       //do whatever necessary here

    }

希望这有帮助。

让我们做第一个

public static ________ countSpaces(________) {


    }
// method to return the number of spaces in a String
public static ________ countSpaces(________) {

}
需要补充的是

// method to return the number of spaces in a String
public static int countSpaces(String input) {
   int spaces = 0;
   // Do the magic counting here
   return spaces;
}

现在,该方法以字符串作为输入并返回整数作为输出。

我还没有提供所有的方法实现。有些方法留给提问者来实现。不过,下面是一些有用的提示-

import java.util.Scanner;

public class StringMethods {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Enter a sentence");
        String s = input.nextLine();

        System.out.println("\nNumber of Spaces in the string = " + countSpaces(s) + "\n");
        System.out.println("Number of E's in the string = " + countChar(s, "E") + "\n");
        System.out.println("Number of Stars in the string = " + countChar(s, "*") + "\n");
        System.out.println("Does string contain vowels? " + anyVowels(s) + "\n");
        System.out.println("String reversed = " + reverse(s) + "\n");
        System.out.println("String shifted by one letter is " + shift(s)  + "\n");
    }

    // method to return the number of spaces in a String
    public static int countSpaces(String input) {
         return input.split(" ",-1).length-1;
    }

    // method to count the number of instances of
    // a particular character in a String
    // the method will return the total of upper and lower
    // case instances if it is a letter
    public static int countChar(String input, String findStr) {


    }

    // method to detect whether a String contains a vowel or not
    // must include call(s) to countChar
    // returns true or false
    public static boolean anyVowels(String input) {

    }


    // method to return the reverse of a String
    public static String reverse(String input) {
        StringBuffer sb = new StringBuffer(input);
        return sb.reverse().toString();
    }

    // method to return a character shifted one place in the alphaber
    // a becomes b, b become c etc. z becomes a
    // A becomes B, B becomes C etc. Z becomes A
    // all other characters are left unchanged
    public static char shift(char input) {

        char shiftedChar = '';
        // TODO - code to shift the char

        return shiftedChar;
    }

    // method to return a String with each letter shifted one place in the alphabet
    // method must include call(s) to previous method
    public static String shift(String input) {

        String shiftedString = null;

        //TODO shit the char for each char in a String

        return shiftedString;
    }

}

在提出问题之前,你需要表现出一些努力。仅仅在这里放弃你的家庭作业将导致否决票和一个封闭的问题。因为我们在这里谈论的是绝对基础,你应该寻找一个基础教程(我推荐).我明白你的意思,我不是想请人帮我做作业。我只是需要一个例子来帮助我。我不是想表现得粗鲁,对不起。@Christopherwhit我在下面给了你一些建议。我不认为你的问题是粗鲁的。例子的问题是,它们通常不会给你提供像阅读书籍那样多的信息正确的教程。你可能是在盲目地复制代码,当某些东西有点不同时,它就不再起作用了。这就像试图建造一座房子,你的选择是“成为一名工程师”或“只是看看邻居的房子,试着得到相同的最终结果”.此外,在阅读了教程之后,你不必再回来为你的下一个家庭作业问另一个问题(或者至少没有这个问题那么广泛).你完全正确。我读了我关于方法的书,我只是很难用那种方式学习。不过我一定会看看教程。这很有帮助,所以数据类型在后面,明白了。非常感谢!没问题-如果你对答案满意,请投票/接受它-谢谢。@Christopher Whitt-看看这是否正确帮助。我认为其他答案已经说明了该怎么做。完成90%的家庭作业不会帮助他学习。这只是告诉他,当家庭作业被扔到这里时,总有人会做大部分的家庭作业。我认为你在那里付出了太多。删除了实现。