繁体   English   中英

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

[英]How to print return from overloaded methods in Java

我在尝试遵循以下说明的同时求和不同字符串的Ascii值:

创建两个重载方法。 一个将给出一个字符串中每个字符的ascii值之和,另一个将产生两个String的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);
    }
}

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

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);
}

您可能需要将用于计算一个String的方法更改为此,因此它将String作为参数并将userInput移动到该方法之外,以使其更干净

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;
}

你的主看起来像

 public static void main(String[] args) 
{
//something to invoke constructor and get strings
int sum1Str= sumAscii(userInput1)
int sum2Str = sumAscii(userInput1,userInput2)
System.out.println("The sum of one string is "+sum1Str);
System.out.println("The sum of two strings is " +sum2Str);
}

您的代码编译有问题吗? 我认为您是因为在主语句中传递了不存在的变量到函数中:

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
    //
}

您主要要做的是在word变量上调用sumAscii() ,以便可以将结果添加到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);
}

如果要用两个不同的词调用重载的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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM