好的,既然我已将public static String
更改为public static int
,则最后一项功能无法打印出来。
谢谢大家的帮助。
这是完整的程序。最后一个功能似乎没有打印。
import java.lang.String;
import java.util.Scanner;
public class stringFuncts{
/**
* @param <String> <str> <Takes in a String and reverses it.>
* @return <rev> <returns the reversed string>
*/
public static String reverseString (String str){
String rev = "";
int length = str.length();
for ( int i = length - 1 ; i >= 0 ; i-- ){
rev = rev + str.charAt(i);
}
return rev;
}
/**
* @param <int> <n> <It will sum up the odds of a set number>
* @return <results> <Will print out the total of the odds values>
*/
public static int sumOfOdds (int n){
int results = 0;
for(int i = 1; i <= n*2; i += 2){
results = results + i;
}
return results;
}
/**
* @param <int> <blanks> <Will count the amount of whitespace in the phrase>
* @return <numBlanks> <Will return the amount of whitespace found in the String>
*/
public static int numberOfBlanks(String blanks){
int numBlanks = 0;
for(int i = 0; i < blanks.length(); i++) {
if(Character.isWhitespace(blanks.charAt(i)))
numBlanks++;
}
return numBlanks;
}
public static void main(String [] args){
Scanner input = new Scanner(System.in);
String str;
int n = 0;
String blanks;
System.out.println("Enter a string to reverse");
str = input.nextLine();
System.out.println("The reverse output is: " + reverseString(str));
System.out.println("");
System.out.println("Enter a value to sum the odds");
n = input.nextInt();
System.out.println("The sum of the odds " + sumOfOdds(n));
System.out.println("");
System.out.println("Enter a string to find the amount of blanks");
blanks = input.nextLine();
System.out.print("The number od blanks in the string is: " + numberOfBlanks(blanks));
}
}
答案 0 :(得分:2)
在numberOfBlanks()
返回类型中,String
数据类型为int
将其更改为{{1}}数据类型
答案 1 :(得分:1)
您的函数声明为public static String
。您正在尝试返回一个数字,因此正确的声明将为public static int
。
答案 2 :(得分:0)
import java.lang.String;
import java.util.Scanner;
public class stringFuncts{
/**
* @param <String> <str> <Takes in a String and reverses it.>
* @return <rev> <returns the reversed string>
*/
public static String reverseString (String str){
String rev = "";
int length = str.length();
for ( int i = length - 1 ; i >= 0 ; i-- ){
rev = rev + str.charAt(i);
}
return rev;
}
/**
* @param <int> <n> <It will sum up the odds of a set number>
* @return <results> <Will print out the total of the odds values>
*/
public static int sumOfOdds (int n){
int results = 0;
for(int i = 1; i <= n*2; i += 2){
results = results + i;
}
return results;
}
/**
* @param <int> <blanks> <Will count the amount of whitespace in the phrase>
* @return <numBlanks> <Will return the amount of whitespace found in the String>
*/
public static int numberOfBlanks(String blanks){
int numBlanks = 0;
for(int i = 0; i < blanks.length(); i++) {
if(Character.isWhitespace(blanks.charAt(i)))
numBlanks++;
}
return numBlanks;
}
public static void main(String [] args){
Scanner input = new Scanner(System.in);
String str;
int n = 0;
String blanks;
System.out.println("Enter a string to reverse");
str = input.nextLine();
System.out.println("The reverse output is: " + reverseString(str));
System.out.println("");
System.out.println("Enter a value to sum the odds");
n = input.nextInt();
System.out.println("The sum of the odds " + sumOfOdds(n));
System.out.println("");
input.nextLine();
System.out.println("Enter a string to find the amount of blanks");
blanks = input.nextLine();
System.out.print("The number od blanks in the string is: " + numberOfBlanks(blanks));
}
}
输出:
Enter a string to reverse
this
The reverse output is: siht
Enter a value to sum the odds
3
The sum of the odds 9
Enter a string to find the amount of blanks
this is spartan!
The number od blanks in the string is: 2