使用 az 输入进行循环

我对 Java 完全是菜鸟,但我希望制作一个程序,接受用户的输入,[A/a] - [C/c]、[D/d] - [F/f] 等等,然后返回一个值([A/aC/c = 1], [D/dF/f = 2]....


如果输入不是 AZ 或 az,则返回“无效输入”。(我想我自己可以解决这个问题)。


我想我可以在输入上做一个“ToUpperCase”语句,但我不完全确定如何这样做。


我不想使用任何特殊的数据库。


到目前为止,这是我的代码:


导入java.util.Scanner;


公共课 TelefonTastatur {


public static void main(String[] args) {


    String korrTall = "Korresponderer til tallet "; //Strings are in Norwegian, but I don't need help with those :-)


    System.out.println("Dette programmet konverterer bokstav-input til korresponderende tall på et telefontastatur.");

    System.out.println("Oppgi en bokstav (A-Z: "); //Asks user for A-Z input.


    Scanner sc = new Scanner(System.in);


    char c = sc.next().charAt(0); //Perhaps toUpperCase to lower number of switch cases?


    switch (c) {

        case ('A'-'C'): //Not sure how to make A-C and/or a-c. I could make an individual case for all inputs, but that would make a lot of code.

        case ('a'-'c'):

        System.out.print(korrTall + "2.");

        break;


        case (D/d - F/f):

        case ():

            System.out.print(korrTall + "3.");

        break;


        case (G/g - I/i):

        case ():

            System.out.print(korrTall + "4.");

        break;


        case (J/j - L/l):

        case ():

            System.out.print(korrTall + "5.");

        break;


        case (M/m - O/o):

        case ():

            System.out.print(korrTall + "6.");

        break;


        case (P/p - S/s):

        case ():

            System.out.print(korrTall + "7.");

        break;


        case (T/t - V/v):

        case ():

            System.out.print(korrTall + "8.");

        break;


        case (W/w - Z/z):

        case ():

            System.out.print(korrTall + "9.");

        break;


        case 'F':

        case 'f':

            System.out.print(korrTall + "0.");

        break;


        default:

            System.out.println("Det du har tastet inn tilsvarer ikke noe tall på et telefontastatur.");

        break;  


    }


}

}


慕桂英546537
浏览 131回答 3
3回答

有只小跳蛙

如果您想读取用户的一封信,您可以使用readInput()代码片段中提供的内容。然后,例如在您的 中main(),您可以要求用户输入 2 个字母,然后您将向他提供结果。    public static void main(String[] args) {        try{            char inputOne = readInput();            char inputTwo = readInput();            handle(inputOne,inputTwo);        }catch(Exception e){            System.out.println(e.getMessage());        }    }    static char readInput(){        System.out.println("Insert a character");        String input = Console.readLine();        if (input.length==0) {            char c = input.charAt(0);            if (Character.isLetter(c)) {                return c;            }        }        throw new Exception("Invalid input!");    }    static void handle(char a, char b){        // your logic to handle the input of the user    }

慕容森

您将必须使用 Scanner 类来完成用户输入。import java.util.Scanner;然后创建一个接受键盘输入的变量。Scanner keyboard = new Scanner(System.in);System.out.println("Enter a letter: ");String text = keyboard.nextLine();创建一个方法,返回给定字符的数字。 在Java中a-z等于,并且等于.97-122A-Z65-90public int toNum(String text) {&nbsp; //Ensure that the text is only 1 character long.&nbsp; if(text.length() > 1) return -1;&nbsp; //Convert the one-character String to type char.&nbsp; char letter = text.charAt(0);&nbsp; //Convert char to its equivalent number value.&nbsp; int rawNum = letter;&nbsp; int newNum;&nbsp; //Convert the number to a value 0-25.&nbsp; if(rawNum >= 'a' && rawNum <= 'z') {&nbsp; &nbsp; newNum = rawNum - 'a';&nbsp; } else if(rawNum >= 'A' && rawNum <= 'Z') {&nbsp; &nbsp; newNum = rawNum - 'A';&nbsp; } else {&nbsp; &nbsp; //None of the characters were letters A-Z.&nbsp; &nbsp; System.out.println("Invalid input");&nbsp; &nbsp; return -1;&nbsp; }&nbsp; //If {a,b,c} are 1 and {d,e,f} are 2, then {0,1,2} -> 1 and {3,4,5} -> 2&nbsp; //Take the floor of the new number divided by 3.&nbsp; int toReturn = Math.floor(newNum / 3.0) + 1;&nbsp; return toReturn;}现在只需使用用户输入调用您的方法即可。toNum(text);您也可以打印用户输入的返回值。System.out.println(toNum(text));

慕工程0101907

你的问题根本不清楚,但我尽力帮助你。下次发布你尝试过的内容。这个简单的代码会对您有所帮助,这可以改进,所以让我们这样做:DScanner scanner = new Scanner(System.in);System.out.println("Insert first char");String firstChar = scanner.next().toUpperCase();if (firstChar.length() != 1 || (firstChar.toCharArray()[0] < 65 || firstChar.toCharArray()[0] > 90)) {&nbsp; &nbsp; System.out.println("Please, insert one single character [a-z/A-Z]");&nbsp; &nbsp; return;}System.out.println("Insert second char");String secondChar = scanner.next().toUpperCase();if (secondChar.length() != 1 || (secondChar.toCharArray()[0] < 65 || firstChar.toCharArray()[0] > 90)) {&nbsp; &nbsp; System.out.println("Please, insert one single character");&nbsp; &nbsp; return;}System.out.println(firstChar + " - " + secondChar + " = " + Math.abs(firstChar.toCharArray()[0] - secondChar.toCharArray()[0]));请注意,您可以创建方法来执行重复操作。在这个简单的示例中,您可以创建一个方法来检查您刚刚从键盘读取的内容是否是单个字符。您可以做的另一项改进是处理用户插入错误的内容。让我们尝试编码:D 再见
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java