字符串修改帮助

时间:2013-10-07 00:04:26

标签: java string

我花了一天时间研究这两个课程。我进一步说,我不得不说我遇到了问题。

基本上我必须输入一个输入的字符串,只返回大写字母,每隔一个字母,整个字符串,但所有元音都用下划线交换,字符串中的元音数量,以及字符串中所有元音的最后位置

我特别设计了我的测试人员课程,我相信他们有一个单独尝试每个命令的菜单,所以我可以测试每个命令。

这是测试人员......

//******************************************
// LetterTest.java 
// Written by Sanchez
// 2013
//*******************************************

//===========================================
// This program tests the CharAPI class.
//===========================================

import java.util.Scanner;

public class LetterTest {

public static void main(String[] args){
    //create Scanner for user input
    Scanner in = new Scanner(System.in);

    //get user input
        System.out.println("Please enter a string of letters");
        String input = in.nextLine();
    System.out.println("\nChoose an option: "
        +"\n1 - Uppercase, "
        +"\n2 - Every Second Letter, "
        +"\n3 - Replace vowels "
        +"\n4 - Number of vowels "
        +"\n5 - Positions of vowels");
    int choice = in.nextInt();

    //Call the method based on user choice

if (choice == 1)  {
        //each method returns a String
    System.out.println(LetterAPI.bigLetters(input) );
}
else if (choice ==2)  {
    System.out.println(LetterAPI.secondLetter(input) );
}
else if (choice ==3)  { 
    System.out.println(LetterAPI.vowelsGone(input) );
}
else if (choice ==4)  { 
    System.out.println(LetterAPI.vowelNumber(input) );
}
else {
    System.out.println(LetterAPI.vowelPositions(input) );
}
}
}

这似乎工作得很好,我很高兴。

我遇到的问题是在我的课堂上使用操作的对象 我已经在一些事情上使用了//所以我可以让它编译。第1,第2和第4,直线上升不返回任何东西。第三个只交换下划线的最后一个字母,即使它不是元音,第五个也很好用,除了我想在所有数字上加1,所以结果从1开始而不是0.我明白了这里有很多事情,但我已经花了一天时间,我终于提交说我急需帮助。

这是对象的代码......

//******************************************
// LetterAPI.java 
// Written by Sanchez
// 2013
//*******************************************

//===========================================
// Objects of this class manipulate an inputted string.
//===========================================

import java.util.Scanner;

//contains a set of methods for maniuplaing the string
public class LetterAPI {

//print only uppercase letters
public static String bigLetters(String input) {
    String result = "";
    for (int i = 0; i <input.length(); i++) {
    char currentLetter=input.charAt(i);
    if (Character.isUpperCase(currentLetter))
    result = result;
    }
    return result;


}
//print every second letter
public static String secondLetter(String input) {
    String result = "";
    for (int i = 0; i <input.length(); i++) {
    //result = input.charAt(input);
    }
    return result;


}
//all vowels replaced by underscores
public static String vowelsGone(String input) {
    String result ="aeiouAEIOU";
    for (int i = 0; i<result.length();i++) {
    result=input.replace(result.charAt(i), '_');
    }
    return result;


}
//the numbers of vowels
public static String vowelNumber(String input) {
    String result = "";
    for (int i = 0; i <input.length(); i++) {

        if (Character.toLowerCase(input.charAt(i)) == 'a' || Character.toLowerCase(input.charAt(i)) == 'e' || Character.toLowerCase(input.charAt(i)) == 'i' || Character.toLowerCase(input.charAt(i)) == 'o' || Character.toLowerCase(input.charAt(i)) == 'u') {
            i++;
        }

    }
    return result;

}
//the positions of all vowels
public static String vowelPositions(String input) {
    String result = "";
    for (int i = 0; i <input.length(); i++) {

        if (Character.toLowerCase(input.charAt(i)) == 'a' || Character.toLowerCase(input.charAt(i)) == 'e' || Character.toLowerCase(input.charAt(i)) == 'i' || Character.toLowerCase(input.charAt(i)) == 'o' || Character.toLowerCase(input.charAt(i)) == 'u') {
            result = result + i + " ";
        }
    }
    return result;

}
}

=== UPDATE ===

谢谢大家!感谢上帝,我取得了一些进展。我已经获得了第3和第4名工作得很好。第一个是仅给出最后一个大写但现在重复我的输入。第二个是给我第一封信。至于最后一个,我试过括号,但我好像打破了它,所以我现在把它放回去。那不是那么重要......至少它有效!如果我无法弄明白,我将不得不记下计数从0开始。但前两个是杀了我......至少它编译。这是我到目前为止的地方......

//******************************************
// LetterAPI.java 
// Written by Sanchez
// 2013
//*******************************************

//===========================================
// Objects of this class manipulate an inputted string.
//===========================================

import java.util.Scanner;

//contains a set of methods for maniuplaing the string
public class LetterAPI {

//print only uppercase letters
public static String bigLetters(String input) {
    String result = "";
    char cl;
    for (int i = 0; i <input.length(); i++) {
    cl=input.charAt(i);
    if (Character.isUpperCase(cl))
    input = input + cl;
    }
    return input;


}
//print every second letter
public static String secondLetter(String input) {
    String result = "";
    for (int i=0; i<input.length(); i+=2) {
    input = input + input.charAt(i) + " ";
    }
    return input;


}
 //all vowels replaced by underscores
public static String vowelsGone(String input) {
    String vowels ="aeiouAEIOU";
    for (int i = 0; i<vowels.length();i++) {
    input=input.replace(vowels.charAt(i), '_');
    }
    return input;


}
//the numbers of vowels
public static String vowelNumber(String input) {
    String result = "";
    int count = 0;
    for (int i = 0; i <input.length(); i++) {

        if (Character.toLowerCase(input.charAt(i)) == 'a' || Character.toLowerCase(input.charAt(i)) == 'e' || Character.toLowerCase(    input.charAt(i)) == 'i' || Character.toLowerCase(input.charAt(i)) == 'o' || Character.toLowerCase(input.charAt(i)) == 'u') {
            count++;
            result = count + " ";
        }

    }
    return result;

}
//the positions of all vowels
public static String vowelPositions(String input) {
    String result = "";
    for (int i = 0; i <input.length(); i++) {

        if (Character.toLowerCase(input.charAt(i)) == 'a' || Character.toLowerCase(input.charAt(i)) == 'e' || Character.toLowerCase(input.charAt(i)) == 'i' || Character.toLowerCase(input.charAt(i)) == 'o' || Character.toLowerCase(input.charAt(i)) == 'u') {
            result = result + i + " ";
        }
    }
    return result;

}
}

1 个答案:

答案 0 :(得分:0)

提示:

  1. 在第1部分中,您执行此操作:result = result;这是荒谬的。它什么都不做。

  2. 在第2部分中,编译错误是因为您尝试char 分配给String。那不合法。但它也不是你应该做的。 (想想“附加”,而不是“分配”...)

  3. 在第3部分中,第一个错误是:String result = "aeiouAEIOU";。实际上,它应该是String result = input;

  4. 在第4部分中,您要计算所有元音,而不是每个不同的元音。您还需要将计数(或计数)转换为String(结果)。

  5. 关于第5部分你说:“工作得很好,除了我想在所有数字中添加1”。这样做吧!

相关问题