不确定我在java中正在使用方法:/

时间:2015-02-20 01:32:08

标签: java

HIII。 我们刚刚开始学习类中的方法。我们应该编写一个程序来添加和减去任何2个整数。它应该使用一个名为validateMe的方法(我已经使用过)接受用户输入值并检查它的int数据类型与否。如果不是,该方法应显示错误消息并继续询问用户输入。如果正确,程序应继续。 关于我的计划,我有2个问题:

  1. 我的程序工作正常。方法正在使用。但我只关心它是否包含一些额外的代码。可能有一些我可以省略并使程序更短。我只关心这是否是最有效的方式。

  2. 正如你在评论中看到的那样,我添加了一个变量'error'。每次用户犯错时,这应该增加1。我应该有一个运行总数为no。所犯的错误。我的程序的这部分不起作用。 任何帮助,将不胜感激。 感谢。

    import java.util.*;
    
    public class Methods {
    
        public static void main(String[]args){
            //int errors=1;
            for(int d=1;d>0;d++)
            {
                Scanner a=new Scanner(System.in);
                System.out.println("Please enter whole number: ");
                double num1=a.nextDouble();
    
                Scanner b=new Scanner(System.in);
                System.out.println("Please enter whole number: ");
                double num2=b.nextDouble();
    
                if(Math.floor(num1)!=num1 || Math.floor(num2)!=num2)
                {
                    validateMe();
                    //errors=errors+1;
                    //System.out.println(errors);
                }
                else 
                {
                    System.out.println("Great job!");
                    System.out.println("Addition: "+(num1+num2));
                    System.out.println("Subtraction: "+(num1-num2));
                }
            }
        }
    
        public static void validateMe(){
            //int errors=1;
            System.out.println("Sorry!Try again.");
            //errors=errors+1;
            //System.out.println("Number of errors: "+errors);
    
            Scanner a=new Scanner(System.in);
            System.out.println("Please enter whole number: ");
            double num1=a.nextDouble();
    
            Scanner b=new Scanner(System.in);
            System.out.println("Please enter whole number: ");
            double num2=b.nextDouble();
    
            if(Math.floor(num1)!=num1 || Math.floor(num2)!=num2)
            {
                validateMe();
    
                //errors=errors+1;
                //System.out.println("Number of errors: "+errors);
    
            }
            else 
            {
                System.out.println("Great job!");
                System.out.println("Addition: "+(num1+num2));
                System.out.println("Subtraction: "+(num1-num2));
            }
        }
    }
    

2 个答案:

答案 0 :(得分:0)

我不确定预期的行为是什么,但是你明确重新定义 errors 变量并在每次调用方法时重新分配值1。这意味着即使你得到10个错误,你的变量永远不会保持10的值,因为它会一直被重置。

如果您尝试在整个程序期间保留所有错误的总数,则可能需要保留静态变量

public class Methods {
     private static int errors = 0;
}

因为这是一个静态变量,所以只会初始化一次:当程序开始并加载类时。

此外,由于您要求提供使代码更清晰的提示,而不是

 errors = errors + 1;

您可以使用

errors++;

具有相同的效果。

此外,您目前正在递增错误太多次。我看到你在 validateMe()方法内部以及在调用该方法之前增加变量。这引出了下一个问题:你重复整个逻辑两次!

您可能想尝试使用 do-while 循环。对于循环,while循环和do-while循环在表达能力方面基本相同。

这是一个粗略的骨架:

double num1 = 0;
double num2 = 0;

int errors = -1;
do {

    // Increment errors every time you go through the loop.
    // The first time you go through it doesn't really count since we set errors to -1
    errors++;

    if (errors > 0) {
        // If we are here, it's because there was an error
        System.out.println("Error: try again");
    }

    // Here you read a variable with Scanner. Technically you only need one, but I'm reusing your code.
    Scanner a=new Scanner(System.in);
    System.out.println("Please enter whole number: ");
    num1=a.nextDouble();

    Scanner b=new Scanner(System.in);
    System.out.println("Please enter whole number: ");
    num2=b.nextDouble();
} while (Math.floor(num1)!=num1 || Math.floor(num2)!=num2);

System.out.println("Great job!");
System.out.println("Addition: "+(num1+num2));
System.out.println("Subtraction: "+(num1-num2));

答案 1 :(得分:0)

有几种方法可以改进这个程序。首先,您不需要多次重新定义扫描仪;您可以根据需要多次使用相同的扫描仪变量。第二件事是你应该以不同的方式使用validateMe()方法,以便你可以重复使用多种用途。

这是做你想做的事情的另一种方式:

import java.util.*;

public class Methods {

    public static void main(String[] args) {
        double numberOne;
        double numberTwo;
        System.out.println("This program will both add and subtract two numbers together.\nPlease only use integers.");
        numberOne = getInt();
        numberTwo = getInt();
        System.out.println("Great Job!");
        System.out.println("Addition: "+ (numberOne + numberTwo));
        System.out.println("Subtraction: " + (numberOne - numberTwo));
    }

    public static double getInt() {
        int count = 0;
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter a number: ");
        double number = input.nextDouble();
        if (!validateMe(number)) {
            count++;
            return getInt();
        }
        else {
            System.out.println("You have incorrectly entered numbers " + count + " times!");
            return number;
        }
    }

    public static boolean validateMe(double number) {
        if (Math.floor(number) != number) {
            System.out.println("You entered an incorrect number!");
            return false;
        }
        else {
            System.out.println("You entered a correct number!");
            return true;
        }
    }
}
  

其他一些提示

  • 我知道你是编程的新手,但是你应该开始习惯使用偶数间距和标签,这样一切都更容易阅读。我知道StackOverflow可能会破坏您的格式,但请确保在 " +" 之类的内容之间添加空格以提高程序的可读性。

  • 在创建方法时,请尝试创建更多方法,以便将它们用于不同目的以缩短代码。


您决不是要使用我为这个问题创建解决方案的方式, ,这是最好的部分!您可以按照自己的方式制作程序!稍后通过缩短使用方法重复使用的代码,尽量让自己更轻松。

相关问题