首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用户输入upperCase和lowerCase - Java

用户输入upperCase和lowerCase - Java
EN

Stack Overflow用户
提问于 2017-11-18 12:19:17
回答 4查看 8.7K关注 0票数 0

我尝试接受大写或LowerCase形式的A或B输入,以继续执行if语句。我想不出该怎么做。目前,它只接受UpperCase A或B。环顾论坛和课堂笔记,什么都没有。任何帮助都会得到重视!我尝试使用toUpperCase(),但我认为我没有把它放在正确的位置。它一直给我一个错误

代码语言:javascript
复制
import java.util.Scanner;

public class printNumber{
    public static void main (String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("Please select an option between A or B: ");
        char user = input.next().charAt(0);

        System.out.println("You have selected " +user+ " : ");    
        if (user == 'A'){
            for(int n=1; n<=100; n++){
                if((n%2)==0)
                    System.out.println(n);
            }
        }
        else if (user =='B'){
            for (int x =1; x<=100; x=x+2){
                System.out.println(x);
            }
        }
        else
            System.out.println("Error, please try again!");
    }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-11-18 12:37:32

您可以使用

代码语言:javascript
复制
char user = input.next().toUpperCase().charAt(0);
票数 2
EN

Stack Overflow用户

发布于 2017-11-18 13:53:58

代码语言:javascript
复制
import java.util.Scanner;

public class printNumber{
  public static void main (String[] args){
        Scanner input = new Scanner(System.in);

        System.out.println("Please select an option between A or B: ");
    String opt=input.nextLine();

    System.out.println("You have selected " +user+ " : ");

    if (opt.equalsIgnoreCase("A")){

    for(int n=1; n<=100; n++){
      if((n%2)==0)
        System.out.println(n);
     }
    }
    else if (opt.equalsIgnoreCase("B")){
    for (int x =1; x<=100; x=x+2){
      System.out.println(x);
     }
    }
    else
      System.out.println("Error, please try again!");
  }
}

或者您可以简单地使用

代码语言:javascript
复制
  import java.util.Scanner;

    public class printNumber{
      public static void main (String[] args){
            Scanner input = new Scanner(System.in);

            System.out.println("Please select an option between A or B: ");
        String opt=input.next();

        System.out.println("You have selected " +user+ " : ");

        if (opt == "A" || opt == "a"){

        for(int n=1; n<=100; n++){
          if((n%2)==0)
            System.out.println(n);
         }
        }
        else if (opt == "B" || opt == "b"){
        for (int x =1; x<=100; x=x+2){
          System.out.println(x);
         }
        }
        else
          System.out.println("Error, please try again!");
      }
    }
票数 2
EN

Stack Overflow用户

发布于 2017-11-18 12:21:20

试试这个

代码语言:javascript
复制
if (user == 'A' || user == 'a')

'B‘也是如此

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47362456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档