【问题标题】:How would I get a String from another class?我如何从另一个类中获取字符串?
【发布时间】:2017-11-19 21:43:30
【问题描述】:
import java.util.*;

class Player {
    public static void main (String [] args) {
        String number = Text.nextLine
    }
}

我想要这个类的用户输入和 带入另一个类并使用数字变量 如果语句

【问题讨论】:

  • 请提供更多信息。
  • Text 似乎是一个类,nextLine 是一个成员变量,是吗?如果它在哪里初始化对象?还是ScannernextLine()
  • 是的,我正在使用扫描仪,是的
  • 看看我的回答。这是你要求的吗?

标签: java string user-input


【解决方案1】:

我希望这个类的用户输入并带入另一个类和 对 If 语句使用 number 变量。

看看下面的例子很简单(确保在一个包中添加两个类不同的java文件作为Player.javaExampleClass.java ),

这是Scanner 的类:

import java.util.*;

public class Player{
    public static void main (String [] args){

        Scanner getInput = new Scanner(System.in);
        System.out.print("Input a number");
        //you can take input as integer if you want integer value by nextInt()
        String number = getInput.nextLine();

        ExampleClass obj = new ExampleClass(number);
        obj.checkMethod();
    }
}

这是查号类:

public class ExampleClass{
    int number;
    public ExampleClass(String number){
        try{
            //If you want to convert into int
            this.number = Integer.parseInt(number);
        }catch(NumberFormatException e){
            System.out.println("Wrong input");
        }
    }

    public void checkMethod(){
        if(number > 5){
            System.out.println("Number is greater.");
        }else{
            System.out.println("Number is lesser.");
        }
    }
}

没什么好说的:

您的示例代码包含语法错误,请先修复这些错误。

  1. 如果你想要整数,你可以使用getInput.nextInt() 而不是 getInput.nextLine()
  2. 您可以创建 getter 和 setter 来设置值和获取值。在我的示例中,我只是通过构造函数设置值。
  3. 使用正确的命名约定。
  4. 在我的示例中,我在构造函数中将String 转换为整数并用try-catch 块包裹以防止NumberFormatException(如果您输入字符或您可以看到wrong input 的内容将打印) .有时在各种情况下,在构造函数中使用 try-catch 并不好。要了解更多信息,请阅读Try / Catch in Constructor - Recommended Practice

【讨论】:

    【解决方案2】:

    您通常在其他类上进行导入,只需导入 Player 类,它应该可以工作

    【讨论】:

      【解决方案3】:

      我不确定我是否得到它,我想你正在使用扫描仪。 我会这样做:

      扫描仪类:

      public class ScannerTest { 
      
          public static void main(String[] args) {
      
              Scanner scanner = new Scanner(System.in);
      
              while (true) {
                  System.out.println("Insert a decimal:");
                  String inputValue = scanner.nextLine();
                  if(!new ScannerCalc().isNumeric(inputValue)){
                      System.out.println("it's not a number...");
                      break;
                  } 
                  else
                      new ScannerCalc().checkNumber(inputValue);
              }
          }
      }
      

      ScannerCalc 类:

      public class ScannerCalc {
      
          public boolean isNumeric(String s) {  
              return s != null && s.matches("[-+]?\\d*\\.?\\d+");  
          }  
      
          public void checkNumber(String number){
      
              if(Integer.parseInt(number)%2==0)
                  System.out.println("it' even");
              else
                  System.out.println("it's odd");
      
          }
      }
      

      注意类的实例化以重用方法。

      【讨论】:

        【解决方案4】:

        如果您想在另一个实体中使用局部变量,最好将其作为参数传递给另一个实体的方法。例如

        OtherClass.operation(scanner.nextLine());   // In case method is static
        
        new OtherClass().operation(scanner.nextLine());   // In case method is not static
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-25
          • 2014-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-05
          相关资源
          最近更新 更多