亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 az 輸入進行循環

使用 az 輸入進行循環

慕桂英546537 2023-08-16 18:06:13
我對 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;      }}}
查看完整描述

3 回答

?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

如果您想讀取用戶的一封信,您可以使用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

    }


查看完整回答
反對 回復 2023-08-16
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

您將必須使用 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-90


public int toNum(String text) {

  //Ensure that the text is only 1 character long.

  if(text.length() > 1) return -1;


  //Convert the one-character String to type char.

  char letter = text.charAt(0);


  //Convert char to its equivalent number value.

  int rawNum = letter;

  int newNum;


  //Convert the number to a value 0-25.

  if(rawNum >= 'a' && rawNum <= 'z') {

    newNum = rawNum - 'a';

  } else if(rawNum >= 'A' && rawNum <= 'Z') {

    newNum = rawNum - 'A';

  } else {

    //None of the characters were letters A-Z.

    System.out.println("Invalid input");

    return -1;

  }


  //If {a,b,c} are 1 and {d,e,f} are 2, then {0,1,2} -> 1 and {3,4,5} -> 2

  //Take the floor of the new number divided by 3.

  int toReturn = Math.floor(newNum / 3.0) + 1;

  return toReturn;

}

現在只需使用用戶輸入調用您的方法即可。


toNum(text);

您也可以打印用戶輸入的返回值。


System.out.println(toNum(text));


查看完整回答
反對 回復 2023-08-16
?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

你的問題根本不清楚,但我盡力幫助你。下次發布你嘗試過的內容。


這個簡單的代碼會對您有所幫助,這可以改進,所以讓我們這樣做:D


Scanner 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)) {

    System.out.println("Please, insert one single character [a-z/A-Z]");

    return;

}

System.out.println("Insert second char");

String secondChar = scanner.next().toUpperCase();

if (secondChar.length() != 1 || (secondChar.toCharArray()[0] < 65 || firstChar.toCharArray()[0] > 90)) {

    System.out.println("Please, insert one single character");

    return;

}


System.out.println(firstChar + " - " + secondChar + " = " + Math.abs(firstChar.toCharArray()[0] - secondChar.toCharArray()[0]));

請注意,您可以創建方法來執行重復操作。在這個簡單的示例中,您可以創建一個方法來檢查您剛剛從鍵盤讀取的內容是否是單個字符。


您可以做的另一項改進是處理用戶插入錯誤的內容。


讓我們嘗試編碼:D 再見


查看完整回答
反對 回復 2023-08-16
  • 3 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號