我正在编写一个程序,我必须使用方法来使用switch语句计算我的字符串,.upper和lower。我的代码没有显示任何可以帮助的错误。
import java.util.*;
public class Strings
{
public static void main(String[] args)
{
String selection;
Scanner keyboard = new Scanner(System.in);
System.out.println("*********** EXAM 3 ENTER A STRING *************");
System.out.println("Enter 1 to display the number of words in the string");
System.out.println("Enter 2 to display the string in all capital letters");
System.out.println("Enter 3 to display the string in all lower case letters");
System.out.println("Enter 4 to display the string in reverse order");
System.out.println("Enter -1 to exit");
selection = keyboard.nextLine();
switch(selection.charAt(0))
{
case 1:
numberOfWords(selection);
break;
case 2:
allCapitals(selection);
break;
case 3:
allLowers(selection);
break;
case 4:
reverseOrder(selection);
break;
}//ends switch
}
/*public static void menuMethod(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.println("*********** EXAM 3 ENTER A STRING *************");
System.out.println("Enter 1 to display the number of words in the string");
System.out.println("Enter 2 to display the string in all capital letters");
System.out.println("Enter 3 to display the string in all lower case letters");
System.out.println("Enter 4 to display the string in reverse order");
System.out.println("Enter -1 to exit");
}
*/
public static void numberOfWords(String selection)
{
String input; // To hold input
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a string: ");
input = keyboard.nextLine();
// Display the number of words.
System.out.println("That string has " + wordCount(input) + " words in it.");
}
public static int wordCount(String str)
{
StringTokenizer strTok = new StringTokenizer(str);
return strTok.countTokens();
}
public static void allCapitals (String str)
{
String input;
String capInput;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a string. ");
input = keyboard.nextLine();//You must use nextLine here next will not work.
capInput = input.toUpperCase();
System.out.println("Your capital case string = \n" + capInput);
}
public static void allLowers (String str)
{
String input;
String lowerInput;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a string. ");
input = keyboard.nextLine();//You must use nextLine here next will not work.
lowerInput = input.toUpperCase();
System.out.println("Your lower case string = \n" + lowerInput);
}
public static void reverseOrder (String str)
{
String input; // To hold input
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter something: ");
input = keyboard.nextLine();
// Display it backwards.
backward(input);
}
public static void backward(String str)
{
for (int i = str.length() - 1; i >= 0; i--)
System.out.print(str.charAt(i));
System.out.println();
}
}
答案 0 :(得分:3)
请使用此代码进行切换
switch(selection.charAt(0))
{
case '1':
numberOfWords(selection);
break;
case '2':
allCapitals(selection);
break;
case '3':
allLowers(selection);
break;
case '4':
reverseOrder(selection);
break;
}//ends switch
答案 1 :(得分:2)
将您的开关条件替换为
switch(Integer.parseInt(selection))
{
//your code
}
而不是
switch(selection.charAt(0))
{
//your code
}
如果使用charAt(0)
,则检查条件必须类似,因为该方法返回char值
case '1':
numberOfWords(selection);
break;
case '2':
allCapitals(selection);
break;
case '3':
allLowers(selection);
break;
case '4':
reverseOrder(selection);
break;
答案 2 :(得分:0)
您还没有说出程序输出有什么问题。尽可能彻底地解释您的问题将有助于您快速得到一个好的答案。
至于可能出现的问题:
您的allLowers()
方法正在使用toUpperCase()
。作为一个好的设计实践,尝试尽可能少地复制粘贴代码,因为复制/粘贴代码意味着可能有一个函数执行相同的操作。
您实际上从未检查过-1
的输入。您在当前方法中所做的一切(如果您要检查退出条件)是检查-
符号,因为这是第一个字符,而不是整个-1
字符串,这很容易如果你扩展程序会导致错误。