偶数索引和奇数索引 - Java

时间:2016-06-14 13:37:56

标签: java

我正在尝试为以下程序编写代码:

1。询问您要输入的字数。

2。将单词作为输入,对于每个单词,显示偶数位置的字符和奇数位置的字符。[0被视为偶数位置]

所以我写了这段代码,但是它显示了一个错误,上面写着"数组无法解析为变量"。我无法弄清楚我哪里出错了。

import java.io.*;
import java.util.*;

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

        System.out.println("Enter the number of words: ");

        int T = in.nextInt();

        for(int m = 0; m<T; m++) {
            String S = in.nextLine();
            int N = S.length();
            char array[] = new char[N];
            for(int n = 0; n<N; n++) {
                array[n] = S.charAt(n+1);
            }
            display(N);
        }
    }

    public static void display(int N) {
        for(int i = 0; i<N; i = i + 2) {
            System.out.print(array[i]);
        }

        System.out.print(" ");

        for(int j = 1; j<N; j = j + 2) {
            System.out.print(array[j]);
        }
    }
}

7 个答案:

答案 0 :(得分:1)

您的代码存在许多问题,从违反命名约定代码识别规则到编译时错误(使用的数组变量)超出声明的范围/块)到运行时错误(在chatAt()处传递了错误的索引)。即使该代码运行正常,任何有经验的开发人员都会立即拒绝它。

我已经粘贴在更干净的工作版下面了(我仍然不会以这种方式实现它(不需要那个数组,一个String对象就够了),但是它最接近你原来的东西:

import java.io.*;
import java.util.*;

public class Trying4 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of words: ");
        int nrWords = Integer.parseInt(scanner.nextLine());

        for (int counter = 0; counter < nrWords; counter++) {
            System.out.print("Word[" + counter + "] : ");
            String word = scanner.nextLine();
            int length = word.length();
            char array[] = new char[length];
            for (int i = 0; i < length; i++) {
                array[i] = word.charAt(i);
            }
            display(length, array);
        }
    }

    public static void display(int N, char[] array) {
        System.out.print("Result EVEN : ");

        for (int i = 0; i < N; i = i + 2) {
            System.out.print(array[i]);
        }

        System.out.print("\nResult ODD : ");

        for (int j = 1; j < N; j = j + 2) {
            System.out.print(array[j]);
        }
        System.out.println("");
    }
}

答案 1 :(得分:0)

它因编译时错误而失败。

您需要将方法public static void display(int N)更改为public static void display(char[] array, int N)并将其传递给main方法中的数组:display(array, N);

这是你唯一的问题,还是有其他问题?

答案 2 :(得分:0)

正在生成错误,因为array正在display中使用,这超出了您声明​​的范围。

要解决此问题,您可以将array声明为实例变量(在任何方法之外),或将数组作为参数传递给display

另外,要从字符串中获取字符数组,请使用S.toCharArray()

import java.io.*;
import java.util.*;

public class Trying4 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the number of words: ");
        int numWords = in.nextInt();
        in.nextLine(); // consume the new-line character after the number
        for(int i = 0; i < numWords; i++) {
            char[] word = in.nextLine().toCharArray();
            display(word);
        }
    }
    public static void display(char[] word) {
        for(int i = 0; i < word.length; i += 2) {
            System.out.print(word[i]);
        }
        System.out.print(" ");
        for(int i = 1; i < word.length; j += 2) {
            System.out.print(word[i]);
        }
        System.out.println();
    }
}

答案 3 :(得分:0)

您需要将数组传递给display()函数。

import java.io.*;
import java.util.*;

public class Trying4 {

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

    System.out.println("Enter the number of words: ");

    int T = in.nextInt();

    for(int m = 0; m<T; m++)
        {
        String S = in.nextLine();
        int N = S.length();
        char array[] = new char[N];
        for(int n = 0; n<N; n++)
            {
            array[n] = S.charAt(n);
        }
        display(N, array);
        }
    }

public static void display(int N, char[] array){

    for(int i = 0; i<N; i = i + 2)
        {
          System.out.print(array[i]);
        }

    System.out.print(" ");

    for(int j = 1; j<N; j = j + 2)
        {
        System.out.print(array[j]);
        }
}
}

答案 4 :(得分:0)

还有一点小小的补充代码,有一个名为toCharArray()的内置方法,其名称是自我解释的,并消除了你正在使用的循环。 char array[] = in.NextLine().toCharArray();

答案 5 :(得分:0)

如果我可能触摸您的代码,因为人们在触摸代码时大多会感到沮丧。

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int wordCount = getInt(sc, "Enter the number of words: ");
    int wordIndex = 1; // Just to type a message "Word 1, word 2, etc."
    sc.nextLine();
    while (wordCount > 0) {
        System.out.printf("Type word %s: ", wordIndex);
        String word = sc.nextLine();
        List<Character> oddPosChars = new ArrayList<>();
        List<Character> evenPosChars = new ArrayList<>();
        for (int i = 0; i < word.length(); i++) {
            if (i % 2 == 0) {
                evenPosChars.add(word.charAt(i));
            } else {
                oddPosChars.add(word.charAt(i));
            }
        }
        print(evenPosChars, "Even Characters: ");
        print(oddPosChars, "Odd Characters: ");
        wordCount--;
        wordIndex++;
    }
}

static int getInt(Scanner sc, String message) {
    System.out.print(message);
    if (sc.hasNextInt()) {
        return sc.nextInt();
    }
    sc.next();
    System.out.println("Wrong input. Try again.");
    return getInt(sc, message);
}

static void print(List<Character> characters, String message) {
    System.out.print(message);
    characters.stream().forEach(System.out::print);
    System.out.println();
}

或者,如果您确定要使用数组:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int wordCount = getInt(sc, "Enter the number of words: ");
    int wordIndex = 1; // Just to type a message "Word 1, word 2, etc."
    sc.nextLine();
    while (wordCount > 0) {
        System.out.printf("Type word %s: ", wordIndex);
        String word = sc.nextLine();
        char[] evenPosChars = new char[word.length() - (word.length() / 2)];
        char[] oddPosChars = new char[word.length() / 2];
        for (int i = 0; i < word.length(); i++) {
            if (i % 2 == 0) {
                evenPosChars[i / 2] = word.charAt(i);
            } else {
                oddPosChars[(i - 1) / 2] = word.charAt(i);
            }
        }
        print(evenPosChars, "Even Characters: ");
        print(oddPosChars, "Odd Characters: ");
        wordCount--;
        wordIndex++;
    }
}

static int getInt(Scanner sc, String message) {
    System.out.print(message);
    if (sc.hasNextInt()) {
        return sc.nextInt();
    }
    sc.next();
    System.out.println("Wrong input. Try again.");
    return getInt(sc, message);
}

static void print(char[] characters, String message) {
    System.out.print(message);
    for (char character : characters) {
        System.out.print(character);
    }
    System.out.println();
}

答案 6 :(得分:0)

import java.io.*;
import java.util.*;
public class Solution {
 private static void ex(String S) {
  char c[] = S.toCharArray();
  int i, j;
  for (i = 0; i < c.length; i++) {
   System.out.print(c[i]);
   i += 1;
  }
  for (j = 1; j < c.length; j++) {
   System.out.print(c[j]);
   j += 1;
  }
 }
 public static void main(String[] args) {
  Scanner scan = new Scanner(System.in);
  int T = scan.nextInt();
  while (hasNext()) {
   for (int m = 0; m <= = T; m++) {
    String S = scan.next();
    ex(S);
    System.out.println();
   }
  }
 }
}