我在使用某些java代码时遇到了一些问题,所以我想我会在这里问一下。我被要求添加一行代码来处理小写输入,但我不确定如何实现它。我可能非常喜欢看起来很容易解决,但是非常感谢任何帮助。
import java.util.Scanner;
public class Worksheet_7
{
public static void main(String[]args)
{
Scanner Fred = new Scanner(System.in);
System.out.println("Today we practice using switch statements \n\n");
char Ans;
System.out.println("What kind of car do you drive");
System.out.println("(F)ord, (C)hevy, (B)uick,(M)azda,(H)onda");
String Word = Fred.next();
Ans=Word.charAt(0);
switch(Ans)
{
case 'C': System.out.println("Chevy-the heartbeat of America");
System.out.println("Buy a ford");
break;
case 'F': System.out.println("Ford tough");
System.out.println("Nice job cowboy");
break;
case 'B': System.out.println("Buick-Luxury you can afford");
System.out.println("Seriously? A Buick? That's not Luxury");
break;
case 'M': System.out.println("Mazda-Zoom, Zoom");
System.out.println("Can't really say anything");
break;
case 'H': System.out.println("Honda-Always dependable");
System.out.println("Yeah, yeah, yeah");
break;
default: System.out.println("Need a better car...?");
}
System.out.println("\n\n done for now..");
}
}
答案 0 :(得分:0)
Fred.next()
是String
您需要在某处调用 String
的public String toUpperCase()
方法。
注意:它返回一个新的String,而不是大写。
或者有Character
方法 public static Character toUpperCase(char ch)
或者您可以更新案例
case 'c':
case 'C':
...
break;
case 'f':
case 'F':
...
break;
答案 1 :(得分:0)
试试这个,用户char
代替public static void main(String[]args)
{
Scanner Fred = new Scanner(System.in);
System.out.println("Today we practice using switch statements \n\n");
String Ans;
System.out.println("What kind of car do you drive");
System.out.println("(F)ord, (C)hevy, (B)uick,(M)azda,(H)onda");
String Word = Fred.next();
Ans=Word.substring(0, 1).toUpperCase();
switch(Ans)
{
case "C": System.out.println("Chevy-the heartbeat of America");
System.out.println("Buy a ford");
break;
case "F": System.out.println("Ford tough");
System.out.println("Nice job cowboy");
break;
case "B": System.out.println("Buick-Luxury you can afford");
System.out.println("Seriously? A Buick? That's not Luxury");
break;
case "M": System.out.println("Mazda-Zoom, Zoom");
System.out.println("Can't really say anything");
break;
case "H": System.out.println("Honda-Always dependable");
System.out.println("Yeah, yeah, yeah");
break;
default: System.out.println("Need a better car...?");
}
System.out.println("\n\n done for now..");
}
:
AppDelegate.swift
答案 2 :(得分:0)
您只需将开关更改为
即可switch (ans.toUpperCase()){