简体   繁体   English

如何在java方法中利用用户输入?

[英]How to utilize user input in java methods?

I have some methods that should incorporate user Input.我有一些方法应该包含用户输入。 I put the scanner in the main method, but the values entered by the user are not getting returned, nor getting used in the code.我将扫描器放在 main 方法中,但是用户输入的值没有被返回,也没有在代码中使用。 How can I turn the user input into a var that can be accessed by the methods?如何将用户输入转换为可由方法访问的 var?

for example:例如:

import java.util.Scanner;
public class example {

int variableOne;
int variableTwo;
int variableResultFromUserInput;

public int variableResultFromUserInput(int variableOne, int variableTwo) {
    this.variableResultFromUserInput = variableOne * variableTwo;
    return this.variableResultFromUserInput;

}
public static void main(String[]args) {
    example myexample = new example();
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What's your variable one? ");
    variableOne = keyboard.nextInt(); //this is the variable that causes trouble

    System.out.println("Whats your variable two?");
    variableTwo = keyboard.hasNextInt(); //this is the second variable that causes trouble

    System.out.println(myexample.variableResultFromUserInput); //this is also wrong
}

}

So there are a lot of things going on in your code that are incorrect, so I will list out the problems:所以你的代码中有很多事情是不正确的,所以我将列出问题:

  1. You are attempting to use both the instance myexample and set the instance variables of variableOne and variableTwo , and pass them as a parameter to an instance method.您正在尝试同时使用实例myexample并设置variableOnevariableTwo的实例variableTwo并将它们作为参数传递给实例方法。 This is completely redundant and serves no purpose, you already have access to them from within variableResultFromUserInput() .这是完全多余的,没有任何用处,您已经可以从variableResultFromUserInput()访问它们。 Access them with this.variableOne (though the this is optional, but clarifies what is going on).使用this.variableOne访问它们(尽管this是可选的,但阐明了发生了什么)。
  2. You need to set variableOne and variableTwo from the static context of main using the instance, AKA use myexample.variableOne = keyboard.nextInt() not variableOne = keyboard.nextInt() .您需要使用实例从mainstatic上下文中设置variableOnevariableTwo ,AKA 使用myexample.variableOne = keyboard.nextInt()而不是variableOne = keyboard.nextInt()
  3. You named a variable the same as your instance method, and were attempting to read that instead of calling the method.您命名了一个与实例方法相同的变量,并试图读取它而不是调用该方法。 You should be using myexample.variableResultFromUserInput() with parenthesis and the correct number of parameters accordingly.您应该使用带有括号的myexample.variableResultFromUserInput()和相应的正确参数数量。 This variable can be completely removed as it is redundant currently.这个变量可以完全删除,因为它目前是多余的。
  4. You do not follow Java naming conventions for the class name, or for variable names.对于类名或变量名,您不遵循 Java 命名约定。 myexample should be myExample , and the class should be Example . myexample应该是myExample ,类应该是Example
  5. variableTwo = keyboard.hasNextInt(); should be keyboard.nextInt() not hasNextInt() .应该是keyboard.nextInt()而不是hasNextInt() hasNextInt only checks to see if there is an integer available, but it does not actually read the integer, it returns a boolean instead that is true or false . hasNextInt只检查是否有可用的整数,但它实际上并不读取整数,它返回一个boolean而不是truefalse

Here is code with all the changes made below:这是包含以下所有更改的代码:

public class Example {

    int variableOne;
    int variableTwo;

    public int variableResultFromUserInput() {
        return this.variableOne * this.variableTwo;
    }

    public static void main(String[]args) {
        Example myExample = new Example();
        Scanner keyboard = new Scanner(System.in);
        System.out.println("What's your variable one? ");
        myExample.variableOne = keyboard.nextInt(); //this is the variable that causes trouble

        System.out.println("Whats your variable two?");
        myExample.variableTwo = keyboard.nextInt(); //this is the second variable that causes trouble

        System.out.println(myExample.variableResultFromUserInput()); //this is also wrong
    }
}

You have done several mistakes in your code.您在代码中犯了几个错误。

  • You didn't call your method.Instead of that, you printed a variable.你没有调用你的方法。相反,你打印了一个变量。
  • HasNextInt is used to check whether there is int in the line that you are reading. HasNextInt 用于检查您正在阅读的行中是否有 int。 It only returns a boolean.它只返回一个布尔值。
  • You have used misleading method names and variable names.您使用了误导性的方法名称和变量名称。

     import java.util.Scanner; public class Main { private static int variableOne; private static int variableTwo; int variableResultFromUserInput; public int variableResultFromUserInput(int variableOne, int variableTwo) { this.variableResultFromUserInput = variableOne * variableTwo; return this.variableResultFromUserInput; } public static void main(String[] args) { Main myexample = new Main(); Scanner keyboard = new Scanner(System.in); System.out.println("What's your variable one? "); variableOne = keyboard.nextInt(); System.out.println("Whats your variable two?"); variableTwo = keyboard.nextInt(); System.out.println(myexample.variableResultFromUserInput(variableOne,variableTwo)); }

    } }

In the last line of your code,在代码的最后一行,

System.out.println(myexample.variableResultFromUserInput);

you are not actually calling the method.您实际上并没有调用该方法。 Your code should differ thus:您的代码应该有所不同:

System.out.println(myexample.variableResultFromUserInput(variableOne,variableTwo);

In the the method "variableResultFromUserInput," you are recursively calling the method.在方法“variableResultFromUserInput”中,您递归地调用该方法。 While there is a way to accomplish what you are doing, I recommend to instead return the mathematical operation.虽然有一种方法可以完成您正在做的事情,但我建议改为返回数学运算。 This would look like the following:这将如下所示:

public int variableResultFromUserInput(int variableOne, int variableTwo) {
return variableOne * variableTwo;}

Let me know how this works out.让我知道这是如何运作的。

Your code have several problems, try out this version:你的代码有几个问题,试试这个版本:

import java.util.Scanner;
public class Example {

  public int variableResultFromUserInput(int variableOne, int variableTwo) {
     return variableOne * variableTwo;
   }

   public static void main(String[]args) {
    Example myexample = new Example();
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What's your variable one? ");
    int variableOne = keyboard.nextInt();

    System.out.println("Whats your variable two?");
    int variableTwo = keyboard.nextInt();

    System.out.println(myexample.variableResultFromUserInput(variableOne, variableTwo));   }
}

You have to put this content in file named Example.java, compile with你必须把这个内容放在名为 Example.java 的文件中,编译

javac Example.java 

and run with并运行

java Example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM