2015-11-01 44 views
2

我试图总结不同的字符串的ASCII值,同时秉承了以下说明:如何打印从重载方法返回在Java中

创建两个重载方法。其中一个将给出字符串中每个字符的 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); 
    } 
} 
+0

你确定计算每个字符的ascii值的逻辑吗?首先重点说明,主要方法不会是一个问题 –

+0

会做。谢谢 – Bob

+0

@BalwinderSingh这是一个正确的方法来获取字符串的每个字符的ASCII码。 –

回答

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

你逝去的整数(S)ValOneValTwo这些功能,并根据您的意见两种方法,你从来没有打算使用它们。这些是用于创建重载函数的虚拟变量。为此,建议使用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); 
} 
+0

他获得任何整数ascii值的逻辑是完全错误的。来自main方法的调用对他现在不会有任何帮助,但是一旦他找出了获得任何整数的ascii值的逻辑,他就会这样做。 –

+0

他的获取字符串中每个char的ascii值的逻辑实际上是正确的。 –

+0

这是真的。但他希望使用在方法调用期间传递的并且不用于获得给定ASCII值的整数 –

0

您可能需要外部改变您的方法来计算一个字符串到这一点,所以它需要的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); 
} 
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 
    // 
} 

要在主要以做的是在你的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()方法有2个不同的字,你需要从用户那里得到另一个词:

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