如何忽略大小写?

huangapple go评论81阅读模式
英文:

How to ignore lettercase?

问题

import java.util.Scanner;

public class NewClass2 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        char milk;
        int s, h, price;
        h = 0;
        price = 0;
        String type, size;
        System.out.println("NutMart");
        System.out.println("Milk selections:\n A. Milk A \n\t 1. Regular ($10) \n\t 2. Medium ($20) \n\t 3. Large ($30)");
        System.out.println(" B. Milk B \n\t 1. Regular ($15) \n\t 2. Medium ($30) \n\t 3. Large ($45)");
        System.out.println(" C. Milk C \n\t 1. Regular ($20) \n\t 2. Medium ($40) \n\t 3. Large ($60)");

        System.out.println("Insert Milk Type: ");
        milk = input.next().charAt(0);

        switch (Character.toLowerCase(milk)) {
            case 'a':
                type = "Milk A";
                h = 10;
                break;
            case 'b':
                type = "Milk B";
                h = 15;
                break;
            case 'c':
                type = "Milk C";
                h = 20;
                break;
            default:
                System.out.println("Invalid!");
                System.out.println("Please select the correct choice: ");
                milk = input.next().charAt(0);
                break;
        }

        System.out.println("Select the size: ");
        while (!input.hasNextInt()) input.next();
        s = input.nextInt();

        switch (s) {
            case 1:
                size = "Regular";
                price = h * 1;
                break;
            case 2:
                size = "Medium";
                price = h * 2;
                break;
            case 3:
                size = "Large";
                price = h * 3;
                break;
            default:
                System.out.println("Invalid");
                System.out.println("Please select the correct choice: ");
                while (!input.hasNextInt()) input.next();
                s = input.nextInt();
                break;
        }
        System.out.println("Individual Price: $" + price);
        System.out.println("Please insert the quantity: ");
        while (!input.hasNextInt()) input.next();
        int quantity = input.nextInt();

        int total = price * quantity;

        System.out.println("Your total will be $" + total);
    }
}
英文:

So, I'm trying to make a simple menu with switches.

I have a letter choice inside it. I'm using next().charAt(0); to scan a letter.

It worked well, but I want to simplify it. you see, I have to make a case each choice both uppercase and lowercase.

So how to ignore case so I don't have to make both cases?

Also note: I'm using the old version of Java and Netbeans 8.2 as my IDE. (because my college keeps insisting not to use the newer one. Probably because they don't have the textbook yet.), so probably newer syntax wouldn't work.

my code:

import java.util.Scanner;
public class NewClass2 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
char milk;
int s, h, price;
h = 0;
price = 0;
String type, size;
System.out.println("NutMart");
System.out.println("Milk selections:\n A. Milk A \n\t 1. Regular ($10) \n\t 2. Medium ($20) \n\t 3. Large ($30)");
System.out.println(" B. Milk B \n\t 1. Regular ($15) \n\t 2. Medium ($30) \n\t 3. Large ($45)");
System.out.println(" C. Milk C \n\t 1. Regular ($20) \n\t 2. Medium ($40) \n\t 3. Large ($60)");
System.out.println("Insert Milk Type: ");
milk = input.next().charAt(0);
switch(milk){
case 'a':
type = "Milk A";
h = 10;
break;
case 'b':
type = "Milk B";
h = 15;
break;
case 'c':
type = "Milk C";
h = 20;
break;
case 'A':
type = "Milk A";
h = 10;
break;
case 'B':
type = "Milk B";
h = 15;
break;
case 'C':
type = "Milk C";
h = 20;
break;
default: 
System.out.println("Invalid!");
System.out.println("Please select the correct choice: ");
milk = input.next().charAt(0);
break;
}
System.out.println("Select the size: ");
while (!input .hasNextInt()) input .next();
s = input.nextInt();
switch(s){
case 1:
size = "Regular";
price = h * 1;
break;
case 2:
size = "Medium";
price = h * 2;
break;
case 3:
size = "Large";
price = h * 3;
break;
default: 
System.out.println("Invalid");
System.out.println("Please select the correct choice: ");
while (!input .hasNextInt()) input .next();
s = input.nextInt();
break;
}
System.out.println("Individual Price: $" + price);
System.out.println("Please insert the quantity: ");
while (!input .hasNextInt()) input .next();
int quantity = input.nextInt();
int total = price * quantity;
System.out.println("Your total will be $" + total );
}
}
</details>
# 答案1
**得分**: 2
注意, 在这种情况下,转换为一致的大小写是实现目标的最佳方法。 但是当您在switch语句中针对不同输入有各种结果时,您可以对case进行以下处理。
  case 'a':
case 'A':
type = "Milk A";
h = 10;
break;
...
无论提供的是'a'还是'A',case都会简单地继续执行。
<details>
<summary>英文:</summary>
Note, that in this case converting to a consistent case is the best was to achieve your goal.  But when you have a variety of results for dissimilar inputs in a switch statement you can do the following for case constructs.
  case &#39;a&#39;:
case &#39;A:
type = &quot;Milk A&quot;;
h = 10;
break;
...
The case will simply fall thru whether &#39;a&#39; or &#39;A&#39; was provided.
</details>

huangapple
  • 本文由 发表于 2020年10月22日 23:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64485365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定