运算符的整数输入

时间:2017-03-11 07:11:09

标签: java

我需要知道如何为这个简单的程序获取操作符的输入我正在这样做;该人输入一个数字,如果它大于10,则显示消息"它起作用"。在哪里说"需要输入"是我需要系统扫描仪入口的地方。

操作员类:

class Classes {

private int Numbers;

public Classes() {}

Classes(String namez) {
    Numbers = Numbers;
}


public int getNumbers() {
    return Numbers;
}

public void setNumbers(int numberz) {
    if((Integer.parseInt(INPUT HERE.getText().toString()) )<=10) {
        System.out.print("It worked.");
    }
}
}

主要课程:

import java.util.Scanner;

public class OneTwoThree {

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter a number: ");
    Classes.Numbers(keyboard.nextLine());
}
}

3 个答案:

答案 0 :(得分:0)

首先

Classes.Numbers(keyboard.nextLine());
在您的名为Classes(keyboard.nextLine());

的班级中,

应首先替换为OneTwoThree

其次

Classes(String namez) {
    Numbers = Numbers;
}

这似乎非常错误。

应该用

之类的东西代替
Classes(String namez) {
    Numbers = Integer.parseInt(namez); //if you are entering integers only through keyboard
}

据我所知,你的问题,

你可以这样,

Classes(String namez) {
        Numbers = Integer.parseInt(namez); //if you are entering integers  only through keyboard

       performOperation(Numbers);// call a method you want,pass number as arg

    }


public static void performOperation(int num){
if(Numbers >10){
              //do stuff
            }
            else{
              //else part
            }
    }
}

此外,正如一个好习惯,您应该将变量Numbers命名为number

我希望它有所帮助。

答案 1 :(得分:0)

您只需传递字符串。

public static void testScanner() {
    try (Scanner keyboard = new Scanner(System.in);) {
        System.out.print("Enter a number: ");
        while (true) {
            String input = keyboard.nextLine();
            if (input.equalsIgnoreCase("exit")) {
                break;
            }
            Handler.handleInput(input);
        }
        System.out.println("Done.");
    }
}

static class Handler {
    public Handler() {

    }

    public static void handleInput(String input) {
        try {
            int x = Integer.parseInt(input);
            if (x <= 10) {
                System.out.println("It worked!");
            } else {
                System.out.println("Aw, Id didn't work.");
            }
        } catch (Exception ex) {
            System.out.println("Hey, watch it buddy. Don't throw any letters in there, I don't like them.");
        }
    }
}

答案 2 :(得分:0)

package mavens.dais.test;

public class ClassesTest {

   private int Numbers;

   public ClassesTest() {}

   ClassesTest(String namez) {
      Numbers = Integer.parseInt(namez);
   }


   public int getNumbers() {
      return Numbers;
   }

    public void setNumbers(int numberz) {
        if(numberz > 10){
            System.out.print("It is worked.");
        }else{
            System.out.print("It is not worked.");
        }
    }


}



package mavens.dais.test;

import java.util.Scanner;

public class OneTwoThre {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter a number: ");
        new ClassesTest().setNumbers(Integer.parseInt(keyboard.nextLine()));
    }   

}