如何将经过验证的用户输入传递给方法参数?

huangapple go评论64阅读模式
英文:

How to pass the validated user inputs to the method parameter?

问题

我有两个类:MyNumbers 和 MyScanner。我正尝试将经过验证的输入传递给 calculateSum(int x, int y) 方法,以便在 MyScanner 类中打印最终结果,但我不知道如何从 MyScanner 类中获取用户输入,然后将它们传递给 MyNumbers 类中的 validate() 方法,以便允许 calculateSum() 方法执行其任务。

提示:validate() 方法应该是无参的 void 方法,但 calculateSum() 方法应该是返回结果字符串的 string 方法,并接受两个参数。我还希望 validate() 方法提示用户输入并验证值,以确保它们在某个范围内。此方法需要不断提示,直到用户输入有效数字。

如何在不引入另一个变量/实现 setter/将变量作为参数传递给 validate() 方法的情况下实现这一点?

public class MyNumbers {

    int number1;
    int number2;

    public void validate() {
        if (number1 >= 10 && number1 <= 50) {
            if (number2 >= 5 && number2 <= 20) {
                System.out.println(calculateSum(number1, number2));
            }
        } else {
            System.out.println("Number should be between 10 and 50");
        }
    }

    public String calculateSum(int x, int y) {
        this.number1 = x;
        this.number2 = y;
        validate();
        return "Sum: " + (number1 + number2);
    }
}

public class MyScanner {

    public static void main(String[] args) {
        MyNumbers myNumbers = new MyNumbers();
        Scanner scanner = new Scanner(System.in);

        int option = scanner.nextInt();

        do {
            switch (option) {
                case 1:
                    System.out.println("Enter number 1");
                    int x = scanner.nextInt();
                    System.out.println("Enter number 2");
                    int y = scanner.nextInt();
                    myNumbers.calculateSum(x, y);
                    break;
            }
        } while (option != 0);
    }
}
英文:

I have two classes: MyNumbers and MyScanner. I am trying to pass the validated inputs to the calculateSum (int x, int y) method in order to print the final result in the MyScanner class, but I don't know how I can take the user inputs from the MyScanner class to pass them to the validate() method in the MyNumbers class so that it allows the calculateSum() method to perform its task.

P.S. validate() method should be void and parameterless, but calculateSum() method should be string to return the result as a string and take two parameters. I also want the validate() method to prompt for user input and validate the values to make sure that they are in the certain range. This method needs to keep prompting until user inserts a valid number.

How can I achieve this without introducing another variable/implementing setters/ passing variables as a parameter to the validate() method?

public class MyNumbers {
int number1;
int number2;
public void validate() {
if (number1 &gt;= 10 &amp;&amp; number1 &lt;= 50) {
if(number2 &gt;= 5 &amp;&amp; number2 &lt;= 20){
System.out.println(calculateSum(number1, number2));
}
} 
else {
System.out.println(&quot;Number should be between 10 and 50&quot;);
}
}
public String calculateSum(int x, int y) {
this.number1 = x;
this.number2 = y;
validate();
return &quot;Sum: &quot; + number1 + number2;
}
}
public class MyScanner {
public static void main(String[] args) {
MyNumbers myNumbers = new myNumbers();
Scanner scanner = new Scanner(System.in);
int option = scanner.nextInt();
do{
switch(option){
case 1:
System.out.println(&quot;Enter number 1&quot;);
int x = scanner.nextInt();
System.out.println(&quot;Enter number 2&quot;);
int y = scanner.nextInt();
myNumbers.calculateSum(x, y);
break;
}
}
while(option!=0);
}
}

答案1

得分: 1

import java.util.*;

class MyNumbers {
    
    int number1;
    int number2;
    
    public void validate(int number1 , int number2) {
        
        if (number1 >= 10 && number1 <= 50) {
            if(number2 >= 5 && number2 <= 20){
                System.out.println(calculateSum(number1, number2));
            }
        } 
                
        else {
            System.out.println("Number should be between 10 and 50");
        }
    }


    public String calculateSum(int x, int y) {
        
        this.number1 = x;
        this.number2 = y;
    
        return "Sum: " + number1 + number2;
    }
}


public class Main {

    public static void main(String[] args) {
    
        MyNumbers myNumbers = new MyNumbers();
        Scanner scanner = new Scanner(System.in);
    
        int option = scanner.nextInt();
        switch(option){
            case 1:
                System.out.println("Enter number 1");
                int x = scanner.nextInt();
                System.out.println("Enter number 2");
                int y = scanner.nextInt();
                myNumbers.validate(x, y);
                myNumbers.calculateSum(x, y);
                break;
        }
    }
}
英文:

EDIT :


import java.util.*; 
class MyNumbers {
int number1;
int number2;
public void validate() {
int valid=0;
int x;
int y;
Scanner s = new Scanner(System.in);
System.out.println(&quot;Welcome!&quot;);
do{
System.out.println(&quot;Enter Number 1: &quot;);
x = s.nextInt();
if (x &gt;= 10 &amp;&amp; x &lt;= 50) {
valid=2;
}
else {
System.out.println(&quot;Number 1 should be between 10 and 50&quot;);
}
}while(valid!=2);
do {
System.out.println(&quot;Enter Number 2: &quot;);
y = s.nextInt();
if(y &gt;= 5 &amp;&amp; y &lt;= 20){
System.out.println(calculateSum(x, y));
valid=3;
}
else {
System.out.println(&quot;Number 2 should be between 5 and 20&quot;);
}
}while(valid!=3);
}
public String calculateSum(int x, int y) {
this.number1 = x;
this.number2 = y;
return &quot;Sum: &quot; + (number1 + number2);
}
}
public class Main {
public static void main(String[] args) {
MyNumbers myNumbers = new MyNumbers();
myNumbers.validate();
}
}

Try


import java.util.*; 
class MyNumbers {
int number1;
int number2;
public void validate(int number1 , int number2) {
if (number1 &gt;= 10 &amp;&amp; number1 &lt;= 50) {
if(number2 &gt;= 5 &amp;&amp; number2 &lt;= 20){
System.out.println(calculateSum(number1, number2));
}
} 
else {
System.out.println(&quot;Number should be between 10 and 50&quot;);
}
}
public String calculateSum(int x, int y) {
this.number1 = x;
this.number2 = y;
return &quot;Sum: &quot; + number1 + number2;
}
}
public class Main {
public static void main(String[] args) {
MyNumbers myNumbers = new MyNumbers();
Scanner scanner = new Scanner(System.in);
int option = scanner.nextInt();
switch(option){
case 1:
System.out.println(&quot;Enter number 1&quot;);
int x = scanner.nextInt();
System.out.println(&quot;Enter number 2&quot;);
int y = scanner.nextInt();
myNumbers.validate(x, y);
myNumbers.calculateSum(x, y);
break;
}
}
}

huangapple
  • 本文由 发表于 2020年9月13日 16:56:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63868916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定