在String方法Java中获取int变量

时间:2017-11-18 01:45:07

标签: java methods

这是作业。只是把它放在那里。我觉得最好首先展示我的代码并解释我正在尝试做什么

import java.util.Scanner;
public class Question1 {
public static void main(String[] args){
    int length, odd, even, largest;
    int n = getNumber();
    length=odd=even=largest=initialize();
    String sequence=createSequence(n, largest, odd, even, length);
    displaySequence(sequence);
    displayStatistics(largest, length, odd, even);
}
private static int getNumber(){
   Scanner kb = new Scanner(System.in);
   System.out.println("Enter the value of the number: ");
   int number = kb.nextInt();
   return number;
}
private static int initialize(){
    return 0;
}
public static String createSequence(int n, int largest, int odd, int even, int length){
    String sequence="";
    sequence+=n+" ";
    while (n!=1){
        n = (n%2==0) ? n/2 : 3*n+1;
        System.out.print(", "+n);
        sequence+=n+" ";
        if(n%2==0){ even++;
        }else{ odd++;}
        if(n>=largest){ largest = n;
        }
        length++;
    }
    return sequence;
}
private static void displaySequence(String sequence){
    System.out.println(sequence);
}
public static String displayStatistics(int length, int even, int odd, int largest){
    String nil = "";
    System.out.println("The largest number in the sequence was "+largest);
    System.out.println("The length of the sequence was "+length);
    System.out.println("There were "+odd+" odd numbers");
    System.out.println("There were "+even+" even numbers");
    return nil;
}

}

我需要尝试显示序列中最大的数字,序列的长度,序列中有多少偶数以及序列中有多少个奇数。但由于我无法在createSequence方法中返回int值,因此无法获取每个统计信息的新值。阻止我显示所述统计数据。我如何访问这些新变量以用于最终方法?

注意:

要求:

  1. 在主

  2. 中声明变量
  3. 在Initialize()

  4. 中初始化变量
  5. createSequence(创建序列)

  6. displaySequence(然后以单独的方法显示序列)

  7. 最后showStatistics即长度,均衡,赔率,最大(用自己的方法),这是困扰我的那个

2 个答案:

答案 0 :(得分:1)

尝试从我的基本代码重新实现您的代码

public class Question{

    public class NumberStats {
        int len;
        boolean isOdd;
    }

    private ArrayList<NumberStats> stats = new ArrayList<>();
    public static void main(String[] args){
        Question q = new Question();
        Scanner kb = new Scanner(System.in);
        String number = "";
        do {
            System.out.println("Enter a series of numbers or q to quit: ");
            number = kb.next();
            q.stats.add(q.parseNumber(number));
        } while (number.equals("q")==false);

        q.printSummary();


    }

    private void printSummary(){
        int oddCount = 0;
        int evenCount = 0;
        int longestNumber = 0;

        for (NumberStats s : stats){
            if (longestNumber<s.len){
                longestNumber = s.len;
            }
            if (s.isOdd){
                oddCount+=1;
            } else {
                evenCount+=1;
            }
        }
        System.out.println(String.format("Longest number length was : %i, Odd Numbers: %i, Even Numbers: %i",longestNumber,oddCount,evenCount));
    }
    private NumberStats parseNumber(String number){
        NumberStats stats = new NumberStats();
        Integer lastNumber = Integer.parseInt(String.valueOf(number.charAt(number.length()));
        stats.isOdd = true;
        if (lastNumber%2==0){
            stats.isOdd = false;
        }
        stats.len = number.length();
        return stats;

    }
}

答案 1 :(得分:0)

如果您创建了一个Sequence类,您的老师可能会有点印象深刻。类属性将是统计数据(可以通过getter方法访问)。 createSequence方法将成为构造函数。您可以实现一个toString方法来返回内容的字符串表示。