如何在Java中打印重载方法的返回值

时间:2015-11-01 22:14:23

标签: java methods ascii

我正在尝试按照以下说明对不同字符串的Ascii值求和:

  

创建两个重载方法。一个将给出总和的   字符串中每个字符的ascii值和另一个字符串   产生两个字符串的ascii值的总和。

使用我已经写过的方法,我怎样才能打印出我的主要内容?谢谢!

public class Exam3Question2 
{
    public static int sumAscii(String Input)
    {

        String s = Input; 
        int sum = 0;
        for (int i = 0; i < s.length(); i++)
        {
            sum+= (int)s.charAt(i);
        }
        return sum;
    }

    public  int sumAscii( String Input1, String Input2)
    {
        int sum = sumAscii(Input1) + sumAscii(Input2);
        return sum;
    }
    public static void main(String[] args) 
    {
        Exam3Question2 c = new Exam3Question2();
        Scanner in = new Scanner(System.in);
        String word;
        System.out.println("Enter some text");
        word = in.nextLine();
        sumAscii(word);
        int sum1 = c.sumAscii(Input1);
        int sum2 = c.sumAscii(Input1, Input2);
        int sum3 = sum1 + sum2;
        System.out.println("The sum of the two strings is: " + sum3);
    }
}

3 个答案:

答案 0 :(得分:0)

使用您编写的两种方法,您可以像这样打印

Exam3Question2 c = new Exam3Question2();
int one = c.sumAscii(0);
int two = c.sumAscii(0, 0);
System.out.println("The sum of one String is " + one);
System.out.println("The sum of two Strings is " + two);

您正在将整数ValOne ValTwo传递给这些函数,根据您的评论,您从未计划使用它们。这些是用于创建重载函数的虚拟变量。为此目的,建议使用String个变量。由于@The Law已经为for循环提出了解决方案,我将简单地添加一个Java 8替代方案。

public int sumAscii(String s) {
    return s.chars().sum();
}

public int sumAscii(String s1, String s2) {
    return sumAscii(s1) + sumAscii(s2);
}

然后你会在你的主要电话中打电话:

public static void main(String[] args) {
    Exam3Question2 c = new Exam3Question2();
    Scanner in = new Scanner(System.in);

    System.out.println("Enter some text");
    String word1 = in.nextLine();

    System.out.println("Enter some text");
    String word2 = in.nextLine();

    int sum1 = c.sumAscii(word1);
    System.out.println("The sum of one string is: " + sum1);
    int sum2 = c.sumAscii(word1, word2);
    System.out.println("The sum of two strings is: " + sum2);
}

答案 1 :(得分:0)

您可能希望将计算一个String的方法更改为此方法,因此它将String作为参数并将userInput移到方法之外,以使其更加干净

def is_dig(s):
    if not s: # empty string
        return False
    for ch in s:
        if not 48 <= ord(ch) <= 57:
            return False
    return True

你的下一个方法实际上可以在新的重载方法中使用前面的方法,如下所示:

public int sumAscii(String userInput)
{

    String s = userInput; 
    int sum = 0;
    for (int i = 0; i < s.length(); i++)
    {
        sum+= (int)s.charAt(i);
    }
    return sum;
}

你的主要看起来像

public int sumAscii(String userInput1, String userInput2)
{

int sum = sumAscii(userInput1) + sumAscii(userInput2);
    return sum;
}

答案 2 :(得分:0)

您的代码编译存在问题吗?我想你是因为在你的主要陈述中你传递的是不存在于函数中的变量:

public static void main(String[] args) 
{
    Exam3Question2 c = new Exam3Question2();
    Scanner in = new Scanner(System.in);
    String word;
    System.out.println("Enter some text");
    word = in.nextLine();
    sumAscii(word);

    int sum1 = c.sumAscii(Input1); //What is Input1
    int sum2 = c.sumAscii(Input1, Input2); //What is Input2

    int sum3 = sum1 + sum2;
    System.out.println("The sum of the two strings is: " + sum3);
}

另一方面,没有必要创建当前类的对象来调用sumAscii()函数,因为它们是当前类的一部分:

public static void main(String[] args) 
{
    //You don't need this line
    //Exam3Question2 c = new Exam3Question2();

    // Remove the c. from the beginning of the function calls
    int sum1 = sumAscii(Input1); //What is Input1
    int sum2 = sumAscii(Input1, Input2); //What is Input2
    //
}

您希望在main中执行的操作是在sumAscii()变量上调用word,以便将结果添加到sum1

public static void main(String[] args) 
{
    Scanner in = new Scanner(System.in);
    System.out.println("Enter some text");
    String word = in.nextLine();

    int sum1 = sumAscii(word);
    System.out.println("The sum of the string " + word + " is: " + sum1);
}

如果您想用2个不同的单词调用重载的sumAscii()方法,则需要从用户那里获取另一个单词:

public static void main(String[] args) 
{
    Scanner in = new Scanner(System.in);

    System.out.println("Enter some text");
    String word = in.nextLine();

    System.out.println("Enter some more text");
    String word2 = in.nextLine();

    int sum2 = sumAscii(word, word2);
    System.out.println("The sum of the two strings is: " + sum2);
}

最后,您的重载sumAscii()方法可以简化为:

public int sumAscii(String Input1, String Input2)
{
    return sumAscii(Input1 + Input2);
}