开发者

How come my program doesn't return the value?

import java.util.Scanner;
public class SentenceVowels
{
    public static void main(String[] args)
    {
        int x,z, count, vowels;
        String sentence;
        char v;

        z = 0;
        vowels = 0;

        Scanner keyboard = new Scanner(System.in);
        System.out.print("Please enter a sentence, and the program" +
                        " will return the # of vowels: ");
        System.out.println();
        sentence = keyboard.nextLine();

        x = sentence.length();

        for(count=1; count <= x; count++)
            {
                char letter = sentence.charAt(z);
    开发者_如何学Python            isVowel(letter);
                if(v == 1)
                    vowels = vowels + 1;
                z++;
            }
        System.out.println("The amount of vowels in the sentence you inputed " +
                  " was: " + vowels);

    }

    public static char isVowel(char l)
    {
        int v, y;

        if ((l == 'a')||(l ==  'e')||(l ==  'i')||(l ==  'o')||
                (l == 'u'))
            {
                v = 1;
                return v;
            }

    }
}


Probably you are missing a:

v = isVowel(letter);

instead of:

isVowel(letter);

By the way, the code could be much more simpler. As it stand it seems too much convoluted.
Amongst the bizarre things there is that v is declared as int inside the isVowel method but then it is returned as char. I don't see why you are not returning a simple boolean from isVowel.
Another waste is that you could use char letter = sentence.charAt(count-1); instead of using z that you don't really need.
Then y is not used inside isVowel...
Well essentially the code is not pretty at all. You should really considering refacoring it quite a bit.


Because the "v" in isVowel isn't being passed back to the main program. The minimum fix is v = isVowel(letter);

Looks like you are a beginning programmer. We all were once. But there are some other issues about improving your program. For instance count and z are serving the same purpose (except that count start from 1). The usual idiom is for (z=0; z<sentence.length(); ++z);.

And: your isVowel should return a boolean, not a char. (That's true of all functions with a name like isSomething.)


Since this is clearly homework (or an informal "learning exercise"), and you need to learn to fix compilation errors and debug your code here are a couple of hints:

  1. Fix compilation errors before you try to run your program. Fixing compilation errors involves reading and attempting to understand the error messages!!

  2. Use a debugger to single step the execution of your program, and watch what actually happening.

  3. Look for variables that are used before it is initialized, or that are never initialized.


Here's a much less "expensive" version of your code. One of the mistakes that new Java programmers make is excessively declaring variables, overcomplicating what should be a simple program. In practice, this is an inefficient use of memory. I'll put some pointers on what I changed below the code.

import java.util.Scanner;

public class SentenceVowels
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter a sentence, and the program" +
                        " will return the # of vowels: ");
        // System.out.println(); You don't need this - you could just make the last 
        //line a println and avoid the unnecessary line 
        String sentence = keyboard.nextLine();

        int vowels = 0;
        for (int i = 0 ; i < sentence.length() ; i++) {
            vowels += isVowel(sentence.charAt(i));
        }
        System.out.println("The amount of vowels in the sentence you inputed " +
                  " was: " + vowels);

    }

    public static int isVowel(char l) {
        if (l=='a' || l=='e' || l=='i' || l=='o' || l=='u') {
            return 1;
        }
        return 0;
    }
}

Here's what I changed.

  1. Your code declares several "iterators", which are basically integers that point to the part of the sentence you are currently at. You really only need one iterator if you have one String object - declaring all those variables is redundant.
  2. Java is very flexible with how you pass value around and work with them. Notice how I don't create a new variable every time I want to pass a parameter to another function, or if I want to return something. This is really the elegance of Java and the beauty of abstraction (google "Object Oriented Programming" if you are unclear on what abstraction means)
  3. Try being creative with how you define functions. If you know you're just counting vowels, you know your isVowel function will, eventually, be computed into an integer that says if its a vowel or not - yes, then increment "vowels" by 1, else don't. In the isVowel function, if any of the conditions evaluate to true, a 1 is returned, otherwise a 0 is returned. Remember, the function stops once it returns something, so if a 1 is returned, the function is over, and it won't read the "return 0" line. However, if the conditional is false, we know for sure it's not a vowel, so we just return 0.
  4. You don't need to add a System.out.println() if you want a new line. Well-formatted Java code should attempt to avoid multiple System.out.print calls in a row - they can be condensed into a single call.

Hopefully this helps. All the best to you on your Java journey!!


This will do it:

import java.util.Scanner;

public class SentenceVowels {
    public static void main(String[] args) {
        int vowels = 0;
        String sentence;

        Scanner keyboard = new Scanner(System.in);
        System.out.print("Please enter a sentence, and the program"
                + " will return the # of vowels: ");
        System.out.println();
        sentence = keyboard.nextLine().toString();

        for (char letter : sentence.toCharArray()) {
            vowels += isVowel(letter);
        }
        System.out.println("The amount of vowels in the sentence you inputed "
                + " was: " + vowels);

        System.exit(0);
    }

    public static int isVowel(char l) {
        if ((l == 'a') || (l == 'e') || (l == 'i') || (l == 'o') || (l == 'u')) {
            return 1;
        } else {
            return 0;
        }
    }
}

The issues you had were:

  1. You must initialise your variable 'v'
  2. Your method isVowel must be able to return a value. If your 'if' statement argument fails then the method cannot return anything. Therefore you have and else statement to return a negative result.
  3. You have declared your isVowel method return type to be 'char' however in your return statement you are returning an 'int' (int v;). Your method must return a value that is of the same type that you declare in your method header.
  4. In your line

    vowels = vowels + 1;

vowels is set to 0 and never changes. You need to change it so vowels is summing the return value from your method 'isVowel' so

vowels += isVowel(letter);
  1. Your line

    char letter = sentence.charAt(z);

will only ever refer to the '0'th character of your string because you declare it as 0 and it never changes. Try using the 'count' from your for loop as you have set it to increment. Also you should initialise the count to '0' as array indexes always begin at '0'. So

x = sentence.length();
for(count=0; count < x; count++) {
    char letter = sentence.charAt(count);
       .
       .
       .

Hope this helps to understand the problems a bit better.

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜