如何使用多个静态电源?

时间:2017-03-26 23:18:46

标签: java

尝试制作一个可以使用sumDigits,reverse和isPalindrome的程序。我需要从main调用它们并测试它们。我被告知要使用public static int sumDigits(int n),public static int reverse(int number)和public static boolean isPalindrome(int number)。我只是难以让所有三个程序一起运行。也不知道如何打印这三个答案。任何和所有的帮助将不胜感激。老实说,我现在能做的就是sumDigits x.x.我很困惑。

import java.util.Scanner;

public class Project3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Enter an integer");
int n = input.nextInt();
int sum = sumDigits(n);
System.out.println("The sum is " + sum);
System.out.println("The reverse is " + n);
}

public static int sumDigits(long n) {
int num = (int)(n);
int sum = 0;

while (num > 0) {

sum += num % 10;
num = num / 10;

}
return sum;
}
public static int reverse(int number) {
int reverse = 0;
int rem = 0;
while (number != 0) {
rem = number % 10;
reverse = (reverse * 10) + rem;
number = number / 10;
}
return reverse;
}

public static boolean isPalindrome(int number) {
int reverse = reverse(number);
if(reverse == number) {
return true;
}
else 
return false;
}
}

2 个答案:

答案 0 :(得分:0)

调用这3个方法在主方法中使用主要方法的名称:

public static void main(String arg[]){

   Scanner input = new Scanner(System.in);

   System.out.println("Enter an integer");
   int n = input.nextInt();
   int sum = sumDigits(n); //here is were the your first method is called
   System.out.println("The sum is " + sum);
   System.out.println("The reverse is " + n);

  int returnValue = reverse(5); //here is your second method call
  boolean rValue = isPalindrome(5); //here is your third method is called
}

我希望这有帮助

答案 1 :(得分:0)

Java具有静态和非静态方法和变量

静态

  • 在运行时

  • 之前定义
  • 如果在课堂上,它在所有实例之间共享

  • 通常用于实用程序函数和常量

非静态

  • 在运行时之前无法定义,需要实例化

  • 我们称之为"正常"方法,由单个类实例使用

现在在你的情况下,因为所有都是静态的,所以可以从静态上下文(例如main)调用它们,而不首先实例化一个类

以下是使用静态和非静态方法的一些示例代码

public class Util{

    public static void main(String[] args){
        Util.method1("static"); // calls on the utils static method and prints "static"
        String s = Util.method2("static2"); // calls the method, and gets the string back
        System.out.println(s); // writes out the string it got back
        Util util = new Util(); // instantiates Util class to call non static methods
        util.method3(); // prints "not static", notice this is a call on the instance not the class

        System.out.println(isPalindrome(12321));
        System.out.println(Util.isPalindrome(123321));
        System.out.println(Util.isPalindrome(123456));
        System.out.println(util.isPalindrome(82218615));
        System.out.println(util.isPalindrome(0));
        System.out.println(util.isPalindrome(-12321));
        System.out.println(Util.isPalindrome(-123321));
        System.out.println(isPalindrome(-123456));
        System.out.println(util.isPalindrome(-82218615));
        // notice how a static can be acced in 3 ways
        // defined in this class, so no need to define where it's from.
        // call it on the class it's from, by calling on the class that contains it
        // call on an instance of the class that contains it
    }
    public static void method1(String input){
        System.out.println(input); // prints input
    }

    public static String method2(String input){
        return input; // just for the example, normally useless
    }

    public void method3(){ // notice not static
        System.out.println("not static");
    }

    public static boolean isPalindrome(int in){ // checks if palindrome
        String input = Integer.toString(in); // String of the input
        StringBuilder sr = new StringBuilder(); // StringBuilder to reverse one side
        if(input.charAt(0) == ('-')) // remove - from negative numbers assuming that is still supposed to pass the test
            input = input.substring(1, input.length());
        String left; // left side
        String right; // right side

        if(input.length() % 2 == 0){ // even lengths
            left = input.substring(0, input.length()/2); // adds in whole left side to left string
            right = sr.append(input.substring(input.length()/2, input.length())).reverse().toString();// adds in whole right side to right string
            // note that the function above used the reverse on the string side
        }
        else { // odd lengths
            left = input.substring(0, input.length()/2);// adds in whole left side to left string
            right = sr.append(input.substring(input.length()/2+1, input.length()).toString()).reverse().toString();// adds in whole right side to right string
            // note that the function above used the reverse on the string side
        }
        return right.equals(left);// returns if true or false (palindrome or not based on if right == left after reversed one side)
    }
}

这会产生此输出(来自main方法)

  

静态

     

static2

     

不是静态的

     

     

     

     

     

     

     

     

     

额外

上面添加了代码

如何检查isPalindrome 理论上,回收物是诸如" 12321"这两种方式都是相同的,因此检查它们需要两件事,

  • 首先检查长度是否不均匀,如果是,则忽略中间,否则在2
  • 中均匀分开
  • 其次,反转其中一面,然后比较两边是否相等

最后请注意OOP,重视数据封装和数据完整性,这就是为什么通常一个类处理它自己的数据并且只提供静态方法作为实用程序