【问题标题】:How can I read any user input from the scanner library?如何从扫描仪库中读取任何用户输入?
【发布时间】:2016-01-30 10:22:02
【问题描述】:

我对 java 还很陌生,所以不要认为这是一些白痴。无论如何,我一直在尝试制作一个程序,它可以从控制台读取某个字母,然后决定使用哪个操作,比如说添加。但是,我无法获得一个 If 循环来读取决定使用哪个运算符的变量,这是代码,请帮忙。

import java.util.Scanner;


class Main {

  public static void main(String[] args) {
       Scanner user_input = new Scanner( System.in );
        int number;
        String function;
        System.out.println("What Do You Want to Do? (a to add; s to" + 
       " subrtact; d to divited; m to multiply, and sq to square your nummber.)" );
        function = user_input.next();
        if (function == "sq"){
            System.out.print("Enter your number: ");
            number = user_input.nextInt();
            System.out.print(number * number);
        } else {
            System.out.println("Unidentified Function!");
  }
}
}

(我把描述写得更短以便合适)。

【问题讨论】:

    标签: java java.util.scanner


    【解决方案1】:

    这只是一个示例,可帮助您朝着正确的方向开始。

    import java.util.Scanner;
    
    public class Example {
    
        public static void main(String[] args) {
            Scanner user_input = new Scanner(System.in);
            int num1, num2, result;
    
            System.out.println("What Do You Want to Do? (a to add; s to"
                    + " subrtact; d to divited; m to multiply, and s to square your nummber.)");
    
            String choice = user_input.next();
    
            // Add
            if (Character.isLetter('a')) {
                System.out.println("Enter first number: ");
                num1 = user_input.nextInt();
                System.out.println("Enter second number: ");
                num2 = user_input.nextInt();
    
                result = num1 + num2;
                System.out.println("Answer: " + result);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果您在扫描仪上使用 hasNext(),它将等待输入,直到您停止程序。同样使用 equals() 是比较字符串的更好方法。

      while(user_input.hasNext()){
                      function = user_input.next();
                      if (function.equals("s")){
                          System.out.print("Enter your number: ");
                          number = user_input.nextInt();
                          System.out.print(number * number);
                      } else {
                          System.out.println("Unidentified Function!");
                      }
                  }
      

      【讨论】:

        【解决方案3】:
         Scanner s = new Scanner(System.in);
            String str = s.nextLine();
        int a=s.nextInt();
        int b=s.nextInt();
            if(str.equals("+"))
            c=a+b;
            else if(str.equals("-"))
            c=a-b;
            else if(str.equals("/"))
            c=a/b;
            // you can add operators as your use
            else
            System.out.println("Unidentified operator" );
        

        希望对你有帮助!

        【讨论】:

          猜你喜欢
          • 2022-01-07
          • 1970-01-01
          • 2014-05-11
          • 1970-01-01
          • 2019-12-11
          • 1970-01-01
          相关资源
          最近更新 更多