当前位置: 代码迷 >> java >> 只有偶数和只有奇数的字符串
  详细解决方案

只有偶数和只有奇数的字符串

热度:13   发布时间:2023-07-25 19:49:23.0

我知道已经有一些问题在问与我的问题类似的东西,但是尽管阅读了这些内容,但它们并没有完全满足我的要求。 我正在创建一个代码,接受用户输入的0-100(含)之间的数字。 无论数字是多少,它将打印出导致该数字和该数字产生的所有数字

例如:用户输入= 25输出= 012345678910111213141516171819202122232425

我有那部分工作。 现在,我应该使用该字符串并创建两个新字符串,一个仅用于奇数,另一个用于偶数。

例如:用户输入= 25输出:奇数:135791113151719212325和偶数= 024681012141618202224

到目前为止,这是我的代码:

import java.util.Scanner;

public class OddAndEven{
public String quantityToString() {
  Scanner number = new Scanner(System.in);
  int n = number.nextInt();
  String allNums = "";
  if ((n >= 0) && (n <= 100)) {
     for (int i = 0;i <= n; ++i)
     allNums = allNums + i;

     return allNums;
  }
  else {
     return "";
  }


}

  public void oddAndEvenNumbers(int num) {//Start of second method

     String allNums = ""; //String that quantityToString returns
     String odd = "";
     String even = "";

     if ((num >= 0) && (num < 10)) { //Looks at only single digit numbers
        for (int i = 0; i <= allNums.length(); i++) {
           if (Integer.parseInt(allNums.charAt(i))%2 == 0) { //trying to get the allNums string to be broken into individual numbers to evaluate
           even = even + allNums.charAt(i); //adding the even numbers of the string
           }
           else {
              odd = odd + allNums.charAt(i);
           }
        }
     }
     else { //supposed to handle numbers with double digits
        for (int i = 10; i <= allNums.length(); i = i + 2) {
           if (Integer.parseInt(allNums.charAt(i))%2 == 0) {
              even = even + allNums.charAt(i);
           }
           else {
              odd = odd + allNums.charAt(i);
           }
        }
     }
  System.out.println("Odd Numbers: " + odd);
  System.out.println("Even Numbers: " + even);

       }
public static void main(String[] args) {

System.out.println(new OddAndEven().quantityToString());
//System.out.println(new OddAndEven().oddAndEvenNumbers(allNums));
   //Testing     

  OddAndEven obj = new OddAndEven();
  System.out.println("Testing n = 5");
  obj.oddAndEvenNumbers(5);
  System.out.println("Testing n = 99");
  obj.oddAndEvenNumbers(99);

我知道我的问题在于应该将字符串拆开并评估各个数字,但是我不知道该怎么办。 (我也尝试过substring()&trim())而且我还没有学会如何使用数组,因此这就是为什么我没有尝试使用数组的原因。

我认为您可以这样:

    int x = 20;
    StringBuilder evenNumberStringBuilder = new StringBuilder();
    StringBuilder oddNumberStringBuilder = new StringBuilder();
    for(int i =0 ; i<x+1; i++){
        if(i % 2 == 0)evenNumberStringBuilder.append(i);
        else oddNumberStringBuilder.append(i);
    }

    System.out.println(evenNumberStringBuilder);
    System.out.println(oddNumberStringBuilder);

输出:

02468101214161820
135791113151719

您已经将输入作为整数,所以不要使用字符串。 我建议使用此循环。

    Scanner number = new Scanner(System.in);

    System.out.print("Even Numbers: ");
    for (int i = 0; i <= number; i=i+2) {
     System.out.print(i);
    }

    System.out.println("");

    System.out.print("Odd Numbers: ");
    for (int i = 1; i <= number; i=i+2) {
     System.out.print(i);
    }

您可以在将数字存储在allnumbers字符串中的同时简单地求值,这是一个起作用的代码:

    int x = 23; //user input
    String s=""; //contains all numbers from 0 to userinput
    String odd =""; //contains all odd numbers from 0 to userinput
    String even = ""; //contains all even numbers from 0 to userinput

    for(int i = 0 ; i< x+1 ; i++){
    s += i;
        if(i%2==0) //if i is an even number
            even += i;
        else //if i is an odd number 
            odd += i;
    }

    System.out.println(s); //displaying all numbers from 0 to user input
    System.out.println(odd); //displaying odd numbers from 0 to user input
    System.out.println(even); //displaying even numbers from 0 to user input
  相关解决方案