[英]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:所以你的代码中有很多事情是不正确的,所以我将列出问题:
myexample
and set the instance variables of variableOne
and variableTwo
, and pass them as a parameter to an instance method.您正在尝试同时使用实例myexample
并设置variableOne
和variableTwo
的实例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
是可选的,但阐明了发生了什么)。variableOne
and variableTwo
from the static
context of main
using the instance, AKA use myexample.variableOne = keyboard.nextInt()
not variableOne = keyboard.nextInt()
.您需要使用实例从main
的static
上下文中设置variableOne
和variableTwo
,AKA 使用myexample.variableOne = keyboard.nextInt()
而不是variableOne = keyboard.nextInt()
。myexample.variableResultFromUserInput()
with parenthesis and the correct number of parameters accordingly.您应该使用带有括号的myexample.variableResultFromUserInput()
和相应的正确参数数量。 This variable can be completely removed as it is redundant currently.这个变量可以完全删除,因为它目前是多余的。myexample
should be myExample
, and the class should be Example
. myexample
应该是myExample
,类应该是Example
。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
而不是true
或false
。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 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.