gpt4 book ai didi

java - 在 Java String 方法中获取 int 变量

转载 作者:行者123 更新时间:2023-12-02 11:59:38 25 4
gpt4 key购买 nike

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

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 值,因此我无法获取每个统计信息的新值。阻止我显示上述统计数据。我如何访问这些新变量以在 final方法中使用?

注意:

要求:

  1. 在main中声明变量

  2. 在Initialize()中初始化变量

  3. createSequence(创建序列)

  4. displaySequence(然后在单独的方法中显示序列)

  5. 最后显示统计信息,即长度、偶数、赔率、最大(以自己的方法),这是困扰我的一个

最佳答案

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

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;

}
}

关于java - 在 Java String 方法中获取 int 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47361631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com